Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonValue.cs
Go to the documentation of this file.
5
7
8public abstract class JsonValue : JsonNode
9{
10 internal const string CreateUnreferencedCodeMessage = "Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.";
11
16
17 public static JsonValue? Create(bool? value, JsonNodeOptions? options = null)
18 {
19 if (!value.HasValue)
20 {
21 return null;
22 }
24 }
25
30
31 public static JsonValue? Create(byte? value, JsonNodeOptions? options = null)
32 {
33 if (!value.HasValue)
34 {
35 return null;
36 }
38 }
39
44
45 public static JsonValue? Create(char? value, JsonNodeOptions? options = null)
46 {
47 if (!value.HasValue)
48 {
49 return null;
50 }
52 }
53
58
60 {
61 if (!value.HasValue)
62 {
63 return null;
64 }
66 }
67
72
74 {
75 if (!value.HasValue)
76 {
77 return null;
78 }
80 }
81
86
87 public static JsonValue? Create(decimal? value, JsonNodeOptions? options = null)
88 {
89 if (!value.HasValue)
90 {
91 return null;
92 }
94 }
95
100
101 public static JsonValue? Create(double? value, JsonNodeOptions? options = null)
102 {
103 if (!value.HasValue)
104 {
105 return null;
106 }
108 }
109
114
116 {
117 if (!value.HasValue)
118 {
119 return null;
120 }
122 }
123
128
129 public static JsonValue? Create(short? value, JsonNodeOptions? options = null)
130 {
131 if (!value.HasValue)
132 {
133 return null;
134 }
136 }
137
142
143 public static JsonValue? Create(int? value, JsonNodeOptions? options = null)
144 {
145 if (!value.HasValue)
146 {
147 return null;
148 }
150 }
151
156
157 public static JsonValue? Create(long? value, JsonNodeOptions? options = null)
158 {
159 if (!value.HasValue)
160 {
161 return null;
162 }
164 }
165
166 [CLSCompliant(false)]
171
172 [CLSCompliant(false)]
173 public static JsonValue? Create(sbyte? value, JsonNodeOptions? options = null)
174 {
175 if (!value.HasValue)
176 {
177 return null;
178 }
180 }
181
186
187 public static JsonValue? Create(float? value, JsonNodeOptions? options = null)
188 {
189 if (!value.HasValue)
190 {
191 return null;
192 }
194 }
195
196 public static JsonValue? Create(string? value, JsonNodeOptions? options = null)
197 {
198 if (value == null)
199 {
200 return null;
201 }
203 }
204
205 [CLSCompliant(false)]
210
211 [CLSCompliant(false)]
212 public static JsonValue? Create(ushort? value, JsonNodeOptions? options = null)
213 {
214 if (!value.HasValue)
215 {
216 return null;
217 }
219 }
220
221 [CLSCompliant(false)]
226
227 [CLSCompliant(false)]
228 public static JsonValue? Create(uint? value, JsonNodeOptions? options = null)
229 {
230 if (!value.HasValue)
231 {
232 return null;
233 }
235 }
236
237 [CLSCompliant(false)]
242
243 [CLSCompliant(false)]
244 public static JsonValue? Create(ulong? value, JsonNodeOptions? options = null)
245 {
246 if (!value.HasValue)
247 {
248 return null;
249 }
251 }
252
254 {
255 if (value.ValueKind == JsonValueKind.Null)
256 {
257 return null;
258 }
261 }
262
264 {
265 if (!value.HasValue)
266 {
267 return null;
268 }
269 JsonElement element = value.Value;
270 if (element.ValueKind == JsonValueKind.Null)
271 {
272 return null;
273 }
276 }
277
278 private protected JsonValue(JsonNodeOptions? options = null)
279 : base(options)
280 {
281 }
282
283 [RequiresUnreferencedCode("Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed. Use the overload that takes a JsonTypeInfo, or make sure all of the required types are preserved.")]
284 public static JsonValue? Create<T>(T? value, JsonNodeOptions? options = null)
285 {
286 if (value == null)
287 {
288 return null;
289 }
290 if (value is JsonElement)
291 {
292 object obj = value;
293 JsonElement element = (JsonElement)((obj is JsonElement) ? obj : null);
294 if (element.ValueKind == JsonValueKind.Null)
295 {
296 return null;
297 }
300 }
302 }
303
305 {
306 if (jsonTypeInfo == null)
307 {
308 throw new ArgumentNullException("jsonTypeInfo");
309 }
310 if (value == null)
311 {
312 return null;
313 }
314 if (value is JsonElement)
315 {
316 object obj = value;
317 JsonElement element = (JsonElement)((obj is JsonElement) ? obj : null);
318 if (element.ValueKind == JsonValueKind.Null)
319 {
320 return null;
321 }
323 }
325 }
326
327 internal override void GetPath(List<string> path, JsonNode child)
328 {
329 if (base.Parent != null)
330 {
331 base.Parent.GetPath(path, this);
332 }
333 }
334
335 public abstract bool TryGetValue<T>([NotNullWhen(true)] out T? value);
336
338 {
339 if (element.ValueKind == JsonValueKind.Object || element.ValueKind == JsonValueKind.Array)
340 {
342 }
343 }
344}
345[DebuggerDisplay("{ToJsonString(),nq}")]
347internal abstract class JsonValue<TValue> : JsonValue
348{
350 [DebuggerDisplay("{Json,nq}")]
351 private class DebugView
352 {
355
356 public string Json => _node.ToJsonString();
357
358 public string Path => _node.GetPath();
359
360 public TValue Value => _node.Value;
361
363 {
364 _node = node;
365 }
366 }
367
368 public readonly TValue _value;
369
370 public TValue Value => _value;
371
372 public JsonValue(TValue value, JsonNodeOptions? options = null)
373 : base(options)
374 {
375 if (value is JsonNode)
376 {
378 }
379 _value = value;
380 }
381
382 public override T GetValue<T>()
383 {
384 TValue value = _value;
385 if (value is T)
386 {
387 object obj = value;
388 return (T)((obj is T) ? obj : null);
389 }
391 {
392 return ConvertJsonElement<T>();
393 }
395 }
396
397 public override bool TryGetValue<T>([NotNullWhen(true)] out T value)
398 {
399 TValue value2 = _value;
400 if (value2 is T val)
401 {
402 value = val;
403 return true;
404 }
405 value2 = _value;
407 {
409 }
410 value = default(T);
411 return false;
412 }
413
414 internal TypeToConvert ConvertJsonElement<TypeToConvert>()
415 {
417 switch (jsonElement.ValueKind)
418 {
419 case JsonValueKind.Number:
420 if (typeof(TypeToConvert) == typeof(int) || typeof(TypeToConvert) == typeof(int?))
421 {
422 return (TypeToConvert)(object)jsonElement.GetInt32();
423 }
424 if (typeof(TypeToConvert) == typeof(long) || typeof(TypeToConvert) == typeof(long?))
425 {
426 return (TypeToConvert)(object)jsonElement.GetInt64();
427 }
428 if (typeof(TypeToConvert) == typeof(double) || typeof(TypeToConvert) == typeof(double?))
429 {
430 return (TypeToConvert)(object)jsonElement.GetDouble();
431 }
432 if (typeof(TypeToConvert) == typeof(short) || typeof(TypeToConvert) == typeof(short?))
433 {
434 return (TypeToConvert)(object)jsonElement.GetInt16();
435 }
436 if (typeof(TypeToConvert) == typeof(decimal) || typeof(TypeToConvert) == typeof(decimal?))
437 {
438 return (TypeToConvert)(object)jsonElement.GetDecimal();
439 }
440 if (typeof(TypeToConvert) == typeof(byte) || typeof(TypeToConvert) == typeof(byte?))
441 {
442 return (TypeToConvert)(object)jsonElement.GetByte();
443 }
444 if (typeof(TypeToConvert) == typeof(float) || typeof(TypeToConvert) == typeof(float?))
445 {
446 return (TypeToConvert)(object)jsonElement.GetSingle();
447 }
448 if (typeof(TypeToConvert) == typeof(uint) || typeof(TypeToConvert) == typeof(uint?))
449 {
450 return (TypeToConvert)(object)jsonElement.GetUInt32();
451 }
452 if (typeof(TypeToConvert) == typeof(ushort) || typeof(TypeToConvert) == typeof(ushort?))
453 {
454 return (TypeToConvert)(object)jsonElement.GetUInt16();
455 }
456 if (typeof(TypeToConvert) == typeof(ulong) || typeof(TypeToConvert) == typeof(ulong?))
457 {
458 return (TypeToConvert)(object)jsonElement.GetUInt64();
459 }
460 if (typeof(TypeToConvert) == typeof(sbyte) || typeof(TypeToConvert) == typeof(sbyte?))
461 {
462 return (TypeToConvert)(object)jsonElement.GetSByte();
463 }
464 break;
465 case JsonValueKind.String:
466 if (typeof(TypeToConvert) == typeof(string))
467 {
468 return (TypeToConvert)(object)jsonElement.GetString();
469 }
470 if (typeof(TypeToConvert) == typeof(DateTime) || typeof(TypeToConvert) == typeof(DateTime?))
471 {
472 return (TypeToConvert)(object)jsonElement.GetDateTime();
473 }
474 if (typeof(TypeToConvert) == typeof(DateTimeOffset) || typeof(TypeToConvert) == typeof(DateTimeOffset?))
475 {
476 return (TypeToConvert)(object)jsonElement.GetDateTimeOffset();
477 }
478 if (typeof(TypeToConvert) == typeof(Guid) || typeof(TypeToConvert) == typeof(Guid?))
479 {
480 return (TypeToConvert)(object)jsonElement.GetGuid();
481 }
482 if (typeof(TypeToConvert) == typeof(char) || typeof(TypeToConvert) == typeof(char?))
483 {
484 string @string = jsonElement.GetString();
485 if (@string.Length == 1)
486 {
487 return (TypeToConvert)(object)@string[0];
488 }
489 }
490 break;
491 case JsonValueKind.True:
492 case JsonValueKind.False:
493 if (typeof(TypeToConvert) == typeof(bool) || typeof(TypeToConvert) == typeof(bool?))
494 {
495 return (TypeToConvert)(object)jsonElement.GetBoolean();
496 }
497 break;
498 }
500 }
501
502 internal bool TryConvertJsonElement<TypeToConvert>([NotNullWhen(true)] out TypeToConvert result)
503 {
505 switch (jsonElement.ValueKind)
506 {
507 case JsonValueKind.Number:
508 if (typeof(TypeToConvert) == typeof(int) || typeof(TypeToConvert) == typeof(int?))
509 {
510 int value;
511 bool result2 = jsonElement.TryGetInt32(out value);
512 result = (TypeToConvert)(object)value;
513 return result2;
514 }
515 if (typeof(TypeToConvert) == typeof(long) || typeof(TypeToConvert) == typeof(long?))
516 {
517 long value2;
518 bool result2 = jsonElement.TryGetInt64(out value2);
519 result = (TypeToConvert)(object)value2;
520 return result2;
521 }
522 if (typeof(TypeToConvert) == typeof(double) || typeof(TypeToConvert) == typeof(double?))
523 {
524 double value3;
525 bool result2 = jsonElement.TryGetDouble(out value3);
526 result = (TypeToConvert)(object)value3;
527 return result2;
528 }
529 if (typeof(TypeToConvert) == typeof(short) || typeof(TypeToConvert) == typeof(short?))
530 {
531 short value4;
532 bool result2 = jsonElement.TryGetInt16(out value4);
533 result = (TypeToConvert)(object)value4;
534 return result2;
535 }
536 if (typeof(TypeToConvert) == typeof(decimal) || typeof(TypeToConvert) == typeof(decimal?))
537 {
538 decimal value5;
539 bool result2 = jsonElement.TryGetDecimal(out value5);
540 result = (TypeToConvert)(object)value5;
541 return result2;
542 }
543 if (typeof(TypeToConvert) == typeof(byte) || typeof(TypeToConvert) == typeof(byte?))
544 {
545 byte value6;
546 bool result2 = jsonElement.TryGetByte(out value6);
547 result = (TypeToConvert)(object)value6;
548 return result2;
549 }
550 if (typeof(TypeToConvert) == typeof(float) || typeof(TypeToConvert) == typeof(float?))
551 {
552 float value7;
553 bool result2 = jsonElement.TryGetSingle(out value7);
554 result = (TypeToConvert)(object)value7;
555 return result2;
556 }
557 if (typeof(TypeToConvert) == typeof(uint) || typeof(TypeToConvert) == typeof(uint?))
558 {
559 uint value8;
560 bool result2 = jsonElement.TryGetUInt32(out value8);
561 result = (TypeToConvert)(object)value8;
562 return result2;
563 }
564 if (typeof(TypeToConvert) == typeof(ushort) || typeof(TypeToConvert) == typeof(ushort?))
565 {
566 ushort value9;
567 bool result2 = jsonElement.TryGetUInt16(out value9);
568 result = (TypeToConvert)(object)value9;
569 return result2;
570 }
571 if (typeof(TypeToConvert) == typeof(ulong) || typeof(TypeToConvert) == typeof(ulong?))
572 {
573 ulong value10;
574 bool result2 = jsonElement.TryGetUInt64(out value10);
575 result = (TypeToConvert)(object)value10;
576 return result2;
577 }
578 if (typeof(TypeToConvert) == typeof(sbyte) || typeof(TypeToConvert) == typeof(sbyte?))
579 {
580 sbyte value11;
581 bool result2 = jsonElement.TryGetSByte(out value11);
582 result = (TypeToConvert)(object)value11;
583 return result2;
584 }
585 break;
586 case JsonValueKind.String:
587 if (typeof(TypeToConvert) == typeof(string))
588 {
589 string @string = jsonElement.GetString();
590 result = (TypeToConvert)(object)@string;
591 return true;
592 }
593 if (typeof(TypeToConvert) == typeof(DateTime) || typeof(TypeToConvert) == typeof(DateTime?))
594 {
596 bool result2 = jsonElement.TryGetDateTime(out value12);
597 result = (TypeToConvert)(object)value12;
598 return result2;
599 }
600 if (typeof(TypeToConvert) == typeof(DateTimeOffset) || typeof(TypeToConvert) == typeof(DateTimeOffset?))
601 {
603 bool result2 = jsonElement.TryGetDateTimeOffset(out value13);
604 result = (TypeToConvert)(object)value13;
605 return result2;
606 }
607 if (typeof(TypeToConvert) == typeof(Guid) || typeof(TypeToConvert) == typeof(Guid?))
608 {
610 bool result2 = jsonElement.TryGetGuid(out value14);
611 result = (TypeToConvert)(object)value14;
612 return result2;
613 }
614 if (typeof(TypeToConvert) == typeof(char) || typeof(TypeToConvert) == typeof(char?))
615 {
616 string string2 = jsonElement.GetString();
617 if (string2.Length == 1)
618 {
619 result = (TypeToConvert)(object)string2[0];
620 return true;
621 }
622 }
623 break;
624 case JsonValueKind.True:
625 case JsonValueKind.False:
626 if (typeof(TypeToConvert) == typeof(bool) || typeof(TypeToConvert) == typeof(bool?))
627 {
628 result = (TypeToConvert)(object)jsonElement.GetBoolean();
629 return true;
630 }
631 break;
632 }
633 result = default(TypeToConvert);
634 return false;
635 }
636}
static string NodeUnableToConvertElement
Definition SR.cs:296
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string NodeUnableToConvert
Definition SR.cs:294
static string NodeElementCannotBeObjectOrArray
Definition SR.cs:288
Definition SR.cs:7
DebugView(JsonValue< TValue > node)
Definition JsonValue.cs:362
static ? JsonValue Create(int? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:143
static JsonValue Create(ushort value, JsonNodeOptions? options=null)
Definition JsonValue.cs:206
JsonValue(JsonNodeOptions? options=null)
Definition JsonValue.cs:278
static ? JsonValue Create(DateTime? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:59
static ? JsonValue Create(sbyte? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:173
static JsonValue Create(double value, JsonNodeOptions? options=null)
Definition JsonValue.cs:96
static JsonValue Create(int value, JsonNodeOptions? options=null)
Definition JsonValue.cs:138
static ? JsonValue Create(string? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:196
static ? JsonValue Create(byte? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:31
override void GetPath(List< string > path, JsonNode child)
Definition JsonValue.cs:327
static JsonValue Create(sbyte value, JsonNodeOptions? options=null)
Definition JsonValue.cs:167
static JsonValue Create(uint value, JsonNodeOptions? options=null)
Definition JsonValue.cs:222
static ? JsonValue Create(ulong? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:244
static JsonValue Create(DateTimeOffset value, JsonNodeOptions? options=null)
Definition JsonValue.cs:68
static JsonValue Create(bool value, JsonNodeOptions? options=null)
Definition JsonValue.cs:12
TypeToConvert ConvertJsonElement< TypeToConvert >()
Definition JsonValue.cs:414
static JsonValue Create(char value, JsonNodeOptions? options=null)
Definition JsonValue.cs:40
static ? JsonValue Create(long? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:157
static ? JsonValue Create(JsonElement? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:263
static JsonValue Create(byte value, JsonNodeOptions? options=null)
Definition JsonValue.cs:26
static ? JsonValue Create(decimal? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:87
const string CreateUnreferencedCodeMessage
Definition JsonValue.cs:10
static ? JsonValue Create(char? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:45
static JsonValue Create(float value, JsonNodeOptions? options=null)
Definition JsonValue.cs:182
static JsonValue Create(decimal value, JsonNodeOptions? options=null)
Definition JsonValue.cs:82
static JsonValue Create(DateTime value, JsonNodeOptions? options=null)
Definition JsonValue.cs:54
static JsonValue Create(Guid value, JsonNodeOptions? options=null)
Definition JsonValue.cs:110
static ? JsonValue Create(float? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:187
static ? JsonValue Create(JsonElement value, JsonNodeOptions? options=null)
Definition JsonValue.cs:253
bool TryConvertJsonElement< TypeToConvert >([NotNullWhen(true)] out TypeToConvert result)
Definition JsonValue.cs:502
static ? JsonValue Create(short? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:129
JsonValue(TValue value, JsonNodeOptions? options=null)
Definition JsonValue.cs:372
static ? JsonValue Create< T >(T? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:284
static JsonValue Create(long value, JsonNodeOptions? options=null)
Definition JsonValue.cs:152
static ? JsonValue Create(uint? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:228
bool TryGetValue< T >([NotNullWhen(true)] out T? value)
static ? JsonValue Create(Guid? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:115
static void VerifyJsonElementIsNotArrayOrObject(ref JsonElement element)
Definition JsonValue.cs:337
static JsonValue Create(ulong value, JsonNodeOptions? options=null)
Definition JsonValue.cs:238
static ? JsonValue Create(DateTimeOffset? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:73
static ? JsonValue Create(bool? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:17
static JsonValue Create(short value, JsonNodeOptions? options=null)
Definition JsonValue.cs:124
static ? JsonValue Create(double? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:101
static ? JsonValue Create(ushort? value, JsonNodeOptions? options=null)
Definition JsonValue.cs:212
static void ThrowArgumentException_NodeValueNotAllowed(string paramName)