/
JToken.cs
565 lines (511 loc) · 19.8 KB
/
JToken.cs
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Jsonata.Net.Native.Eval;
namespace Jsonata.Net.Native.Json
{
[DebuggerDisplay("{Type}: {ToFlatString()}")]
public abstract class JToken
{
public readonly JTokenType Type;
private JToken? m_parent = null;
internal JToken? parent
{
get => this.m_parent;
set
{
if (this == EvalProcessor.UNDEFINED && value != null)
{
throw new InvalidOperationException($"Attempt to set parent on {nameof(EvalProcessor)}.{nameof(EvalProcessor.UNDEFINED)}");
}
this.m_parent = value;
}
}
protected JToken(JTokenType type)
{
this.Type = type;
}
private static JValue AsValue(JToken token)
{
if (token is JValue value)
{
return value;
}
throw new Exception("Token is not a JValue");
}
public static explicit operator int(JToken value)
{
JValue v = AsValue(value);
if (v.Type != JTokenType.Integer)
{
throw new Exception("Cannot convert to int");
}
return Convert.ToInt32(v.Value, CultureInfo.InvariantCulture);
}
public static explicit operator long(JToken value)
{
JValue v = AsValue(value);
if (v.Type != JTokenType.Integer)
{
throw new Exception("Cannot convert to long");
}
return Convert.ToInt64(v.Value, CultureInfo.InvariantCulture);
}
public static explicit operator decimal(JToken value)
{
JValue v = AsValue(value);
if (v.Type != JTokenType.Float)
{
throw new ArgumentException("Can not convert to Decimal");
}
return Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture);
}
public static explicit operator double(JToken value)
{
JValue v = AsValue(value);
if (v.Type != JTokenType.Float)
{
throw new ArgumentException("Can not convert to Double");
}
return Convert.ToDouble(v.Value, CultureInfo.InvariantCulture);
}
public static explicit operator float(JToken value)
{
JValue v = AsValue(value);
if (v.Type != JTokenType.Float)
{
throw new ArgumentException("Can not convert to Double");
}
return Convert.ToSingle(v.Value, CultureInfo.InvariantCulture);
}
public static explicit operator string(JToken value)
{
JValue v = AsValue(value);
if (v.Type != JTokenType.String)
{
throw new ArgumentException("Can not convert to String");
}
return Convert.ToString(v.Value, CultureInfo.InvariantCulture)!;
}
public static explicit operator bool(JToken value)
{
JValue v = AsValue(value);
if (v.Type != JTokenType.Boolean)
{
throw new ArgumentException("Can not convert to Bool");
}
return Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture);
}
public static JToken Parse(TextReader reader, ParseSettings? settings = null)
{
JsonParser parser = new JsonParser(reader, settings ?? ParseSettings.DefaultSettings);
return parser.Parse();
}
public static JToken Parse(string source, ParseSettings? settings = null)
{
using (StringReader reader = new StringReader(source))
{
return Parse(reader, settings);
}
}
public static JToken FromObject(object? sourceObj)
{
switch (sourceObj)
{
case null:
return JValue.CreateNull();
case bool value:
return new JValue(value);
case string value:
return new JValue(value);
case char value:
return new JValue(value);
case int value:
return new JValue(value);
case uint value:
return new JValue(value);
case long value:
return new JValue(value);
case ulong value:
return new JValue((decimal)value); //won't fit in (s)long
case byte value:
return new JValue(value);
case sbyte value:
return new JValue(value);
case short value:
return new JValue(value);
case ushort value:
return new JValue(value);
case float value:
return new JValue(value);
case double value:
return new JValue(value);
case decimal value:
return new JValue(value);
case System.Collections.IDictionary dictionary:
return FromDictionary(dictionary);
case System.Collections.ICollection list:
return FromCollection(list);
default:
return FromObj(sourceObj);
}
}
private static JToken FromObj(object sourceObj)
{
JObject result = new JObject();
foreach (PropertyInfo pi in sourceObj.GetType().GetProperties())
{
result.Add(pi.Name, JToken.FromObject(pi.GetValue(sourceObj)));
}
return result;
}
private static JToken FromCollection(ICollection list)
{
JArray array = new JArray(list.Count);
foreach (object? item in list)
{
array.Add(JToken.FromObject(item));
}
return array;
}
private static JToken FromDictionary(IDictionary dictionary)
{
JObject result = new JObject();
foreach (DictionaryEntry entry in dictionary)
{
result.Add(entry.Key.ToString()!, JToken.FromObject(entry.Value));
}
return result;
}
internal void ClearParent()
{
this.parent = null;
this.ClearParentNested();
}
protected abstract void ClearParentNested();
public string ToIndentedString()
{
return this.ToIndentedString(SerializationSettings.DefaultSettings);
}
public string ToIndentedString(SerializationSettings options)
{
StringBuilder builder = new StringBuilder();
this.ToIndentedStringImpl(builder, 0, options);
return builder.ToString();
}
internal abstract void ToIndentedStringImpl(StringBuilder builder, int indent, SerializationSettings options);
public string ToFlatString()
{
return this.ToFlatString(SerializationSettings.DefaultSettings);
}
public string ToFlatString(SerializationSettings options)
{
StringBuilder builder = new StringBuilder();
this.ToStringFlatImpl(builder, options);
return builder.ToString();
}
internal abstract void ToStringFlatImpl(StringBuilder builder, SerializationSettings options);
public abstract JToken DeepClone();
public static bool DeepEquals(JToken lhs, JToken rhs)
{
return lhs.DeepEquals(rhs);
}
public abstract bool DeepEquals(JToken other);
public T ToObject<T>()
{
return this.ToObject<T>(ToObjectSettings.DefaultSettings)!;
}
public T ToObject<T>(ToObjectSettings settings)
{
return (T)this.ToObject(typeof(T), settings)!;
}
public object? ToObject(Type type)
{
return this.ToObject(type, ToObjectSettings.DefaultSettings);
}
public object? ToObject(Type type, ToObjectSettings settings)
{
if (type == typeof(string))
{
if (this.Type == JTokenType.Null)
{
return null;
}
return (string)this;
}
else if (type == typeof(int))
{
return (int)this;
}
else if (type == typeof(long))
{
return (long)this;
}
else if (type == typeof(float))
{
if (this.Type == JTokenType.Integer)
{
//explicit support, because casts are strict for a reason
return (float)(long)this;
}
return (float)this;
}
else if (type == typeof(double))
{
if (this.Type == JTokenType.Integer)
{
//explicit support, because casts are strict for a reason
return (double)(long)this;
}
return (double)this;
}
else if (type == typeof(decimal))
{
if (this.Type == JTokenType.Integer)
{
//explicit support, because casts are strict for a reason
return (decimal)(long)this;
}
return (decimal)this;
}
else if (type == typeof(bool))
{
return (bool)this;
}
else if (Nullable.GetUnderlyingType(type) != null)
{
if (this.Type == JTokenType.Null)
{
return null;
}
else
{
//wrap value in Nullable<T>
Type valueType = Nullable.GetUnderlyingType(type)!;
object? value = this.ToObject(valueType, settings);
object? result = Activator.CreateInstance(type, new object?[] { value });
return result;
}
}
else if (typeof(IDictionary).IsAssignableFrom(type))
{
if (type.GenericTypeArguments.Length != 2 || type.GenericTypeArguments[0] != typeof(string))
{
throw new ArgumentException($"Cannot convert to dict of type {type.Name}: unexpected generic args");
}
Type valueType = type.GenericTypeArguments[1];
Type resultType = typeof(Dictionary<,>).MakeGenericType(typeof(string), valueType);
if (!type.IsAssignableFrom(resultType))
{
throw new ArgumentException($"Cannot convert to dict of type {type.Name}: not assignable from Dictionary");
}
if (this.Type == JTokenType.Null)
{
return null;
}
else if (this.Type == JTokenType.Object)
{
return ConvertToDictionary(resultType, valueType, settings);
}
else
{
throw new ArgumentException($"Cannot convert {this.Type} to dict {type.Name}");
}
}
else if (typeof(IList).IsAssignableFrom(type))
{
if (type.GenericTypeArguments.Length != 1)
{
throw new ArgumentException($"Cannot convert to list of type {type.Name}: unexpected generic args");
}
Type valueType = type.GenericTypeArguments[0];
Type resultType = typeof(List<>).MakeGenericType(valueType);
if (!type.IsAssignableFrom(resultType))
{
throw new ArgumentException($"Cannot convert to list of type {type.Name}: not assignable from List");
}
if (this.Type == JTokenType.Null)
{
return null;
}
else if (this.Type == JTokenType.Array)
{
return this.ConvertToList(resultType, valueType, settings);
}
else
{
throw new ArgumentException($"Cannot convert {this.Type} to array {type.Name}");
}
}
else if (type.IsEnum)
{
string? value = (string)this;
#if (NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1)
if (!Enum.TryParse(type, value, out object? result))
{
throw new ArgumentException($"Failed to parse '{value}' to enum {type.Name}");
}
return result;
#else
try
{
object? result = Enum.Parse(type, value);
return result;
}
catch (Exception)
{
throw new ArgumentException($"Failed to parse '{value}' to enum {type.Name}");
}
#endif
}
else if (type == typeof(object))
{
switch (this.Type)
{
case JTokenType.Object:
return this.ConvertToDictionary(typeof(Dictionary<,>).MakeGenericType(typeof(string), typeof(object)), typeof(object), settings);
case JTokenType.Array:
return this.ConvertToList(typeof(List<>).MakeGenericType(typeof(object)), typeof(object), settings);
case JTokenType.Integer:
return (long)this;
case JTokenType.Float:
return (double)this;
case JTokenType.String:
return (string)this;
case JTokenType.Boolean:
return (bool)this;
case JTokenType.Null:
return null;
default:
throw new ArgumentException($"Cannot convert {this.Type} to object");
}
}
else if (type.IsClass)
{
if (this.Type == JTokenType.Null)
{
return null;
}
else if (this.Type == JTokenType.Object)
{
object result;
try
{
result = Activator.CreateInstance(type)!;
}
catch (Exception ex)
{
throw new ArgumentException($"Failed to create instance of class {type.Name}: {ex.Message}", ex);
}
Dictionary<string, PropertyInfo> resultProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(pi => pi.Name);
JObject thisObj = (JObject)this;
foreach (KeyValuePair<string, PropertyInfo> resultProperty in resultProperties)
{
if (!thisObj.Properties.TryGetValue(resultProperty.Key, out JToken? thisProperty))
{
if (settings.AllowMissingProperties)
{
continue;
}
throw new ArgumentException($"Missing value for '{resultProperty.Key}'");
}
object? value;
try
{
value = thisProperty.ToObject(resultProperty.Value.PropertyType, settings);
}
catch (Exception ex)
{
throw new ArgumentException($"Failed to convert value for '{resultProperty.Key}': {ex.Message}", ex);
}
try
{
resultProperty.Value.SetValue(result, value);
}
catch (Exception ex)
{
throw new ArgumentException($"Failed to set value for '{resultProperty.Key}': {ex.Message}", ex);
}
}
if (!settings.AllowUndecaredProperties && thisObj.Keys.Except(resultProperties.Keys).Any())
{
throw new ArgumentException($"Specified unknown properties: {String.Join(",", thisObj.Keys.Except(resultProperties.Keys))}");
}
return result;
}
else
{
throw new ArgumentException($"Cannot convert {this.Type} to obj {type.Name}");
}
}
else
{
throw new ArgumentException($"Cannot convert {this.Type} to some {type.Name}");
}
}
private object ConvertToList(Type listType, Type valueType, ToObjectSettings settings)
{
System.Collections.IList result = (System.Collections.IList)Activator.CreateInstance(listType)!;
foreach (JToken element in ((JArray)this).ChildrenTokens)
{
object? value = element.ToObject(valueType, settings);
result.Add(value);
}
return result;
}
private object ConvertToDictionary(Type dictionaryType, Type valueType, ToObjectSettings settings)
{
System.Collections.IDictionary result = (System.Collections.IDictionary)Activator.CreateInstance(dictionaryType)!;
foreach (KeyValuePair<string, JToken> property in ((JObject)this).Properties)
{
object? value = property.Value.ToObject(valueType, settings);
result.Add(property.Key, value);
}
return result;
}
//see https://stackoverflow.com/questions/19176024/how-to-escape-special-characters-in-building-a-json-string
// https://www.freeformatter.com/json-escape.html
public static void EscapeString(string source, StringBuilder target)
{
foreach (char c in source)
{
switch (c)
{
case '\b':
target.Append(@"\b");
break;
case '\f':
target.Append(@"\f");
break;
case '\n':
target.Append(@"\n");
break;
case '\r':
target.Append(@"\r");
break;
case '\t':
target.Append(@"\t");
break;
case '"':
target.Append("\\\"");
break;
case '\\':
target.Append(@"\\");
break;
default:
target.Append(c);
break;
}
}
}
}
}