<PackageReference Include="System.Text.Json" Version="6.0.5" />

JsonMetadataServices

public static class JsonMetadataServices
Provides helpers to create and initialize metadata for JSON-serializable types.
using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Text.Json.Nodes; using System.Text.Json.Serialization.Converters; namespace System.Text.Json.Serialization.Metadata { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] [EditorBrowsable(EditorBrowsableState.Never)] public static class JsonMetadataServices { private static JsonConverter<bool> s_booleanConverter; private static JsonConverter<byte[]> s_byteArrayConverter; private static JsonConverter<byte> s_byteConverter; private static JsonConverter<char> s_charConverter; private static JsonConverter<DateTime> s_dateTimeConverter; private static JsonConverter<DateTimeOffset> s_dateTimeOffsetConverter; private static JsonConverter<decimal> s_decimalConverter; private static JsonConverter<double> s_doubleConverter; private static JsonConverter<Guid> s_guidConverter; private static JsonConverter<short> s_int16Converter; private static JsonConverter<int> s_int32Converter; private static JsonConverter<long> s_int64Converter; private static JsonConverter<JsonArray> s_jsonArrayConverter; private static JsonConverter<JsonElement> s_jsonElementConverter; private static JsonConverter<JsonNode> s_jsonNodeConverter; private static JsonConverter<JsonObject> s_jsonObjectConverter; private static JsonConverter<JsonValue> s_jsonValueConverter; private static JsonConverter<object> s_objectConverter; private static JsonConverter<float> s_singleConverter; private static JsonConverter<sbyte> s_sbyteConverter; private static JsonConverter<string> s_stringConverter; private static JsonConverter<TimeSpan> s_timeSpanConverter; private static JsonConverter<ushort> s_uint16Converter; private static JsonConverter<uint> s_uint32Converter; private static JsonConverter<ulong> s_uint64Converter; private static JsonConverter<Uri> s_uriConverter; private static JsonConverter<Version> s_versionConverter; public static JsonConverter<bool> BooleanConverter => s_booleanConverter ?? (s_booleanConverter = new System.Text.Json.Serialization.Converters.BooleanConverter()); public static JsonConverter<byte[]> ByteArrayConverter => s_byteArrayConverter ?? (s_byteArrayConverter = new ByteArrayConverter()); public static JsonConverter<byte> ByteConverter => s_byteConverter ?? (s_byteConverter = new System.Text.Json.Serialization.Converters.ByteConverter()); public static JsonConverter<char> CharConverter => s_charConverter ?? (s_charConverter = new System.Text.Json.Serialization.Converters.CharConverter()); public static JsonConverter<DateTime> DateTimeConverter => s_dateTimeConverter ?? (s_dateTimeConverter = new System.Text.Json.Serialization.Converters.DateTimeConverter()); public static JsonConverter<DateTimeOffset> DateTimeOffsetConverter => s_dateTimeOffsetConverter ?? (s_dateTimeOffsetConverter = new System.Text.Json.Serialization.Converters.DateTimeOffsetConverter()); public static JsonConverter<decimal> DecimalConverter => s_decimalConverter ?? (s_decimalConverter = new System.Text.Json.Serialization.Converters.DecimalConverter()); public static JsonConverter<double> DoubleConverter => s_doubleConverter ?? (s_doubleConverter = new System.Text.Json.Serialization.Converters.DoubleConverter()); public static JsonConverter<Guid> GuidConverter => s_guidConverter ?? (s_guidConverter = new System.Text.Json.Serialization.Converters.GuidConverter()); public static JsonConverter<short> Int16Converter => s_int16Converter ?? (s_int16Converter = new System.Text.Json.Serialization.Converters.Int16Converter()); public static JsonConverter<int> Int32Converter => s_int32Converter ?? (s_int32Converter = new System.Text.Json.Serialization.Converters.Int32Converter()); public static JsonConverter<long> Int64Converter => s_int64Converter ?? (s_int64Converter = new System.Text.Json.Serialization.Converters.Int64Converter()); public static JsonConverter<JsonArray> JsonArrayConverter => s_jsonArrayConverter ?? (s_jsonArrayConverter = new JsonArrayConverter()); public static JsonConverter<JsonElement> JsonElementConverter => s_jsonElementConverter ?? (s_jsonElementConverter = new JsonElementConverter()); public static JsonConverter<JsonNode> JsonNodeConverter => s_jsonNodeConverter ?? (s_jsonNodeConverter = new JsonNodeConverter()); public static JsonConverter<JsonObject> JsonObjectConverter => s_jsonObjectConverter ?? (s_jsonObjectConverter = new JsonObjectConverter()); public static JsonConverter<JsonValue> JsonValueConverter => s_jsonValueConverter ?? (s_jsonValueConverter = new JsonValueConverter()); [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public static JsonConverter<object> ObjectConverter { [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] get { return s_objectConverter ?? (s_objectConverter = new ObjectConverter()); } } public static JsonConverter<float> SingleConverter => s_singleConverter ?? (s_singleConverter = new System.Text.Json.Serialization.Converters.SingleConverter()); [CLSCompliant(false)] public static JsonConverter<sbyte> SByteConverter { get { return s_sbyteConverter ?? (s_sbyteConverter = new System.Text.Json.Serialization.Converters.SByteConverter()); } } public static JsonConverter<string> StringConverter => s_stringConverter ?? (s_stringConverter = new System.Text.Json.Serialization.Converters.StringConverter()); public static JsonConverter<TimeSpan> TimeSpanConverter => s_timeSpanConverter ?? (s_timeSpanConverter = new System.Text.Json.Serialization.Converters.TimeSpanConverter()); [CLSCompliant(false)] public static JsonConverter<ushort> UInt16Converter { get { return s_uint16Converter ?? (s_uint16Converter = new System.Text.Json.Serialization.Converters.UInt16Converter()); } } [CLSCompliant(false)] public static JsonConverter<uint> UInt32Converter { get { return s_uint32Converter ?? (s_uint32Converter = new System.Text.Json.Serialization.Converters.UInt32Converter()); } } [CLSCompliant(false)] public static JsonConverter<ulong> UInt64Converter { get { return s_uint64Converter ?? (s_uint64Converter = new System.Text.Json.Serialization.Converters.UInt64Converter()); } } public static JsonConverter<Uri> UriConverter => s_uriConverter ?? (s_uriConverter = new UriConverter()); public static JsonConverter<Version> VersionConverter => s_versionConverter ?? (s_versionConverter = new System.Text.Json.Serialization.Converters.VersionConverter()); public static JsonTypeInfo<TElement[]> CreateArrayInfo<[System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TElement[]> collectionInfo) { return new JsonTypeInfoInternal<TElement[]>(options, collectionInfo, (Func<JsonConverter<TElement[]>>)(() => new ArrayConverter<TElement[], TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateListInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : List<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new ListOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateDictionaryInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : Dictionary<TKey, TValue> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new DictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateImmutableDictionaryInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 1, 0, 1, 1, 1 })] Func<IEnumerable<KeyValuePair<TKey, TValue>>, TCollection> createRangeFunc) where TCollection : IReadOnlyDictionary<TKey, TValue> { Func<JsonConverter<TCollection>> converterCreator = () => new ImmutableDictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>(); if (createRangeFunc == null) throw new ArgumentNullException("createRangeFunc"); return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, converterCreator, (object)createRangeFunc, (object)null); } public static JsonTypeInfo<TCollection> CreateIDictionaryInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IDictionary<TKey, TValue> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IDictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateIReadOnlyDictionaryInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IReadOnlyDictionary<TKey, TValue> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IReadOnlyDictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateImmutableEnumerableInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo, Func<IEnumerable<TElement>, TCollection> createRangeFunc) where TCollection : IEnumerable<TElement> { Func<JsonConverter<TCollection>> converterCreator = () => new ImmutableEnumerableOfTConverter<TCollection, TElement>(); if (createRangeFunc == null) throw new ArgumentNullException("createRangeFunc"); return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, converterCreator, (object)createRangeFunc, (object)null); } public static JsonTypeInfo<TCollection> CreateIListInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IList { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IListConverter<TCollection>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateIListInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IList<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IListOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateISetInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : ISet<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new ISetOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateICollectionInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : ICollection<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new ICollectionOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateStackInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : Stack<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new StackOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateQueueInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : Queue<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new QueueOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateConcurrentStackInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : ConcurrentStack<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new ConcurrentStackOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateConcurrentQueueInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : ConcurrentQueue<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new ConcurrentQueueOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateIEnumerableInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IEnumerable<TElement> { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IEnumerableOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateIDictionaryInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IDictionary { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IDictionaryConverter<TCollection>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateStackInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 1, 2 })] Action<TCollection, object> addFunc) where TCollection : IEnumerable { return CreateStackOrQueueInfo(options, collectionInfo, addFunc); } public static JsonTypeInfo<TCollection> CreateQueueInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 1, 2 })] Action<TCollection, object> addFunc) where TCollection : IEnumerable { return CreateStackOrQueueInfo(options, collectionInfo, addFunc); } private static JsonTypeInfo<TCollection> CreateStackOrQueueInfo<TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo, Action<TCollection, object> addFunc) where TCollection : IEnumerable { Func<JsonConverter<TCollection>> converterCreator = () => new StackOrQueueConverter<TCollection>(); if (addFunc == null) throw new ArgumentNullException("addFunc"); return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, converterCreator, (object)null, (object)addFunc); } public static JsonTypeInfo<TCollection> CreateIEnumerableInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IEnumerable { return new JsonTypeInfoInternal<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IEnumerableConverter<TCollection>()), (object)null, (object)null); } public static JsonConverter<T> GetUnsupportedTypeConverter<[System.Runtime.CompilerServices.Nullable(2)] T>() { return new UnsupportedTypeConverter<T>(); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] public static JsonConverter<T> GetEnumConverter<[System.Runtime.CompilerServices.Nullable(0)] T>(JsonSerializerOptions options) where T : struct, Enum { if (options == null) throw new ArgumentNullException("options"); return new EnumConverter<T>(EnumConverterOptions.AllowNumbers, options); } [System.Runtime.CompilerServices.NullableContext(0)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] public static JsonConverter<T?> GetNullableConverter<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] JsonTypeInfo<T> underlyingTypeInfo) where T : struct { if (underlyingTypeInfo == null) throw new ArgumentNullException("underlyingTypeInfo"); JsonConverter<T> jsonConverter = underlyingTypeInfo.PropertyInfoForTypeInfo?.ConverterBase as JsonConverter<T>; if (jsonConverter == null) throw new InvalidOperationException(System.SR.Format(System.SR.SerializationConverterNotCompatible, jsonConverter, typeof(T))); return new NullableConverter<T>(jsonConverter); } public static JsonPropertyInfo CreatePropertyInfo<[System.Runtime.CompilerServices.Nullable(2)] T>(JsonSerializerOptions options, JsonPropertyInfoValues<T> propertyInfo) { if (options == null) throw new ArgumentNullException("options"); if (propertyInfo == null) throw new ArgumentNullException("propertyInfo"); Type declaringType = propertyInfo.DeclaringType; if (declaringType == (Type)null) throw new ArgumentException("DeclaringType"); JsonTypeInfo propertyTypeInfo = propertyInfo.PropertyTypeInfo; if (propertyTypeInfo == null) throw new ArgumentException("PropertyTypeInfo"); string propertyName = propertyInfo.PropertyName; if (propertyName == null) throw new ArgumentException("PropertyName"); JsonConverter converter = propertyInfo.Converter; if (converter == null) { converter = (propertyTypeInfo.PropertyInfoForTypeInfo.ConverterBase as JsonConverter<T>); if (converter == null) throw new InvalidOperationException(System.SR.Format(System.SR.ConverterForPropertyMustBeValid, declaringType, propertyName, typeof(T))); } if (!propertyInfo.IsProperty && propertyInfo.IsVirtual) throw new InvalidOperationException(System.SR.Format(System.SR.FieldCannotBeVirtual, "IsProperty", "IsVirtual")); JsonPropertyInfo<T> jsonPropertyInfo = new JsonPropertyInfo<T>(); jsonPropertyInfo.InitializeForSourceGen(options, propertyInfo); return jsonPropertyInfo; } public static JsonTypeInfo<T> CreateObjectInfo<T>(JsonSerializerOptions options, JsonObjectInfoValues<T> objectInfo) { if (options == null) throw new ArgumentNullException("options"); if (objectInfo == null) throw new ArgumentNullException("objectInfo"); return new JsonTypeInfoInternal<T>(options, objectInfo); } public static JsonTypeInfo<T> CreateValueInfo<[System.Runtime.CompilerServices.Nullable(2)] T>(JsonSerializerOptions options, JsonConverter converter) { JsonTypeInfo<T> jsonTypeInfo = new JsonTypeInfoInternal<T>(options); jsonTypeInfo.PropertyInfoForTypeInfo = CreateJsonPropertyInfoForClassInfo(typeof(T), jsonTypeInfo, converter, options); return jsonTypeInfo; } internal static JsonPropertyInfo CreateJsonPropertyInfoForClassInfo(Type type, JsonTypeInfo typeInfo, JsonConverter converter, JsonSerializerOptions options) { JsonPropertyInfo jsonPropertyInfo = converter.CreateJsonPropertyInfo(); jsonPropertyInfo.InitializeForTypeInfo(type, typeInfo, converter, options); return jsonPropertyInfo; } } }