1 |
#Region "Microsoft.VisualBasic::13d066718eb77c8eee2f252c495064d0, Microsoft.VisualBasic.Core\Serialization\JSON\JsonSerialization.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
#End Region
|
45 |
|
46 |
Imports System.IO
|
47 |
Imports System.Runtime.CompilerServices
|
48 |
Imports System.Runtime.Serialization.Json
|
49 |
Imports System.Text
|
50 |
Imports System.Text.RegularExpressions
|
51 |
Imports System.Web.Script.Serialization
|
52 |
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
53 |
Imports Microsoft.VisualBasic.Language
|
54 |
Imports Microsoft.VisualBasic.Linq
|
55 |
Imports Microsoft.VisualBasic.Scripting.MetaData
|
56 |
Imports Microsoft.VisualBasic.Text
|
57 |
Imports Factory = System.Runtime.Serialization.Json.DataContractJsonSerializer
|
58 |
Imports r = System.Text.RegularExpressions.Regex
|
59 |
|
60 |
Namespace Serialization.JSON
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
<Package("Json.Contract")> Public Module JsonContract
|
67 |
|
68 |
<Extension>
|
69 |
Public Function MatrixJson(matrix As Double()()) As String
|
70 |
Dim rows = matrix.Select(Function(row) $"[ {row.JoinBy(", ")} ]")
|
71 |
Dim json = $"[ {rows.JoinBy("," & ASCII.LF)} ]"
|
72 |
Return json
|
73 |
End Function
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
<ExportAPI("Get.Json")>
|
82 |
<Extension>
|
83 |
Public Function GetObjectJson(type As Type, obj As Object,
|
84 |
Optional indent As Boolean = True,
|
85 |
Optional simpleDict As Boolean = True,
|
86 |
Optional knownTypes As IEnumerable(Of Type) = Nothing) As String
|
87 |
|
88 |
Using ms As New MemoryStream()
|
89 |
Call ms.writeJsonInternal(
|
90 |
obj:=obj,
|
91 |
type:=type,
|
92 |
simpleDict:=simpleDict,
|
93 |
knownTypes:=knownTypes
|
94 |
)
|
95 |
|
96 |
If indent Then
|
97 |
Return Formatter.Format(Encoding.UTF8.GetString(ms.ToArray()))
|
98 |
Else
|
99 |
Return Encoding.UTF8.GetString(ms.ToArray())
|
100 |
End If
|
101 |
End Using
|
102 |
End Function
|
103 |
|
104 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
105 |
<Extension>
|
106 |
Private Sub writeJsonInternal(output As Stream, obj As Object, type As Type, simpleDict As Boolean, knownTypes As IEnumerable(Of Type))
|
107 |
If simpleDict Then
|
108 |
Dim settings As New DataContractJsonSerializerSettings With {
|
109 |
.UseSimpleDictionaryFormat = True,
|
110 |
.SerializeReadOnlyTypes = True,
|
111 |
.KnownTypes = knownTypes _
|
112 |
.SafeQuery _
|
113 |
.ToArray
|
114 |
}
|
115 |
Call New Factory(type, settings).WriteObject(output, obj)
|
116 |
Else
|
117 |
Call New Factory(type).WriteObject(output, obj)
|
118 |
End If
|
119 |
End Sub
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
<Extension>
|
129 |
Public Function WriteLargeJson(Of T)(obj As T, path$, Optional simpleDict As Boolean = True) As Boolean
|
130 |
Using ms As FileStream = path.Open(, doClear:=True)
|
131 |
Call ms.writeJsonInternal(obj, GetType(T), simpleDict, Nothing)
|
132 |
End Using
|
133 |
|
134 |
Return True
|
135 |
End Function
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
143 |
<Extension>
|
144 |
Public Function RemoveJsonNullItems(json As String) As String
|
145 |
Return r.Replace(json, """[^""]+""[:]\s*null\s*,?", "", RegexICSng)
|
146 |
End Function
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
162 |
<Extension> Public Function GetJson(Of T)(obj As T,
|
163 |
Optional indent As Boolean = False,
|
164 |
Optional simpleDict As Boolean = True,
|
165 |
Optional knownTypes As IEnumerable(Of Type) = Nothing) As String
|
166 |
Return GetType(T).GetObjectJson(obj, indent, simpleDict, knownTypes)
|
167 |
End Function
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
<param name="json">null -> Nothing</param>
|
173 |
|
174 |
|
175 |
<ExportAPI("LoadObject")>
|
176 |
<Extension>
|
177 |
Public Function LoadObject(json$,
|
178 |
type As Type,
|
179 |
Optional simpleDict As Boolean = True,
|
180 |
Optional throwEx As Boolean = True,
|
181 |
Optional ByRef exception As Exception = Nothing) As Object
|
182 |
|
183 |
If String.Equals(json, "null", StringComparison.OrdinalIgnoreCase) Then
|
184 |
Return Nothing
|
185 |
End If
|
186 |
|
187 |
Using MS As New MemoryStream(Encoding.UTF8.GetBytes(json))
|
188 |
Dim settings As New DataContractJsonSerializerSettings With {
|
189 |
.UseSimpleDictionaryFormat = simpleDict,
|
190 |
.SerializeReadOnlyTypes = True
|
191 |
}
|
192 |
Dim ser As New DataContractJsonSerializer(type, settings)
|
193 |
Dim de As Func(Of Object) = Function() ser.ReadObject(MS)
|
194 |
Dim obj = TryCatch(de, $"Incorrect JSON string format => >>>{json}<<<", throwEx, exception)
|
195 |
Return obj
|
196 |
End Using
|
197 |
End Function
|
198 |
|
199 |
<Extension>
|
200 |
Public Function LoadJSONObject(jsonStream As Stream, type As Type, Optional simpleDict As Boolean = True) As Object
|
201 |
If jsonStream Is Nothing Then
|
202 |
Return Nothing
|
203 |
Else
|
204 |
Dim settings As New DataContractJsonSerializerSettings With {
|
205 |
.UseSimpleDictionaryFormat = simpleDict,
|
206 |
.SerializeReadOnlyTypes = True
|
207 |
}
|
208 |
Return New DataContractJsonSerializer(type, settings) _
|
209 |
.ReadObject(jsonStream)
|
210 |
End If
|
211 |
End Function
|
212 |
|
213 |
|
214 |
从文本文件或者文本内容之中进行JSON反序列化
|
215 |
|
216 |
<param name="json">This string value can be json text or json file path.</param>
|
217 |
<Extension> Public Function LoadJSON(Of T)(json$,
|
218 |
Optional simpleDict As Boolean = True,
|
219 |
Optional throwEx As Boolean = True,
|
220 |
Optional ByRef exception As Exception = Nothing) As T
|
221 |
Dim text$ = json.SolveStream(Encodings.UTF8)
|
222 |
Dim value As Object = text.LoadObject(GetType(T), simpleDict, throwEx, exception)
|
223 |
Dim obj As T = DirectCast(value, T)
|
224 |
Return obj
|
225 |
End Function
|
226 |
|
227 |
|
228 |
XML CDATA to json
|
229 |
|
230 |
|
231 |
|
232 |
<param name="simpleDict"></param>
|
233 |
|
234 |
|
235 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
236 |
<Extension>
|
237 |
Public Function LoadObject(Of T As New)(json As XElement, Optional simpleDict As Boolean = True) As T
|
238 |
Return json.Value.LoadJSON(Of T)(simpleDict:=simpleDict)
|
239 |
End Function
|
240 |
|
241 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
242 |
<Extension>
|
243 |
Public Function LoadJsonFile(Of T)(file$, Optional encoding As Encoding = Nothing, Optional simpleDict As Boolean = True) As T
|
244 |
Return (file.ReadAllText(encoding Or UTF8, throwEx:=False, suppress:=True) Or "null".AsDefault) _
|
245 |
.LoadJSON(Of T)(simpleDict)
|
246 |
End Function
|
247 |
|
248 |
Const JsonLongTime$ = "\d+-\d+-\d+T\d+:\d+:\d+\.\d+"
|
249 |
|
250 |
Public Function EnsureDate(json$, Optional propertyName$ = Nothing) As String
|
251 |
Dim pattern$ = $"""{JsonLongTime}"""
|
252 |
|
253 |
If Not propertyName.StringEmpty Then
|
254 |
pattern = $"""{propertyName}""\s*:\s*" & pattern
|
255 |
End If
|
256 |
|
257 |
Dim dates = r.Matches(json, pattern, RegexICSng)
|
258 |
Dim sb As New StringBuilder(json)
|
259 |
Dim [date] As Date
|
260 |
|
261 |
For Each m As Match In dates
|
262 |
Dim s$ = m.Value
|
263 |
|
264 |
If Not propertyName.StringEmpty Then
|
265 |
With r.Replace(s, $"""{propertyName}""\s*:", "", RegexICSng) _
|
266 |
.Trim _
|
267 |
.Trim(ASCII.Quot)
|
268 |
|
269 |
[date] = Date.Parse(.ByRef)
|
270 |
End With
|
271 |
sb.Replace(s, $"""{propertyName}"":" & [date].GetJson)
|
272 |
Else
|
273 |
[date] = Date.Parse(s.Trim(ASCII.Quot))
|
274 |
sb.Replace(s, [date].GetJson)
|
275 |
End If
|
276 |
Next
|
277 |
|
278 |
Return sb.ToString
|
279 |
End Function
|
280 |
End Module
|
281 |
End Namespace
|