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

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<DateOnly> s_dateOnlyConverter; private static JsonConverter<TimeOnly> s_timeOnlyConverter; 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<JsonDocument> s_jsonDocumentConverter; 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 BooleanConverter()); public static JsonConverter<byte[]> ByteArrayConverter => s_byteArrayConverter ?? (s_byteArrayConverter = new ByteArrayConverter()); public static JsonConverter<byte> ByteConverter => s_byteConverter ?? (s_byteConverter = new ByteConverter()); public static JsonConverter<char> CharConverter => s_charConverter ?? (s_charConverter = new CharConverter()); public static JsonConverter<DateTime> DateTimeConverter => s_dateTimeConverter ?? (s_dateTimeConverter = new DateTimeConverter()); public static JsonConverter<DateTimeOffset> DateTimeOffsetConverter => s_dateTimeOffsetConverter ?? (s_dateTimeOffsetConverter = new DateTimeOffsetConverter()); public static JsonConverter<DateOnly> DateOnlyConverter => s_dateOnlyConverter ?? (s_dateOnlyConverter = new DateOnlyConverter()); public static JsonConverter<TimeOnly> TimeOnlyConverter => s_timeOnlyConverter ?? (s_timeOnlyConverter = new TimeOnlyConverter()); public static JsonConverter<decimal> DecimalConverter => s_decimalConverter ?? (s_decimalConverter = new DecimalConverter()); public static JsonConverter<double> DoubleConverter => s_doubleConverter ?? (s_doubleConverter = new DoubleConverter()); public static JsonConverter<Guid> GuidConverter => s_guidConverter ?? (s_guidConverter = new GuidConverter()); public static JsonConverter<short> Int16Converter => s_int16Converter ?? (s_int16Converter = new Int16Converter()); public static JsonConverter<int> Int32Converter => s_int32Converter ?? (s_int32Converter = new Int32Converter()); public static JsonConverter<long> Int64Converter => s_int64Converter ?? (s_int64Converter = new 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()); public static JsonConverter<JsonDocument> JsonDocumentConverter => s_jsonDocumentConverter ?? (s_jsonDocumentConverter = new JsonDocumentConverter()); [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 SingleConverter()); [CLSCompliant(false)] public static JsonConverter<sbyte> SByteConverter { get { return s_sbyteConverter ?? (s_sbyteConverter = new SByteConverter()); } } public static JsonConverter<string> StringConverter => s_stringConverter ?? (s_stringConverter = new StringConverter()); public static JsonConverter<TimeSpan> TimeSpanConverter => s_timeSpanConverter ?? (s_timeSpanConverter = new TimeSpanConverter()); [CLSCompliant(false)] public static JsonConverter<ushort> UInt16Converter { get { return s_uint16Converter ?? (s_uint16Converter = new UInt16Converter()); } } [CLSCompliant(false)] public static JsonConverter<uint> UInt32Converter { get { return s_uint32Converter ?? (s_uint32Converter = new UInt32Converter()); } } [CLSCompliant(false)] public static JsonConverter<ulong> UInt64Converter { get { return s_uint64Converter ?? (s_uint64Converter = new UInt64Converter()); } } public static JsonConverter<Uri> UriConverter => s_uriConverter ?? (s_uriConverter = new UriConverter()); public static JsonConverter<Version> VersionConverter => s_versionConverter ?? (s_versionConverter = new VersionConverter()); public static JsonTypeInfo<TElement[]> CreateArrayInfo<[System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TElement[]> collectionInfo) { return new SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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> { if (createRangeFunc == null) ThrowHelper.ThrowArgumentNullException("createRangeFunc"); return new SourceGenJsonTypeInfo<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new ImmutableDictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>()), (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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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> { if (createRangeFunc == null) ThrowHelper.ThrowArgumentNullException("createRangeFunc"); return new SourceGenJsonTypeInfo<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new ImmutableEnumerableOfTConverter<TCollection, TElement>()), (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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<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 SourceGenJsonTypeInfo<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IEnumerableOfTConverter<TCollection, TElement>()), (object)null, (object)null); } public static JsonTypeInfo<TCollection> CreateIAsyncEnumerableInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection, [System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IAsyncEnumerable<TElement> { return new SourceGenJsonTypeInfo<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new IAsyncEnumerableOfTConverter<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 SourceGenJsonTypeInfo<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 { if (addFunc == null) ThrowHelper.ThrowArgumentNullException("addFunc"); return new SourceGenJsonTypeInfo<TCollection>(options, collectionInfo, (Func<JsonConverter<TCollection>>)(() => new StackOrQueueConverter<TCollection>()), (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 SourceGenJsonTypeInfo<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) ThrowHelper.ThrowArgumentNullException("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) ThrowHelper.ThrowArgumentNullException("underlyingTypeInfo"); JsonConverter<T> typedConverter = GetTypedConverter<T>(underlyingTypeInfo.Converter); return new NullableConverter<T>(typedConverter); } [System.Runtime.CompilerServices.NullableContext(0)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] public static JsonConverter<T?> GetNullableConverter<T>([System.Runtime.CompilerServices.Nullable(1)] JsonSerializerOptions options) where T : struct { if (options == null) ThrowHelper.ThrowArgumentNullException("options"); JsonConverter<T> typedConverter = GetTypedConverter<T>(options.GetConverterInternal(typeof(T))); return new NullableConverter<T>(typedConverter); } internal static JsonConverter<T> GetTypedConverter<T>(JsonConverter converter) { JsonConverter<T> jsonConverter = converter as JsonConverter<T>; if (jsonConverter == null) throw new InvalidOperationException(System.SR.Format(System.SR.SerializationConverterNotCompatible, jsonConverter, typeof(T))); return jsonConverter; } public static JsonPropertyInfo CreatePropertyInfo<[System.Runtime.CompilerServices.Nullable(2)] T>(JsonSerializerOptions options, JsonPropertyInfoValues<T> propertyInfo) { if (options == null) ThrowHelper.ThrowArgumentNullException("options"); if (propertyInfo == null) ThrowHelper.ThrowArgumentNullException("propertyInfo"); Type declaringType = propertyInfo.DeclaringType; if (declaringType == (Type)null) throw new ArgumentException("DeclaringType"); string propertyName = propertyInfo.PropertyName; if (propertyName == null) throw new ArgumentException("PropertyName"); if (!propertyInfo.IsProperty && propertyInfo.IsVirtual) throw new InvalidOperationException(System.SR.Format(System.SR.FieldCannotBeVirtual, "IsProperty", "IsVirtual")); return new JsonPropertyInfo<T>(propertyInfo, options); } public static JsonTypeInfo<T> CreateObjectInfo<T>(JsonSerializerOptions options, JsonObjectInfoValues<T> objectInfo) { if (options == null) ThrowHelper.ThrowArgumentNullException("options"); if (objectInfo == null) ThrowHelper.ThrowArgumentNullException("objectInfo"); return new SourceGenJsonTypeInfo<T>(options, objectInfo); } public static JsonTypeInfo<T> CreateValueInfo<[System.Runtime.CompilerServices.Nullable(2)] T>(JsonSerializerOptions options, JsonConverter converter) { if (options == null) ThrowHelper.ThrowArgumentNullException("options"); if (converter == null) ThrowHelper.ThrowArgumentNullException("converter"); return new SourceGenJsonTypeInfo<T>(converter, options); } } }