This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 616
Expand file tree
/
Copy pathJsonObject.cs
More file actions
252 lines (217 loc) · 7.58 KB
/
JsonObject.cs
File metadata and controls
252 lines (217 loc) · 7.58 KB
1
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using ServiceStack.Text.Common;
using ServiceStack.Text.Json;
namespace ServiceStack.Text
{
public static class JsonExtensions
{
public static T JsonTo<T>(this Dictionary<string, string> map, string key)
{
return Get<T>(map, key);
}
/// <summary>
/// Get JSON string value converted to T
/// </summary>
public static T Get<T>(this Dictionary<string, string> map, string key, T defaultValue = default)
{
if (map == null)
return default;
return map.TryGetValue(key, out var strVal) ? JsonSerializer.DeserializeFromString<T>(strVal) : defaultValue;
}
public static T[] GetArray<T>(this Dictionary<string, string> map, string key)
{
if (map == null)
return TypeConstants<T>.EmptyArray;
return map.TryGetValue(key, out var value)
? (map is JsonObject obj ? value.FromJson<T[]>() : value.FromJsv<T[]>())
: TypeConstants<T>.EmptyArray;
}
/// <summary>
/// Get JSON string value
/// </summary>
public static string Get(this Dictionary<string, string> map, string key)
{
if (map == null)
return null;
return map.TryGetValue(key, out var strVal)
? JsonTypeSerializer.Instance.UnescapeString(strVal)
: null;
}
public static JsonArrayObjects ArrayObjects(this string json)
{
return Text.JsonArrayObjects.Parse(json);
}
public static List<T> ConvertAll<T>(this JsonArrayObjects jsonArrayObjects, Func<JsonObject, T> converter)
{
var results = new List<T>();
foreach (var jsonObject in jsonArrayObjects)
{
results.Add(converter(jsonObject));
}
return results;
}
public static T ConvertTo<T>(this JsonObject jsonObject, Func<JsonObject, T> convertFn)
{
return jsonObject == null
? default
: convertFn(jsonObject);
}
public static Dictionary<string, string> ToDictionary(this JsonObject jsonObject)
{
return jsonObject == null
? new Dictionary<string, string>()
: new Dictionary<string, string>(jsonObject);
}
}
public class JsonObject : Dictionary<string, string>, IEnumerable<KeyValuePair<string, string>>
{
/// <summary>
/// Get JSON string value
/// </summary>
public new string this[string key]
{
get => this.Get(key);
set => base[key] = value;
}
public new Enumerator GetEnumerator()
{
var to = new Dictionary<string, string>();
foreach (var key in Keys)
{
to[key] = this[key];
}
return to.GetEnumerator();
}
IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, string>>.GetEnumerator()
=> GetEnumerator();
public Dictionary<string, string> ToUnescapedDictionary()
{
var to = new Dictionary<string, string>();
var enumerateAsConcreteDict = (Dictionary<string, string>)this;
foreach (var entry in enumerateAsConcreteDict)
{
to[entry.Key] = entry.Value;
}
return to;
}
public static JsonObject Parse(string json)
{
return JsonSerializer.DeserializeFromString<JsonObject>(json);
}
public static JsonArrayObjects ParseArray(string json)
{
return JsonArrayObjects.Parse(json);
}
public JsonArrayObjects ArrayObjects(string propertyName)
{
return this.TryGetValue(propertyName, out var strValue)
? JsonArrayObjects.Parse(strValue)
: null;
}
public JsonObject Object(string propertyName)
{
return this.TryGetValue(propertyName, out var strValue)
? Parse(strValue)
: null;
}
/// <summary>
/// Get unescaped string value
/// </summary>
public string GetUnescaped(string key)
{
return base[key];
}
/// <summary>
/// Get unescaped string value
/// </summary>
public string Child(string key)
{
return base[key];
}
/// <summary>
/// Write JSON Array, Object, bool or number values as raw string
/// </summary>
public static void WriteValue(TextWriter writer, object value)
{
var strValue = value as string;
if (!string.IsNullOrEmpty(strValue))
{
var firstChar = strValue[0];
var lastChar = strValue[strValue.Length - 1];
if ((firstChar == JsWriter.MapStartChar && lastChar == JsWriter.MapEndChar)
|| (firstChar == JsWriter.ListStartChar && lastChar == JsWriter.ListEndChar)
|| JsonUtils.True == strValue
|| JsonUtils.False == strValue
|| IsJavaScriptNumber(strValue))
{
writer.Write(strValue);
return;
}
}
JsonUtils.WriteString(writer, strValue);
}
private static bool IsJavaScriptNumber(string strValue)
{
var firstChar = strValue[0];
if (firstChar == '0')
{
if (strValue.Length == 1)
return true;
if (!strValue.Contains("."))
return false;
}
if (!strValue.Contains("."))
{
if (long.TryParse(strValue, out var longValue))
{
return longValue < JsonUtils.MaxInteger && longValue > JsonUtils.MinInteger;
}
return false;
}
if (double.TryParse(strValue, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
{
return doubleValue < JsonUtils.MaxInteger && doubleValue > JsonUtils.MinInteger;
}
return false;
}
public T ConvertTo<T>()
{
return (T)this.ConvertTo(typeof(T));
}
public object ConvertTo(Type type)
{
var map = new Dictionary<string, object>();
foreach (var entry in this)
{
map[entry.Key] = entry.Value;
}
return map.FromObjectDictionary(type);
}
}
public class JsonArrayObjects : List<JsonObject>
{
public static JsonArrayObjects Parse(string json)
{
return JsonSerializer.DeserializeFromString<JsonArrayObjects>(json);
}
}
public interface IValueWriter
{
void WriteTo(ITypeSerializer serializer, TextWriter writer);
}
public struct JsonValue : IValueWriter
{
private readonly string json;
public JsonValue(string json)
{
this.json = json;
}
public T As<T>() => JsonSerializer.DeserializeFromString<T>(json);
public override string ToString() => json;
public void WriteTo(ITypeSerializer serializer, TextWriter writer) => writer.Write(json ?? JsonUtils.Null);
}
}