1 #Region "Microsoft.VisualBasic::ab797db8f8d4be8697911e311fa88785, Microsoft.VisualBasic.Core\Serialization\JSON\JsonFormatter.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module JsonFormatter
35     
36     '         FunctionFormat, Minify
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Text.RegularExpressions
44 Imports Microsoft.VisualBasic.Serialization.JSON.Formatter.Internals
45
46 Namespace Serialization.JSON.Formatter
47
48     ''' <summary>
49     ''' Provides JSON formatting functionality.
50     ''' </summary>
51     Public Module JsonFormatter
52
53         ''' <summary>
54         ''' Returns a 'pretty printed' version of the specified JSON string, formatted for human
55         ''' consumption.
56         ''' </summary>
57         ''' <param name="json">A valid JSON string.</param>
58         ''' <returns>A 'pretty printed' version of the specified JSON string.</returns>
59         Public Function Format(json As StringAs String
60             If json Is Nothing Then
61                 Throw New ArgumentNullException("json should not be null.")
62             End If
63
64             Dim context As New JsonFormatterStrategyContext()
65             Dim formatter As New JsonFormatterInternal(context)
66
67             Return formatter.Format(json)
68         End Function
69
70         ''' <summary>
71         ''' Returns a 'minified' version of the specified JSON string, stripped of all 
72         ''' non-essential characters.
73         ''' </summary>
74         ''' <param name="json">A valid JSON string.</param>
75         ''' <returns>A 'minified' version of the specified JSON string.</returns>
76         Public Function Minify(json As StringAs String
77             If json Is Nothing Then
78                 Throw New ArgumentNullException("json should not be null.")
79             End If
80
81             Return Regex.Replace(json, "(""(?:[^""\\]|\\.)*"")|\s+""$1")
82         End Function
83     End Module
84 End Namespace