Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonElement.cs
Go to the documentation of this file.
5using System.IO;
6
7namespace System.Text.Json;
8
9[DebuggerDisplay("{DebuggerDisplay,nq}")]
10public readonly struct JsonElement
11{
12 [DebuggerDisplay("{Current,nq}")]
13 public struct ArrayEnumerator : IEnumerable<JsonElement>, IEnumerable, IEnumerator<JsonElement>, IEnumerator, IDisposable
14 {
15 private readonly JsonElement _target;
16
17 private int _curIdx;
18
19 private readonly int _endIdxOrVersion;
20
22 {
23 get
24 {
25 if (_curIdx < 0)
26 {
27 return default(JsonElement);
28 }
30 }
31 }
32
34
36 {
37 _target = target;
38 _curIdx = -1;
40 }
41
43 {
44 ArrayEnumerator result = this;
45 result._curIdx = -1;
46 return result;
47 }
48
53
58
59 public void Dispose()
60 {
62 }
63
64 public void Reset()
65 {
66 _curIdx = -1;
67 }
68
69 public bool MoveNext()
70 {
72 {
73 return false;
74 }
75 if (_curIdx < 0)
76 {
77 _curIdx = _target._idx + 12;
78 }
79 else
80 {
82 }
84 }
85 }
86
87 [DebuggerDisplay("{Current,nq}")]
88 public struct ObjectEnumerator : IEnumerable<JsonProperty>, IEnumerable, IEnumerator<JsonProperty>, IEnumerator, IDisposable
89 {
90 private readonly JsonElement _target;
91
92 private int _curIdx;
93
94 private readonly int _endIdxOrVersion;
95
97 {
98 get
99 {
100 if (_curIdx < 0)
101 {
102 return default(JsonProperty);
103 }
105 }
106 }
107
109
111 {
112 _target = target;
113 _curIdx = -1;
115 }
116
118 {
119 ObjectEnumerator result = this;
120 result._curIdx = -1;
121 return result;
122 }
123
128
133
134 public void Dispose()
135 {
137 }
138
139 public void Reset()
140 {
141 _curIdx = -1;
142 }
143
144 public bool MoveNext()
145 {
147 {
148 return false;
149 }
150 if (_curIdx < 0)
151 {
152 _curIdx = _target._idx + 12;
153 }
154 else
155 {
157 }
158 _curIdx += 12;
159 return _curIdx < _endIdxOrVersion;
160 }
161 }
162
163 private readonly JsonDocument _parent;
164
165 private readonly int _idx;
166
169
170 public JsonValueKind ValueKind => TokenType.ToValueKind();
171
172 public JsonElement this[int index]
173 {
174 get
175 {
178 }
179 }
180
182 private string DebuggerDisplay => $"ValueKind = {ValueKind} : \"{ToString()}\"";
183
184 internal JsonElement(JsonDocument parent, int idx)
185 {
186 _parent = parent;
187 _idx = idx;
188 }
189
190 public int GetArrayLength()
191 {
194 }
195
197 {
198 if (propertyName == null)
199 {
200 throw new ArgumentNullException("propertyName");
201 }
203 {
204 return value;
205 }
206 throw new KeyNotFoundException();
207 }
208
210 {
212 {
213 return value;
214 }
215 throw new KeyNotFoundException();
216 }
217
219 {
221 {
222 return value;
223 }
224 throw new KeyNotFoundException();
225 }
226
228 {
229 if (propertyName == null)
230 {
231 throw new ArgumentNullException("propertyName");
232 }
233 return TryGetProperty(propertyName.AsSpan(), out value);
234 }
235
241
247
248 public bool GetBoolean()
249 {
250 JsonTokenType tokenType = TokenType;
251 return tokenType switch
252 {
253 JsonTokenType.False => false,
254 JsonTokenType.True => true,
255 _ => throw ThrowHelper.GetJsonElementWrongTypeException("Boolean", tokenType),
256 };
257 }
258
259 public string? GetString()
260 {
262 return _parent.GetString(_idx, JsonTokenType.String);
263 }
264
265 public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value)
266 {
269 }
270
271 public byte[] GetBytesFromBase64()
272 {
273 if (TryGetBytesFromBase64(out byte[] value))
274 {
275 return value;
276 }
278 }
279
280 [CLSCompliant(false)]
281 public bool TryGetSByte(out sbyte value)
282 {
285 }
286
287 [CLSCompliant(false)]
288 public sbyte GetSByte()
289 {
291 {
292 return value;
293 }
294 throw new FormatException();
295 }
296
297 public bool TryGetByte(out byte value)
298 {
301 }
302
303 public byte GetByte()
304 {
305 if (TryGetByte(out var value))
306 {
307 return value;
308 }
309 throw new FormatException();
310 }
311
312 public bool TryGetInt16(out short value)
313 {
316 }
317
318 public short GetInt16()
319 {
321 {
322 return value;
323 }
324 throw new FormatException();
325 }
326
327 [CLSCompliant(false)]
328 public bool TryGetUInt16(out ushort value)
329 {
332 }
333
334 [CLSCompliant(false)]
335 public ushort GetUInt16()
336 {
338 {
339 return value;
340 }
341 throw new FormatException();
342 }
343
344 public bool TryGetInt32(out int value)
345 {
348 }
349
350 public int GetInt32()
351 {
353 {
354 return value;
355 }
357 }
358
359 [CLSCompliant(false)]
360 public bool TryGetUInt32(out uint value)
361 {
364 }
365
366 [CLSCompliant(false)]
367 public uint GetUInt32()
368 {
370 {
371 return value;
372 }
374 }
375
376 public bool TryGetInt64(out long value)
377 {
380 }
381
382 public long GetInt64()
383 {
385 {
386 return value;
387 }
389 }
390
391 [CLSCompliant(false)]
392 public bool TryGetUInt64(out ulong value)
393 {
396 }
397
398 [CLSCompliant(false)]
399 public ulong GetUInt64()
400 {
402 {
403 return value;
404 }
406 }
407
408 public bool TryGetDouble(out double value)
409 {
412 }
413
414 public double GetDouble()
415 {
417 {
418 return value;
419 }
421 }
422
423 public bool TryGetSingle(out float value)
424 {
427 }
428
429 public float GetSingle()
430 {
432 {
433 return value;
434 }
436 }
437
438 public bool TryGetDecimal(out decimal value)
439 {
442 }
443
444 public decimal GetDecimal()
445 {
447 {
448 return value;
449 }
451 }
452
454 {
457 }
458
460 {
462 {
463 return value;
464 }
466 }
467
473
475 {
477 {
478 return value;
479 }
481 }
482
484 {
487 }
488
489 public Guid GetGuid()
490 {
491 if (TryGetGuid(out var value))
492 {
493 return value;
494 }
496 }
497
498 internal string GetPropertyName()
499 {
502 }
503
504 public string GetRawText()
505 {
508 }
509
515
516 internal string GetPropertyRawText()
517 {
520 }
521
522 public bool ValueEquals(string? text)
523 {
524 if (TokenType == JsonTokenType.Null)
525 {
526 return text == null;
527 }
528 return TextEqualsHelper(text.AsSpan(), isPropertyName: false);
529 }
530
532 {
533 if (TokenType == JsonTokenType.Null)
534 {
535 return utf8Text == default(ReadOnlySpan<byte>);
536 }
538 }
539
541 {
542 if (TokenType == JsonTokenType.Null)
543 {
544 return text == default(ReadOnlySpan<char>);
545 }
546 return TextEqualsHelper(text, isPropertyName: false);
547 }
548
554
560
562 {
563 if (writer == null)
564 {
565 throw new ArgumentNullException("writer");
566 }
569 }
570
572 {
574 JsonTokenType tokenType = TokenType;
575 if (tokenType != JsonTokenType.StartArray)
576 {
578 }
579 return new ArrayEnumerator(this);
580 }
581
583 {
585 JsonTokenType tokenType = TokenType;
586 if (tokenType != JsonTokenType.StartObject)
587 {
588 throw ThrowHelper.GetJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType);
589 }
590 return new ObjectEnumerator(this);
591 }
592
593 public override string ToString()
594 {
595 switch (TokenType)
596 {
597 case JsonTokenType.None:
598 case JsonTokenType.Null:
599 return string.Empty;
600 case JsonTokenType.True:
601 return bool.TrueString;
602 case JsonTokenType.False:
603 return bool.FalseString;
604 case JsonTokenType.StartObject:
605 case JsonTokenType.StartArray:
606 case JsonTokenType.Number:
608 case JsonTokenType.String:
609 return GetString();
610 default:
611 return string.Empty;
612 }
613 }
614
616 {
619 {
620 return this;
621 }
622 return _parent.CloneElement(_idx);
623 }
624
625 private void CheckValidInstance()
626 {
627 if (_parent == null)
628 {
629 throw new InvalidOperationException();
630 }
631 }
632
634 {
636 bool flag = JsonDocument.TryParseValue(ref reader, out document, shouldThrow: true, useArrayPools: false);
637 return document.RootElement;
638 }
639
645
651
653 {
655 return jsonDocument.RootElement;
656 }
657
658 public static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] out JsonElement? element)
659 {
661 bool result = JsonDocument.TryParseValue(ref reader, out document, shouldThrow: false, useArrayPools: false);
662 element = document?.RootElement;
663 return result;
664 }
665}
string GetRawValueAsString(int index)
JsonElement GetArrayIndexElement(int currentIndex, int arrayIndex)
int GetEndIndex(int index, bool includeEndElement)
string GetNameOfPropertyValue(int index)
bool TryGetNamedPropertyValue(int index, ReadOnlySpan< char > propertyName, out JsonElement value)
bool TryGetValue(int index, [NotNullWhen(true)] out byte[] value)
static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] out JsonDocument? document)
void WriteElementTo(int index, Utf8JsonWriter writer)
bool TextEquals(int index, ReadOnlySpan< char > otherText, bool isPropertyName)
string GetPropertyRawValueAsString(int valueIndex)
JsonElement CloneElement(int index)
ReadOnlyMemory< byte > GetRawValue(int index, bool includeQuotes)
static JsonDocument ParseValue(Stream utf8Json, JsonDocumentOptions options)
string GetString(int index, JsonTokenType expectedType)
JsonTokenType GetJsonTokenType(int index)
static FormatException GetFormatException()
static InvalidOperationException GetJsonElementWrongTypeException(JsonTokenType expectedType, JsonTokenType actualType)
new IEnumerator< T > GetEnumerator()
TokenType
Definition TokenType.cs:4
bool ValueEquals(ReadOnlySpan< byte > utf8Text)
JsonElement GetProperty(ReadOnlySpan< char > propertyName)
bool TryGetDateTimeOffset(out DateTimeOffset value)
bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value)
bool TryGetGuid(out Guid value)
bool TextEqualsHelper(ReadOnlySpan< char > text, bool isPropertyName)
bool TryGetUInt32(out uint value)
void WriteTo(Utf8JsonWriter writer)
bool TryGetInt32(out int value)
static JsonElement ParseValue(ref Utf8JsonReader reader)
bool ValueEquals(string? text)
bool TryGetInt64(out long value)
DateTimeOffset GetDateTimeOffset()
bool TryGetDouble(out double value)
JsonElement GetProperty(string propertyName)
bool TryGetProperty(ReadOnlySpan< char > propertyName, out JsonElement value)
JsonElement GetProperty(ReadOnlySpan< byte > utf8PropertyName)
override string ToString()
bool TryGetInt16(out short value)
static bool TryParseValue(ref Utf8JsonReader reader, [NotNullWhen(true)] out JsonElement? element)
bool TryGetUInt64(out ulong value)
bool TryGetProperty(string propertyName, out JsonElement value)
bool ValueEquals(ReadOnlySpan< char > text)
ArrayEnumerator EnumerateArray()
bool TryGetSByte(out sbyte value)
static JsonElement ParseValue(string json, JsonDocumentOptions options)
readonly JsonDocument _parent
bool TryGetUInt16(out ushort value)
ObjectEnumerator EnumerateObject()
bool TextEqualsHelper(ReadOnlySpan< byte > utf8Text, bool isPropertyName, bool shouldUnescape)
JsonElement(JsonDocument parent, int idx)
bool TryGetDateTime(out DateTime value)
bool TryGetSingle(out float value)
static JsonElement ParseValue(Stream utf8Json, JsonDocumentOptions options)
static JsonElement ParseValue(ReadOnlySpan< byte > utf8Json, JsonDocumentOptions options)
ReadOnlyMemory< byte > GetRawValue()
bool TryGetDecimal(out decimal value)
bool TryGetByte(out byte value)
bool TryGetProperty(ReadOnlySpan< byte > utf8PropertyName, out JsonElement value)