Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonSerializer.cs
Go to the documentation of this file.
4using System.IO;
12
13namespace System.Text.Json;
14
15public static class JsonSerializer
16{
17 internal static readonly byte[] s_idPropertyName = new byte[3] { 36, 105, 100 };
18
19 internal static readonly byte[] s_refPropertyName = new byte[4] { 36, 114, 101, 102 };
20
21 internal static readonly byte[] s_valuesPropertyName = new byte[7] { 36, 118, 97, 108, 117, 101, 115 };
22
23 internal static readonly JsonEncodedText s_metadataId = JsonEncodedText.Encode("$id");
24
25 internal static readonly JsonEncodedText s_metadataRef = JsonEncodedText.Encode("$ref");
26
27 internal static readonly JsonEncodedText s_metadataValues = JsonEncodedText.Encode("$values");
28
29 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
31 {
32 if (document == null)
33 {
34 throw new ArgumentNullException("document");
35 }
36 JsonTypeInfo typeInfo = GetTypeInfo(options, typeof(TValue));
37 return ReadDocument<TValue>(document, typeInfo);
38 }
39
40 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
42 {
43 if (document == null)
44 {
45 throw new ArgumentNullException("document");
46 }
47 if (returnType == null)
48 {
49 throw new ArgumentNullException("returnType");
50 }
52 return ReadDocument<object>(document, typeInfo);
53 }
54
56 {
57 if (document == null)
58 {
59 throw new ArgumentNullException("document");
60 }
61 if (jsonTypeInfo == null)
62 {
63 throw new ArgumentNullException("jsonTypeInfo");
64 }
66 }
67
69 {
70 if (document == null)
71 {
72 throw new ArgumentNullException("document");
73 }
74 if (returnType == null)
75 {
76 throw new ArgumentNullException("returnType");
77 }
78 if (context == null)
79 {
80 throw new ArgumentNullException("context");
81 }
82 JsonTypeInfo typeInfo = GetTypeInfo(context, returnType);
83 return ReadDocument<object>(document, typeInfo);
84 }
85
91
92 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
93 public static TValue? Deserialize<TValue>(this JsonElement element, JsonSerializerOptions? options = null)
94 {
95 JsonTypeInfo typeInfo = GetTypeInfo(options, typeof(TValue));
96 return ReadUsingMetadata<TValue>(element, typeInfo);
97 }
98
99 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
100 public static object? Deserialize(this JsonElement element, Type returnType, JsonSerializerOptions? options = null)
101 {
102 if (returnType == null)
103 {
104 throw new ArgumentNullException("returnType");
105 }
107 return ReadUsingMetadata<object>(element, typeInfo);
108 }
109
111 {
112 if (jsonTypeInfo == null)
113 {
114 throw new ArgumentNullException("jsonTypeInfo");
115 }
117 }
118
119 public static object? Deserialize(this JsonElement element, Type returnType, JsonSerializerContext context)
120 {
121 if (returnType == null)
122 {
123 throw new ArgumentNullException("returnType");
124 }
125 if (context == null)
126 {
127 throw new ArgumentNullException("context");
128 }
129 JsonTypeInfo typeInfo = GetTypeInfo(context, returnType);
130 return ReadUsingMetadata<object>(element, typeInfo);
131 }
132
138
139 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
140 public static TValue? Deserialize<TValue>(this JsonNode? node, JsonSerializerOptions? options = null)
141 {
142 JsonTypeInfo typeInfo = GetTypeInfo(options, typeof(TValue));
143 return ReadNode<TValue>(node, typeInfo);
144 }
145
146 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
147 public static object? Deserialize(this JsonNode? node, Type returnType, JsonSerializerOptions? options = null)
148 {
149 if (returnType == null)
150 {
151 throw new ArgumentNullException("returnType");
152 }
154 return ReadNode<object>(node, typeInfo);
155 }
156
158 {
159 if (jsonTypeInfo == null)
160 {
161 throw new ArgumentNullException("jsonTypeInfo");
162 }
164 }
165
166 public static object? Deserialize(this JsonNode? node, Type returnType, JsonSerializerContext context)
167 {
168 if (returnType == null)
169 {
170 throw new ArgumentNullException("returnType");
171 }
172 if (context == null)
173 {
174 throw new ArgumentNullException("context");
175 }
176 JsonTypeInfo typeInfo = GetTypeInfo(context, returnType);
177 return ReadNode<object>(node, typeInfo);
178 }
179
181 {
185 {
186 if (node == null)
187 {
188 utf8JsonWriter.WriteNullValue();
189 }
190 else
191 {
192 node.WriteTo(utf8JsonWriter, options);
193 }
194 }
195 return ReadFromSpan<TValue>(pooledByteBufferWriter.WrittenMemory.Span, jsonTypeInfo);
196 }
197
198 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
200 {
201 Type runtimeType = GetRuntimeType(in value);
203 return WriteDocumentUsingSerializer(in value, typeInfo);
204 }
205
206 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
213
215 {
216 if (jsonTypeInfo == null)
217 {
218 throw new ArgumentNullException("jsonTypeInfo");
219 }
221 }
222
232
243
254
255 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
257 {
258 Type runtimeType = GetRuntimeType(in value);
260 return WriteElementUsingSerializer(in value, typeInfo);
261 }
262
263 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
270
272 {
273 if (jsonTypeInfo == null)
274 {
275 throw new ArgumentNullException("jsonTypeInfo");
276 }
278 }
279
281 {
282 if (context == null)
283 {
284 throw new ArgumentNullException("context");
285 }
289 }
290
301
312
313 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
315 {
316 Type runtimeType = GetRuntimeType(in value);
318 return WriteNodeUsingSerializer(in value, typeInfo);
319 }
320
321 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
328
330 {
331 if (jsonTypeInfo == null)
332 {
333 throw new ArgumentNullException("jsonTypeInfo");
334 }
336 }
337
339 {
340 if (context == null)
341 {
342 throw new ArgumentNullException("context");
343 }
347 }
348
359
370
371 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
373 {
374 if (options == null)
375 {
377 }
378 if (!options.IsInitializedForReflectionSerializer)
379 {
380 options.InitializeForReflectionSerializer();
381 }
382 return options.GetOrAddClassForRootType(runtimeType);
383 }
384
386 {
387 JsonTypeInfo typeInfo = context.GetTypeInfo(type);
388 if (typeInfo == null)
389 {
391 }
392 return typeInfo;
393 }
394
396 {
397 return JsonHelpers.IsInRangeInclusive((int)handling, 0, 7);
398 }
399
401 {
402 JsonConverter converterBase = state.Current.JsonTypeInfo.PropertyInfoForTypeInfo.ConverterBase;
403 if ((int)state.Current.ObjectState < 2 && !TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadNameOrEndObject))
404 {
405 return false;
406 }
407 if (state.Current.ObjectState == StackFrameObjectState.ReadNameOrEndObject)
408 {
409 if (reader.TokenType != JsonTokenType.PropertyName)
410 {
413 return true;
414 }
415 ReadOnlySpan<byte> span = reader.GetSpan();
417 {
418 case MetadataPropertyName.Id:
420 if (!converterBase.CanHaveIdMetadata)
421 {
423 }
425 break;
426 case MetadataPropertyName.Ref:
428 if (converterBase.IsValueType)
429 {
431 }
433 break;
434 case MetadataPropertyName.Values:
436 break;
437 default:
440 return true;
441 }
442 }
443 if (state.Current.ObjectState == StackFrameObjectState.ReadAheadRefValue)
444 {
446 {
447 return false;
448 }
449 }
450 else if (state.Current.ObjectState == StackFrameObjectState.ReadAheadIdValue && !TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadIdValue))
451 {
452 return false;
453 }
454 if (state.Current.ObjectState == StackFrameObjectState.ReadRefValue)
455 {
456 if (reader.TokenType != JsonTokenType.String)
457 {
459 }
460 string @string = reader.GetString();
461 object obj = state.ReferenceResolver.ResolveReference(@string);
464 state.Current.ObjectState = StackFrameObjectState.ReadAheadRefEndObject;
465 }
466 else if (state.Current.ObjectState == StackFrameObjectState.ReadIdValue)
467 {
468 if (reader.TokenType != JsonTokenType.String)
469 {
471 }
472 converterBase.CreateInstanceForReferenceResolver(ref reader, ref state, options);
473 string string2 = reader.GetString();
474 state.ReferenceResolver.AddReference(string2, state.Current.ReturnValue);
476 }
478 if (state.Current.ObjectState == StackFrameObjectState.ReadAheadRefEndObject && !TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadRefEndObject))
479 {
480 return false;
481 }
482 if (state.Current.ObjectState == StackFrameObjectState.ReadRefEndObject && reader.TokenType != JsonTokenType.EndObject)
483 {
485 }
486 return true;
487 }
488
490 {
491 JsonConverter converterBase = state.Current.JsonTypeInfo.PropertyInfoForTypeInfo.ConverterBase;
492 if ((int)state.Current.ObjectState < 2 && !TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadNameOrEndObject))
493 {
494 return false;
495 }
496 if (state.Current.ObjectState == StackFrameObjectState.ReadNameOrEndObject)
497 {
498 if (reader.TokenType != JsonTokenType.PropertyName)
499 {
501 }
502 ReadOnlySpan<byte> span = reader.GetSpan();
504 {
505 case MetadataPropertyName.Id:
507 if (!converterBase.CanHaveIdMetadata)
508 {
510 }
512 break;
513 case MetadataPropertyName.Ref:
515 if (converterBase.IsValueType)
516 {
518 }
520 break;
521 case MetadataPropertyName.Values:
523 break;
524 default:
526 break;
527 }
528 }
529 if (state.Current.ObjectState == StackFrameObjectState.ReadAheadRefValue)
530 {
532 {
533 return false;
534 }
535 }
536 else if (state.Current.ObjectState == StackFrameObjectState.ReadAheadIdValue && !TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadIdValue))
537 {
538 return false;
539 }
540 if (state.Current.ObjectState == StackFrameObjectState.ReadRefValue)
541 {
542 if (reader.TokenType != JsonTokenType.String)
543 {
545 }
546 string @string = reader.GetString();
547 object obj = state.ReferenceResolver.ResolveReference(@string);
550 state.Current.ObjectState = StackFrameObjectState.ReadAheadRefEndObject;
551 }
552 else if (state.Current.ObjectState == StackFrameObjectState.ReadIdValue)
553 {
554 if (reader.TokenType != JsonTokenType.String)
555 {
557 }
558 converterBase.CreateInstanceForReferenceResolver(ref reader, ref state, options);
559 string string2 = reader.GetString();
560 state.ReferenceResolver.AddReference(string2, state.Current.ReturnValue);
562 }
564 if (state.Current.ObjectState == StackFrameObjectState.ReadAheadRefEndObject && !TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadRefEndObject))
565 {
566 return false;
567 }
568 if (state.Current.ObjectState == StackFrameObjectState.ReadRefEndObject)
569 {
570 if (reader.TokenType != JsonTokenType.EndObject)
571 {
573 }
574 return true;
575 }
576 if (state.Current.ObjectState == StackFrameObjectState.ReadAheadValuesName && !TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadValuesName))
577 {
578 return false;
579 }
580 if (state.Current.ObjectState == StackFrameObjectState.ReadValuesName)
581 {
582 if (reader.TokenType != JsonTokenType.PropertyName)
583 {
585 }
586 ReadOnlySpan<byte> span2 = reader.GetSpan();
588 {
590 }
592 state.Current.ObjectState = StackFrameObjectState.ReadAheadValuesStartArray;
593 }
594 if (state.Current.ObjectState == StackFrameObjectState.ReadAheadValuesStartArray && !TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadValuesStartArray))
595 {
596 return false;
597 }
598 if (state.Current.ObjectState == StackFrameObjectState.ReadValuesStartArray)
599 {
600 if (reader.TokenType != JsonTokenType.StartArray)
601 {
603 }
606 }
607 return true;
608 }
609
610 [MethodImpl(MethodImplOptions.AggressiveInlining)]
616
618 {
619 if (propertyName.Length > 0 && propertyName[0] == 36)
620 {
621 switch (propertyName.Length)
622 {
623 case 3:
624 if (propertyName[1] == 105 && propertyName[2] == 100)
625 {
626 return MetadataPropertyName.Id;
627 }
628 break;
629 case 4:
630 if (propertyName[1] == 114 && propertyName[2] == 101 && propertyName[3] == 102)
631 {
632 return MetadataPropertyName.Ref;
633 }
634 break;
635 case 7:
636 if (propertyName[1] == 118 && propertyName[2] == 97 && propertyName[3] == 108 && propertyName[4] == 117 && propertyName[5] == 101 && propertyName[6] == 115)
637 {
638 return MetadataPropertyName.Values;
639 }
640 break;
641 }
642 }
643 return MetadataPropertyName.NoMetadata;
644 }
645
647 {
648 bool flag = false;
649 referenceValue = null;
650 if (element.ValueKind == JsonValueKind.Object)
651 {
652 int num = 0;
653 foreach (JsonProperty item in element.EnumerateObject())
654 {
655 num++;
656 if (flag)
657 {
659 }
660 else if (item.EscapedNameEquals(s_refPropertyName))
661 {
662 if (num > 1)
663 {
665 }
666 if (item.Value.ValueKind != JsonValueKind.String)
667 {
669 }
670 referenceValue = state.ReferenceResolver.ResolveReference(item.Value.GetString());
671 flag = true;
672 }
673 }
674 }
675 return flag;
676 }
677
678 private static void ValidateValueIsCorrectType<T>(object value, string referenceId)
679 {
680 try
681 {
682 T val = (T)value;
683 }
685 {
687 throw;
688 }
689 }
690
716
717 [MethodImpl(MethodImplOptions.AggressiveInlining)]
719 {
720 ReadOnlySpan<byte> span = reader.GetSpan();
721 ReadOnlySpan<byte> result;
722 if (reader._stringHasEscaping)
723 {
724 int idx = span.IndexOf<byte>(92);
726 }
727 else
728 {
729 result = span;
730 }
731 if (options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.Preserve && span.Length > 0 && span[0] == 36)
732 {
734 }
735 return result;
736 }
737
739 {
740 object obj2 = jsonPropertyInfo.GetValueAsObject(obj);
741 if (obj2 != null)
742 {
743 return;
744 }
745 if (jsonPropertyInfo.RuntimeTypeInfo.CreateObject == null)
746 {
747 if (jsonPropertyInfo.DeclaredPropertyType.FullName == "System.Text.Json.Nodes.JsonObject")
748 {
749 obj2 = jsonPropertyInfo.ConverterBase.CreateObject(options);
750 }
751 else
752 {
754 }
755 }
756 else
757 {
758 obj2 = jsonPropertyInfo.RuntimeTypeInfo.CreateObject();
759 }
760 jsonPropertyInfo.SetExtensionDictionaryAsObject(obj, obj2);
761 }
762
764 {
766 {
767 return jsonConverter2.ReadCore(ref reader, options, ref state);
768 }
769 object obj = jsonConverter.ReadCoreAsObject(ref reader, options, ref state);
770 return (TValue)obj;
771 }
772
774 {
776 JsonReaderState state = new JsonReaderState(options.GetReaderOptions());
778 ReadStack state2 = default(ReadStack);
780 JsonConverter converterBase = jsonTypeInfo.PropertyInfoForTypeInfo.ConverterBase;
782 {
783 return jsonConverter.ReadCore(ref reader, options, ref state2);
784 }
785 object obj = converterBase.ReadCoreAsObject(ref reader, options, ref state2);
786 return (TValue)obj;
787 }
788
789 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
791 {
792 JsonTypeInfo typeInfo = GetTypeInfo(options, typeof(TValue));
793 return ReadFromSpan<TValue>(utf8Json, typeInfo);
794 }
795
796 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
798 {
799 if (returnType == null)
800 {
801 throw new ArgumentNullException("returnType");
802 }
804 return ReadFromSpan<object>(utf8Json, typeInfo);
805 }
806
808 {
809 if (jsonTypeInfo == null)
810 {
811 throw new ArgumentNullException("jsonTypeInfo");
812 }
814 }
815
817 {
818 if (returnType == null)
819 {
820 throw new ArgumentNullException("returnType");
821 }
822 if (context == null)
823 {
824 throw new ArgumentNullException("context");
825 }
827 }
828
829 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
831 {
832 if (utf8Json == null)
833 {
834 throw new ArgumentNullException("utf8Json");
835 }
836 JsonTypeInfo typeInfo = GetTypeInfo(options, typeof(TValue));
838 }
839
840 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
842 {
843 if (utf8Json == null)
844 {
845 throw new ArgumentNullException("utf8Json");
846 }
848 }
849
850 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
852 {
853 if (utf8Json == null)
854 {
855 throw new ArgumentNullException("utf8Json");
856 }
857 if (returnType == null)
858 {
859 throw new ArgumentNullException("returnType");
860 }
863 }
864
865 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
867 {
868 if (utf8Json == null)
869 {
870 throw new ArgumentNullException("utf8Json");
871 }
872 if (returnType == null)
873 {
874 throw new ArgumentNullException("returnType");
875 }
877 }
878
880 {
881 if (utf8Json == null)
882 {
883 throw new ArgumentNullException("utf8Json");
884 }
885 if (jsonTypeInfo == null)
886 {
887 throw new ArgumentNullException("jsonTypeInfo");
888 }
890 }
891
893 {
894 if (utf8Json == null)
895 {
896 throw new ArgumentNullException("utf8Json");
897 }
898 if (jsonTypeInfo == null)
899 {
900 throw new ArgumentNullException("jsonTypeInfo");
901 }
903 }
904
906 {
907 if (utf8Json == null)
908 {
909 throw new ArgumentNullException("utf8Json");
910 }
911 if (returnType == null)
912 {
913 throw new ArgumentNullException("returnType");
914 }
915 if (context == null)
916 {
917 throw new ArgumentNullException("context");
918 }
920 }
921
923 {
924 if (utf8Json == null)
925 {
926 throw new ArgumentNullException("utf8Json");
927 }
928 if (returnType == null)
929 {
930 throw new ArgumentNullException("returnType");
931 }
932 if (context == null)
933 {
934 throw new ArgumentNullException("context");
935 }
937 }
938
939 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
941 {
942 if (utf8Json == null)
943 {
944 throw new ArgumentNullException("utf8Json");
945 }
946 if (options == null)
947 {
949 }
950 if (!options.IsInitializedForReflectionSerializer)
951 {
952 options.InitializeForReflectionSerializer();
953 }
955 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
957 {
958 ReadBufferState bufferState = new ReadBufferState(options.DefaultBufferSize);
961 ReadStack readStack = default(ReadStack);
963 JsonReaderState jsonReaderState = new JsonReaderState(options.GetReaderOptions());
964 try
965 {
966 do
967 {
970 object returnValue = readStack.Current.ReturnValue;
972 {
973 while (queue.Count > 0)
974 {
975 yield return queue.Dequeue();
976 }
977 }
978 }
979 while (!bufferState.IsFinalBlock);
980 }
981 finally
982 {
983 bufferState.Dispose();
984 }
985 }
986 }
987
988 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Workaround for https://github.com/mono/linker/issues/1416. All usages are marked as unsafe.")]
993
995 {
997 ReadBufferState bufferState = new ReadBufferState(options.DefaultBufferSize);
998 ReadStack readStack = default(ReadStack);
1000 JsonConverter converter = readStack.Current.JsonPropertyInfo.ConverterBase;
1001 JsonReaderState jsonReaderState = new JsonReaderState(options.GetReaderOptions());
1002 try
1003 {
1004 TValue result;
1005 do
1006 {
1009 }
1010 while (!bufferState.IsFinalBlock);
1011 return result;
1012 }
1013 finally
1014 {
1015 bufferState.Dispose();
1016 }
1017 }
1018
1020 {
1022 ReadBufferState bufferState = new ReadBufferState(options.DefaultBufferSize);
1023 ReadStack readStack = default(ReadStack);
1025 JsonConverter converterBase = readStack.Current.JsonPropertyInfo.ConverterBase;
1026 JsonReaderState jsonReaderState = new JsonReaderState(options.GetReaderOptions());
1027 try
1028 {
1029 TValue result;
1030 do
1031 {
1034 }
1035 while (!bufferState.IsFinalBlock);
1036 return result;
1037 }
1038 finally
1039 {
1040 bufferState.Dispose();
1041 }
1042 }
1043
1045 {
1046 do
1047 {
1048 int num = await utf8Json.ReadAsync(bufferState.Buffer.AsMemory(bufferState.BytesInBuffer), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
1049 if (num == 0)
1050 {
1052 break;
1053 }
1055 }
1056 while (bufferState.BytesInBuffer != bufferState.Buffer.Length);
1057 return bufferState;
1058 }
1059
1061 {
1062 do
1063 {
1064 int num = utf8Json.Read(bufferState.Buffer.AsSpan(bufferState.BytesInBuffer));
1065 if (num == 0)
1066 {
1068 break;
1069 }
1071 }
1072 while (bufferState.BytesInBuffer != bufferState.Buffer.Length);
1073 return bufferState;
1074 }
1075
1077 {
1078 if (bufferState.BytesInBuffer > bufferState.ClearMax)
1079 {
1080 bufferState.ClearMax = bufferState.BytesInBuffer;
1081 }
1082 int num = 0;
1083 if (bufferState.IsFirstIteration)
1084 {
1086 if (bufferState.Buffer.AsSpan().StartsWith(JsonConstants.Utf8Bom))
1087 {
1088 num += JsonConstants.Utf8Bom.Length;
1090 }
1091 }
1092 TValue result = ReadCore<TValue>(ref jsonReaderState, bufferState.IsFinalBlock, new ReadOnlySpan<byte>(bufferState.Buffer, num, bufferState.BytesInBuffer), options, ref readStack, converter);
1093 int num2 = checked((int)readStack.BytesConsumed);
1095 if (!bufferState.IsFinalBlock)
1096 {
1097 if ((uint)bufferState.BytesInBuffer > (uint)bufferState.Buffer.Length / 2u)
1098 {
1099 byte[] buffer = bufferState.Buffer;
1100 int clearMax = bufferState.ClearMax;
1101 byte[] array = ArrayPool<byte>.Shared.Rent((bufferState.Buffer.Length < 1073741823) ? (bufferState.Buffer.Length * 2) : int.MaxValue);
1102 Buffer.BlockCopy(buffer, num2 + num, array, 0, bufferState.BytesInBuffer);
1104 bufferState.ClearMax = bufferState.BytesInBuffer;
1105 new Span<byte>(buffer, 0, clearMax).Clear();
1106 ArrayPool<byte>.Shared.Return(buffer);
1107 }
1108 else if (bufferState.BytesInBuffer != 0)
1109 {
1110 Buffer.BlockCopy(bufferState.Buffer, num2 + num, bufferState.Buffer, 0, bufferState.BytesInBuffer);
1111 }
1112 }
1113 return result;
1114 }
1115
1116 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1122
1132
1133 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1134 public static TValue? Deserialize<TValue>(string json, JsonSerializerOptions? options = null)
1135 {
1136 if (json == null)
1137 {
1138 throw new ArgumentNullException("json");
1139 }
1140 JsonTypeInfo typeInfo = GetTypeInfo(options, typeof(TValue));
1141 return ReadFromSpan<TValue>(json.AsSpan(), typeInfo);
1142 }
1143
1144 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1146 {
1147 JsonTypeInfo typeInfo = GetTypeInfo(options, typeof(TValue));
1148 return ReadFromSpan<TValue>(json, typeInfo);
1149 }
1150
1151 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1152 public static object? Deserialize(string json, Type returnType, JsonSerializerOptions? options = null)
1153 {
1154 if (json == null)
1155 {
1156 throw new ArgumentNullException("json");
1157 }
1158 if (returnType == null)
1159 {
1160 throw new ArgumentNullException("returnType");
1161 }
1163 return ReadFromSpan<object>(json.AsSpan(), typeInfo);
1164 }
1165
1166 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1168 {
1169 if (returnType == null)
1170 {
1171 throw new ArgumentNullException("returnType");
1172 }
1173 if (returnType == null)
1174 {
1175 throw new ArgumentNullException("returnType");
1176 }
1178 return ReadFromSpan<object>(json, typeInfo);
1179 }
1180
1182 {
1183 if (json == null)
1184 {
1185 throw new ArgumentNullException("json");
1186 }
1187 if (jsonTypeInfo == null)
1188 {
1189 throw new ArgumentNullException("jsonTypeInfo");
1190 }
1191 return ReadFromSpan<TValue>(json.AsSpan(), jsonTypeInfo);
1192 }
1193
1195 {
1196 if (jsonTypeInfo == null)
1197 {
1198 throw new ArgumentNullException("jsonTypeInfo");
1199 }
1201 }
1202
1203 public static object? Deserialize(string json, Type returnType, JsonSerializerContext context)
1204 {
1205 if (json == null)
1206 {
1207 throw new ArgumentNullException("json");
1208 }
1209 if (returnType == null)
1210 {
1211 throw new ArgumentNullException("returnType");
1212 }
1213 if (context == null)
1214 {
1215 throw new ArgumentNullException("context");
1216 }
1217 JsonTypeInfo typeInfo = GetTypeInfo(context, returnType);
1218 return ReadFromSpan<object>(json.AsSpan(), typeInfo);
1219 }
1220
1222 {
1223 if (returnType == null)
1224 {
1225 throw new ArgumentNullException("returnType");
1226 }
1227 if (context == null)
1228 {
1229 throw new ArgumentNullException("context");
1230 }
1231 JsonTypeInfo typeInfo = GetTypeInfo(context, returnType);
1232 return ReadFromSpan<object>(json, typeInfo);
1233 }
1234
1236 {
1237 byte[] array = null;
1238 Span<byte> span = (((long)json.Length > 349525L) ? new byte[JsonReaderHelper.GetUtf8ByteCount(json)] : (array = ArrayPool<byte>.Shared.Rent(json.Length * 3)));
1239 try
1240 {
1242 span = span.Slice(0, utf8FromText);
1244 }
1245 finally
1246 {
1247 if (array != null)
1248 {
1249 span.Clear();
1250 ArrayPool<byte>.Shared.Return(array);
1251 }
1252 }
1253 }
1254
1255 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1257 {
1258 JsonTypeInfo typeInfo = GetTypeInfo(options, typeof(TValue));
1259 return Read<TValue>(ref reader, typeInfo);
1260 }
1261
1262 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1264 {
1265 if (returnType == null)
1266 {
1267 throw new ArgumentNullException("returnType");
1268 }
1270 return Read<object>(ref reader, typeInfo);
1271 }
1272
1274 {
1275 if (jsonTypeInfo == null)
1276 {
1277 throw new ArgumentNullException("jsonTypeInfo");
1278 }
1279 return Read<TValue>(ref reader, jsonTypeInfo);
1280 }
1281
1282 public static object? Deserialize(ref Utf8JsonReader reader, Type returnType, JsonSerializerContext context)
1283 {
1284 if (returnType == null)
1285 {
1286 throw new ArgumentNullException("returnType");
1287 }
1288 if (context == null)
1289 {
1290 throw new ArgumentNullException("context");
1291 }
1292 return Read<object>(ref reader, GetTypeInfo(context, returnType));
1293 }
1294
1296 {
1297 ReadStack state = default(ReadStack);
1299 JsonReaderState currentState = reader.CurrentState;
1300 if (currentState.Options.CommentHandling == JsonCommentHandling.Allow)
1301 {
1303 }
1307 try
1308 {
1309 JsonTokenType tokenType = reader.TokenType;
1311 if ((tokenType == JsonTokenType.None || tokenType == JsonTokenType.PropertyName) && !reader.Read())
1312 {
1313 bytes = default(ReadOnlySpan<byte>);
1314 ThrowHelper.ThrowJsonReaderException(ref reader, ExceptionResource.ExpectedOneCompleteToken, 0, bytes);
1315 }
1316 switch (reader.TokenType)
1317 {
1318 case JsonTokenType.StartObject:
1319 case JsonTokenType.StartArray:
1320 {
1321 long tokenStartIndex = reader.TokenStartIndex;
1322 if (!reader.TrySkip())
1323 {
1324 bytes = default(ReadOnlySpan<byte>);
1326 }
1328 ReadOnlySequence<byte> originalSequence = reader.OriginalSequence;
1329 if (originalSequence.IsEmpty)
1330 {
1331 bytes = reader.OriginalSpan;
1332 readOnlySpan = checked(bytes.Slice((int)tokenStartIndex, (int)num));
1333 }
1334 else
1335 {
1337 }
1338 break;
1339 }
1340 case JsonTokenType.Number:
1341 case JsonTokenType.True:
1342 case JsonTokenType.False:
1343 case JsonTokenType.Null:
1344 if (reader.HasValueSequence)
1345 {
1346 source = reader.ValueSequence;
1347 }
1348 else
1349 {
1350 readOnlySpan = reader.ValueSpan;
1351 }
1352 break;
1353 case JsonTokenType.String:
1354 {
1355 ReadOnlySequence<byte> originalSequence2 = reader.OriginalSequence;
1356 if (originalSequence2.IsEmpty)
1357 {
1358 bytes = reader.ValueSpan;
1359 int length = bytes.Length + 2;
1360 readOnlySpan = reader.OriginalSpan.Slice((int)reader.TokenStartIndex, length);
1361 break;
1362 }
1363 long num2 = 2L;
1364 if (reader.HasValueSequence)
1365 {
1366 num2 += reader.ValueSequence.Length;
1367 }
1368 else
1369 {
1370 long num3 = num2;
1371 bytes = reader.ValueSpan;
1372 num2 = num3 + bytes.Length;
1373 }
1374 source = originalSequence2.Slice(reader.TokenStartIndex, num2);
1375 break;
1376 }
1377 default:
1378 {
1379 byte b;
1380 if (reader.HasValueSequence)
1381 {
1382 bytes = reader.ValueSequence.First.Span;
1383 b = bytes[0];
1384 }
1385 else
1386 {
1387 bytes = reader.ValueSpan;
1388 b = bytes[0];
1389 }
1390 byte nextByte = b;
1391 bytes = default(ReadOnlySpan<byte>);
1392 ThrowHelper.ThrowJsonReaderException(ref reader, ExceptionResource.ExpectedStartOfValueNotFound, nextByte, bytes);
1393 break;
1394 }
1395 }
1396 }
1397 catch (JsonReaderException ex)
1398 {
1399 reader = utf8JsonReader;
1401 }
1402 int num4 = (readOnlySpan.IsEmpty ? checked((int)source.Length) : readOnlySpan.Length);
1403 byte[] array = ArrayPool<byte>.Shared.Rent(num4);
1404 Span<byte> span = array.AsSpan(0, num4);
1405 try
1406 {
1407 if (readOnlySpan.IsEmpty)
1408 {
1410 }
1411 else
1412 {
1414 }
1417 JsonConverter converterBase = state.Current.JsonPropertyInfo.ConverterBase;
1419 }
1420 catch (JsonException)
1421 {
1422 reader = utf8JsonReader;
1423 throw;
1424 }
1425 finally
1426 {
1427 span.Clear();
1428 ArrayPool<byte>.Shared.Return(array);
1429 }
1430 }
1431
1432 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1434 {
1435 Type runtimeType = GetRuntimeType(in value);
1437 return WriteBytesUsingSerializer(in value, typeInfo);
1438 }
1439
1440 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1447
1449 {
1450 if (jsonTypeInfo == null)
1451 {
1452 throw new ArgumentNullException("jsonTypeInfo");
1453 }
1455 }
1456
1457 public static byte[] SerializeToUtf8Bytes(object? value, Type inputType, JsonSerializerContext context)
1458 {
1459 if (context == null)
1460 {
1461 throw new ArgumentNullException("context");
1462 }
1466 }
1467
1469 {
1472 using (Utf8JsonWriter writer = new Utf8JsonWriter(pooledByteBufferWriter, options.GetWriterOptions()))
1473 {
1475 }
1476 return pooledByteBufferWriter.WrittenMemory.ToArray();
1477 }
1478
1480 {
1483 using (Utf8JsonWriter writer = new Utf8JsonWriter(pooledByteBufferWriter, options.GetWriterOptions()))
1484 {
1486 }
1487 return pooledByteBufferWriter.WrittenMemory.ToArray();
1488 }
1489
1491 {
1492 MetadataPropertyName result;
1493 if (state.BoxedStructReferenceId != null)
1494 {
1495 writer.WriteString(s_metadataId, state.BoxedStructReferenceId);
1496 result = MetadataPropertyName.Id;
1498 }
1499 else if (!jsonConverter.CanHaveIdMetadata || jsonConverter.IsValueType)
1500 {
1501 result = MetadataPropertyName.NoMetadata;
1502 }
1503 else
1504 {
1505 bool alreadyExists;
1506 string reference = state.ReferenceResolver.GetReference(currentValue, out alreadyExists);
1507 if (alreadyExists)
1508 {
1509 writer.WriteString(s_metadataRef, reference);
1510 writer.WriteEndObject();
1511 result = MetadataPropertyName.Ref;
1512 }
1513 else
1514 {
1515 writer.WriteString(s_metadataId, reference);
1516 result = MetadataPropertyName.Id;
1517 }
1518 }
1519 return result;
1520 }
1521
1523 {
1524 MetadataPropertyName result;
1525 if (state.BoxedStructReferenceId != null)
1526 {
1527 writer.WriteStartObject();
1528 writer.WriteString(s_metadataId, state.BoxedStructReferenceId);
1529 writer.WriteStartArray(s_metadataValues);
1530 result = MetadataPropertyName.Id;
1532 }
1533 else if (!jsonConverter.CanHaveIdMetadata || jsonConverter.IsValueType)
1534 {
1535 writer.WriteStartArray();
1536 result = MetadataPropertyName.NoMetadata;
1537 }
1538 else
1539 {
1540 bool alreadyExists;
1541 string reference = state.ReferenceResolver.GetReference(currentValue, out alreadyExists);
1542 if (alreadyExists)
1543 {
1544 writer.WriteStartObject();
1545 writer.WriteString(s_metadataRef, reference);
1546 writer.WriteEndObject();
1547 result = MetadataPropertyName.Ref;
1548 }
1549 else
1550 {
1551 writer.WriteStartObject();
1552 writer.WriteString(s_metadataId, reference);
1553 writer.WriteStartArray(s_metadataValues);
1554 result = MetadataPropertyName.Id;
1555 }
1556 }
1557 return result;
1558 }
1559
1560 internal static bool TryWriteReferenceForBoxedStruct(object currentValue, ref WriteStack state, Utf8JsonWriter writer)
1561 {
1562 bool alreadyExists;
1563 string reference = state.ReferenceResolver.GetReference(currentValue, out alreadyExists);
1564 if (alreadyExists)
1565 {
1566 writer.WriteStartObject();
1567 writer.WriteString(s_metadataRef, reference);
1568 writer.WriteEndObject();
1569 }
1570 else
1571 {
1573 }
1574 return alreadyExists;
1575 }
1576
1578 {
1580 writer.Flush();
1581 return result;
1582 }
1583
1585 {
1587 {
1588 JsonSerializerContext context = jsonTypeInfo2.Options._context;
1589 if (context != null && context.CanUseSerializationLogic)
1590 {
1591 jsonTypeInfo2.SerializeHandler(writer, value);
1592 writer.Flush();
1593 return;
1594 }
1595 }
1597 }
1598
1600 {
1601 WriteStack state = default(WriteStack);
1603 JsonConverter converterBase = jsonTypeInfo.PropertyInfoForTypeInfo.ConverterBase;
1605 {
1606 jsonConverter.WriteCore(writer, in value, jsonTypeInfo.Options, ref state);
1607 }
1608 else
1609 {
1610 converterBase.WriteCoreAsObject(writer, value, jsonTypeInfo.Options, ref state);
1611 }
1612 writer.Flush();
1613 }
1614
1615 private static Type GetRuntimeType<TValue>(in TValue value)
1616 {
1617 Type type = typeof(TValue);
1618 if (type == JsonTypeInfo.ObjectType && value != null)
1619 {
1620 type = value.GetType();
1621 }
1622 return type;
1623 }
1624
1626 {
1627 if ((object)inputType == null)
1628 {
1629 throw new ArgumentNullException("inputType");
1630 }
1631 if (value != null)
1632 {
1633 Type type = value.GetType();
1634 if (!inputType.IsAssignableFrom(type))
1635 {
1637 }
1639 {
1640 return type;
1641 }
1642 }
1643 return inputType;
1644 }
1645
1646 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1648 {
1649 if (utf8Json == null)
1650 {
1651 throw new ArgumentNullException("utf8Json");
1652 }
1653 Type runtimeType = GetRuntimeType(in value);
1656 }
1657
1658 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1660 {
1661 if (utf8Json == null)
1662 {
1663 throw new ArgumentNullException("utf8Json");
1664 }
1665 Type runtimeType = GetRuntimeType(in value);
1667 WriteStream(utf8Json, in value, typeInfo);
1668 }
1669
1670 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1681
1682 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1684 {
1685 if (utf8Json == null)
1686 {
1687 throw new ArgumentNullException("utf8Json");
1688 }
1691 WriteStream(utf8Json, in value, typeInfo);
1692 }
1693
1695 {
1696 if (utf8Json == null)
1697 {
1698 throw new ArgumentNullException("utf8Json");
1699 }
1700 if (jsonTypeInfo == null)
1701 {
1702 throw new ArgumentNullException("jsonTypeInfo");
1703 }
1705 }
1706
1708 {
1709 if (utf8Json == null)
1710 {
1711 throw new ArgumentNullException("utf8Json");
1712 }
1713 if (jsonTypeInfo == null)
1714 {
1715 throw new ArgumentNullException("jsonTypeInfo");
1716 }
1718 }
1719
1721 {
1722 if (utf8Json == null)
1723 {
1724 throw new ArgumentNullException("utf8Json");
1725 }
1726 if (context == null)
1727 {
1728 throw new ArgumentNullException("context");
1729 }
1732 }
1733
1734 public static void Serialize(Stream utf8Json, object? value, Type inputType, JsonSerializerContext context)
1735 {
1736 if (utf8Json == null)
1737 {
1738 throw new ArgumentNullException("utf8Json");
1739 }
1740 if (context == null)
1741 {
1742 throw new ArgumentNullException("context");
1743 }
1746 }
1747
1749 {
1751 JsonWriterOptions writerOptions = options.GetWriterOptions();
1755 {
1757 };
1759 try
1760 {
1761 bool isFinalBlock;
1762 do
1763 {
1764 state.FlushThreshold = (int)((float)bufferWriter.Capacity * 0.9f);
1765 try
1766 {
1768 if (state.SuppressFlush)
1769 {
1770 state.SuppressFlush = false;
1771 continue;
1772 }
1773 await bufferWriter.WriteToStreamAsync(utf8Json, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
1775 }
1776 finally
1777 {
1778 if (state.PendingTask != null)
1779 {
1780 try
1781 {
1782 await state.PendingTask.ConfigureAwait(continueOnCapturedContext: false);
1783 }
1784 catch
1785 {
1786 }
1787 }
1788 List<IAsyncDisposable> completedAsyncDisposables = state.CompletedAsyncDisposables;
1790 {
1791 await state.DisposeCompletedAsyncDisposables().ConfigureAwait(continueOnCapturedContext: false);
1792 }
1793 }
1794 }
1795 while (!isFinalBlock);
1796 }
1797 catch
1798 {
1799 await state.DisposePendingDisposablesOnExceptionAsync().ConfigureAwait(continueOnCapturedContext: false);
1800 throw;
1801 }
1802 }
1803
1805 {
1807 JsonWriterOptions writerOptions = options.GetWriterOptions();
1810 WriteStack state = default(WriteStack);
1812 bool flag;
1813 do
1814 {
1816 flag = WriteCore(jsonConverter, writer, in value, options, ref state);
1817 pooledByteBufferWriter.WriteToStream(utf8Json);
1819 }
1820 while (!flag);
1821 }
1822
1823 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1824 public static string Serialize<TValue>(TValue value, JsonSerializerOptions? options = null)
1825 {
1826 Type runtimeType = GetRuntimeType(in value);
1828 return WriteStringUsingSerializer(in value, typeInfo);
1829 }
1830
1831 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1838
1843
1844 public static string Serialize(object? value, Type inputType, JsonSerializerContext context)
1845 {
1846 if (context == null)
1847 {
1848 throw new ArgumentNullException("context");
1849 }
1853 }
1854
1856 {
1857 if (jsonTypeInfo == null)
1858 {
1859 throw new ArgumentNullException("jsonTypeInfo");
1860 }
1863 using (Utf8JsonWriter writer = new Utf8JsonWriter(pooledByteBufferWriter, options.GetWriterOptions()))
1864 {
1866 }
1867 return JsonReaderHelper.TranscodeHelper(pooledByteBufferWriter.WrittenMemory.Span);
1868 }
1869
1871 {
1872 if (jsonTypeInfo == null)
1873 {
1874 throw new ArgumentNullException("jsonTypeInfo");
1875 }
1878 using (Utf8JsonWriter writer = new Utf8JsonWriter(pooledByteBufferWriter, options.GetWriterOptions()))
1879 {
1881 }
1882 return JsonReaderHelper.TranscodeHelper(pooledByteBufferWriter.WrittenMemory.Span);
1883 }
1884
1885 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1887 {
1888 if (writer == null)
1889 {
1890 throw new ArgumentNullException("writer");
1891 }
1892 Type runtimeType = GetRuntimeType(in value);
1894 WriteUsingSerializer(writer, in value, typeInfo);
1895 }
1896
1897 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
1908
1910 {
1911 if (writer == null)
1912 {
1913 throw new ArgumentNullException("writer");
1914 }
1915 if (jsonTypeInfo == null)
1916 {
1917 throw new ArgumentNullException("jsonTypeInfo");
1918 }
1920 }
1921
1923 {
1924 if (writer == null)
1925 {
1926 throw new ArgumentNullException("writer");
1927 }
1928 if (context == null)
1929 {
1930 throw new ArgumentNullException("context");
1931 }
1934 }
1935}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
static string JsonSerializerDoesNotSupportComments
Definition SR.cs:180
Definition SR.cs:7
static ReadOnlySpan< byte > Utf8Bom
static JsonDocument ParseRented(PooledByteBufferWriter utf8Json, JsonDocumentOptions options=default(JsonDocumentOptions))
static string Utf8GetString(ReadOnlySpan< byte > bytes)
static bool IsInRangeInclusive(uint value, uint lowerBound, uint upperBound)
static string TranscodeHelper(ReadOnlySpan< byte > utf8Unescaped)
static ReadOnlySpan< byte > GetUnescapedSpan(ReadOnlySpan< byte > utf8Source, int idx)
static int GetUtf8FromText(ReadOnlySpan< char > text, Span< byte > dest)
static int GetUtf8ByteCount(ReadOnlySpan< char > text)
static readonly JsonSerializerOptions s_defaultOptions
static ? object Deserialize(Stream utf8Json, Type returnType, JsonSerializerContext context)
static ? object Deserialize(this JsonElement element, Type returnType, JsonSerializerOptions? options=null)
static ? object Deserialize(this JsonNode? node, Type returnType, JsonSerializerContext context)
static byte[] SerializeToUtf8Bytes(object? value, Type inputType, JsonSerializerContext context)
static TValue ReadFromSpan< TValue >(ReadOnlySpan< byte > utf8Json, JsonTypeInfo jsonTypeInfo, int? actualByteCount=null)
static JsonPropertyInfo LookupProperty(object obj, ReadOnlySpan< byte > unescapedPropertyName, ref ReadStack state, JsonSerializerOptions options, out bool useExtensionProperty, bool createExtensionProperty=true)
static JsonDocument SerializeToDocument< TValue >(TValue value, JsonSerializerOptions? options=null)
static void Serialize< TValue >(Stream utf8Json, TValue value, JsonSerializerOptions? options=null)
static ? object Deserialize(ReadOnlySpan< char > json, Type returnType, JsonSerializerContext context)
static ReadOnlySpan< byte > GetPropertyName(ref ReadStack state, ref Utf8JsonReader reader, JsonSerializerOptions options)
static ? object Deserialize(this JsonDocument document, Type returnType, JsonSerializerOptions? options=null)
static Task SerializeAsync(Stream utf8Json, object? value, Type inputType, JsonSerializerOptions? options=null, CancellationToken cancellationToken=default(CancellationToken))
static void Serialize(Stream utf8Json, object? value, Type inputType, JsonSerializerOptions? options=null)
static ? JsonNode SerializeToNode< TValue >(TValue value, JsonSerializerOptions? options=null)
static readonly JsonEncodedText s_metadataRef
static bool ResolveMetadataForJsonObject< T >(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
static ? object Deserialize(ref Utf8JsonReader reader, Type returnType, JsonSerializerContext context)
static TValue ContinueDeserialize< TValue >(ref ReadBufferState bufferState, ref JsonReaderState jsonReaderState, ref ReadStack readStack, JsonConverter converter, JsonSerializerOptions options)
static ? JsonNode SerializeToNode(object? value, Type inputType, JsonSerializerOptions? options=null)
static ? object Deserialize(ref Utf8JsonReader reader, Type returnType, JsonSerializerOptions? options=null)
static JsonElement SerializeToElement(object? value, Type inputType, JsonSerializerContext context)
static IAsyncEnumerable< TValue?> DeserializeAsyncEnumerable< TValue >(Stream utf8Json, JsonSerializerOptions? options=null, CancellationToken cancellationToken=default(CancellationToken))
static JsonElement SerializeToElement(object? value, Type inputType, JsonSerializerOptions? options=null)
static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerContext context)
static void Serialize(Stream utf8Json, object? value, Type inputType, JsonSerializerContext context)
static ValueTask< object?> DeserializeAsync(Stream utf8Json, Type returnType, JsonSerializerContext context, CancellationToken cancellationToken=default(CancellationToken))
static void Serialize(Utf8JsonWriter writer, object? value, Type inputType, JsonSerializerContext context)
static string WriteStringUsingSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static JsonDocument WriteDocumentUsingGeneratedSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static ValueTask< object?> DeserializeAsync(Stream utf8Json, Type returnType, JsonSerializerOptions? options=null, CancellationToken cancellationToken=default(CancellationToken))
static string Serialize(object? value, Type inputType, JsonSerializerContext context)
static void WriteUsingSerializer< TValue >(Utf8JsonWriter writer, in TValue value, JsonTypeInfo jsonTypeInfo)
static void ValidateValueIsCorrectType< T >(object value, string referenceId)
static void WriteStream< TValue >(Stream utf8Json, in TValue value, JsonTypeInfo jsonTypeInfo)
static string WriteStringUsingGeneratedSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static ? object Deserialize(this JsonElement element, Type returnType, JsonSerializerContext context)
static Type GetRuntimeType< TValue >(in TValue value)
static JsonElement WriteElementUsingGeneratedSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static bool ResolveMetadataForJsonArray< T >(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
static readonly byte[] s_refPropertyName
static ? object Deserialize(string json, Type returnType, JsonSerializerOptions? options=null)
static JsonTypeInfo GetTypeInfo(JsonSerializerContext context, Type type)
static byte[] SerializeToUtf8Bytes< TValue >(TValue value, JsonSerializerOptions? options=null)
static ? object Deserialize(Stream utf8Json, Type returnType, JsonSerializerOptions? options=null)
static Task SerializeAsync< TValue >(Stream utf8Json, TValue value, JsonSerializerOptions? options=null, CancellationToken cancellationToken=default(CancellationToken))
static JsonDocument SerializeToDocument(object? value, Type inputType, JsonSerializerOptions? options=null)
static JsonTypeInfo CreateQueueJsonTypeInfo< TValue >(JsonConverter queueConverter, JsonSerializerOptions queueOptions)
static JsonElement WriteElementUsingSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static ? object Deserialize(ReadOnlySpan< byte > utf8Json, Type returnType, JsonSerializerContext context)
static MetadataPropertyName WriteReferenceForObject(JsonConverter jsonConverter, object currentValue, ref WriteStack state, Utf8JsonWriter writer)
static readonly JsonEncodedText s_metadataValues
static TValue ReadCore< TValue >(JsonConverter jsonConverter, ref Utf8JsonReader reader, JsonSerializerOptions options, ref ReadStack state)
static byte[] WriteBytesUsingGeneratedSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static TValue ReadAll< TValue >(Stream utf8Json, JsonTypeInfo jsonTypeInfo)
static bool IsValidNumberHandlingValue(JsonNumberHandling handling)
static ? object Deserialize(string json, Type returnType, JsonSerializerContext context)
static readonly byte[] s_valuesPropertyName
static byte[] SerializeToUtf8Bytes(object? value, Type inputType, JsonSerializerOptions? options=null)
static TValue ReadUsingMetadata< TValue >(JsonElement element, JsonTypeInfo jsonTypeInfo)
static TValue ReadAllUsingOptions< TValue >(Stream utf8Json, Type returnType, JsonSerializerOptions options)
static JsonNode WriteNodeUsingGeneratedSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static MetadataPropertyName WriteReferenceForCollection(JsonConverter jsonConverter, object currentValue, ref WriteStack state, Utf8JsonWriter writer)
static bool WriteCore< TValue >(JsonConverter jsonConverter, Utf8JsonWriter writer, in TValue value, JsonSerializerOptions options, ref WriteStack state)
static ? JsonNode SerializeToNode(object? value, Type inputType, JsonSerializerContext context)
static bool TryGetReferenceFromJsonElement(ref ReadStack state, JsonElement element, out object referenceValue)
static JsonDocument WriteDocumentUsingSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static ? object Deserialize(ReadOnlySpan< char > json, Type returnType, JsonSerializerOptions? options=null)
static TValue ReadDocument< TValue >(JsonDocument document, JsonTypeInfo jsonTypeInfo)
static async ValueTask< TValue > ReadAllAsync< TValue >(Stream utf8Json, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
static TValue ReadNode< TValue >(JsonNode node, JsonTypeInfo jsonTypeInfo)
static readonly JsonEncodedText s_metadataId
static TValue Read< TValue >(ref Utf8JsonReader reader, JsonTypeInfo jsonTypeInfo)
static ? object Deserialize(ReadOnlySpan< byte > utf8Json, Type returnType, JsonSerializerOptions? options=null)
static void CreateDataExtensionProperty(object obj, JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options)
static Type GetRuntimeTypeAndValidateInputType(object value, Type inputType)
static JsonTypeInfo GetTypeInfo(JsonSerializerOptions options, Type runtimeType)
static bool TryReadAheadMetadataAndSetState(ref Utf8JsonReader reader, ref ReadStack state, StackFrameObjectState nextState)
static async ValueTask< ReadBufferState > ReadFromStreamAsync(Stream utf8Json, ReadBufferState bufferState, CancellationToken cancellationToken)
static bool TryWriteReferenceForBoxedStruct(object currentValue, ref WriteStack state, Utf8JsonWriter writer)
static byte[] WriteBytesUsingSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static void WriteUsingGeneratedSerializer< TValue >(Utf8JsonWriter writer, in TValue value, JsonTypeInfo jsonTypeInfo)
static ? object Deserialize(this JsonDocument document, Type returnType, JsonSerializerContext context)
static ? object Deserialize(this JsonNode? node, Type returnType, JsonSerializerOptions? options=null)
static string Serialize(object? value, Type inputType, JsonSerializerOptions? options=null)
static async Task WriteStreamAsync< TValue >(Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
static ? TValue Deserialize< TValue >(this JsonDocument document, JsonSerializerOptions? options=null)
static MetadataPropertyName GetMetadataPropertyName(ReadOnlySpan< byte > propertyName)
static ValueTask< TValue?> DeserializeAsync< TValue >(Stream utf8Json, JsonSerializerOptions? options=null, CancellationToken cancellationToken=default(CancellationToken))
static ReadBufferState ReadFromStream(Stream utf8Json, ReadBufferState bufferState)
static JsonNode WriteNodeUsingSerializer< TValue >(in TValue value, JsonTypeInfo jsonTypeInfo)
static void Serialize(Utf8JsonWriter writer, object? value, Type inputType, JsonSerializerOptions? options=null)
static readonly byte[] s_idPropertyName
static JsonElement SerializeToElement< TValue >(TValue value, JsonSerializerOptions? options=null)
static Task SerializeAsync(Stream utf8Json, object? value, Type inputType, JsonSerializerContext context, CancellationToken cancellationToken=default(CancellationToken))
static ? JsonNode Parse(ref Utf8JsonReader reader, JsonNodeOptions? nodeOptions=null)
Definition JsonNode.cs:495
virtual void Initialize(JsonSerializerOptions options, JsonTypeInfo jsonTypeInfo=null)
static void ThrowJsonException_MetadataValueWasNotString(JsonTokenType tokenType)
static void ThrowJsonException_MetadataMissingIdBeforeValues(ref ReadStack state, ReadOnlySpan< byte > propertyName)
static void ThrowJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte=0, ReadOnlySpan< byte > bytes=default(ReadOnlySpan< byte >))
static void ThrowJsonException_MetadataReferenceObjectCannotContainOtherProperties(ReadOnlySpan< byte > propertyName, ref ReadStack state)
static void ThrowJsonException_MetadataValuesInvalidToken(JsonTokenType tokenType)
static void ThrowInvalidOperationException_MetadataReferenceOfTypeCannotBeAssignedToType(string referenceId, Type currentType, Type typeToConvert)
static void ThrowJsonException_MetadataInvalidReferenceToValueType(Type propertyType)
static void ThrowInvalidOperationException_NoMetadataForType(Type type)
static void ThrowArgumentException_DeserializeWrongType(Type type, object value)
static void ThrowJsonException_MetadataPreservedArrayValuesNotFound(ref ReadStack state, Type propertyType)
static void ReThrowWithPath(ref ReadStack state, JsonReaderException ex)
static void ThrowJsonException_MetadataInvalidPropertyWithLeadingDollarSign(ReadOnlySpan< byte > propertyName, ref ReadStack state, in Utf8JsonReader reader)
static void ThrowJsonException_MetadataPreservedArrayInvalidProperty(ref ReadStack state, Type propertyType, in Utf8JsonReader reader)
static void ThrowJsonException_MetadataCannotParsePreservedObjectIntoImmutable(Type propertyType)
static void ThrowNotSupportedException_SerializationNotSupported(Type propertyType)
static void ThrowUnexpectedMetadataException(ReadOnlySpan< byte > propertyName, ref Utf8JsonReader reader, ref ReadStack state)
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
unsafe ReadOnlySpan< T > Span
static JsonElement ParseValue(ref Utf8JsonReader reader)
ObjectEnumerator EnumerateObject()
ReadOnlyMemory< byte > GetRawValue()
static JsonEncodedText Encode(string value, JavaScriptEncoder? encoder=null)
void Initialize(Type type, JsonSerializerOptions options, bool supportContinuation)
Definition ReadStack.cs:53
JsonConverter Initialize(Type type, JsonSerializerOptions options, bool supportContinuation)
Definition WriteStack.cs:53