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.Reflection;
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<JsonDocument> s_jsonDocumentConverter;
private static JsonConverter<Memory<byte>> s_memoryByteConverter;
private static JsonConverter<ReadOnlyMemory<byte>> s_readOnlyMemoryByteConverter;
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());
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<byte[]> ByteArrayConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return 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());
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<JsonArray> JsonArrayConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return s_jsonArrayConverter ?? (s_jsonArrayConverter = new JsonArrayConverter());
}
}
public static JsonConverter<JsonElement> JsonElementConverter => s_jsonElementConverter ?? (s_jsonElementConverter = new JsonElementConverter());
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<JsonNode> JsonNodeConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return s_jsonNodeConverter ?? (s_jsonNodeConverter = new JsonNodeConverter());
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<JsonObject> JsonObjectConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return s_jsonObjectConverter ?? (s_jsonObjectConverter = new JsonObjectConverter());
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<JsonValue> JsonValueConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return s_jsonValueConverter ?? (s_jsonValueConverter = new JsonValueConverter());
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<JsonDocument> JsonDocumentConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return s_jsonDocumentConverter ?? (s_jsonDocumentConverter = new JsonDocumentConverter());
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public static JsonConverter<Memory<byte>> MemoryByteConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
get {
return s_memoryByteConverter ?? (s_memoryByteConverter = new MemoryByteConverter());
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
public static JsonConverter<ReadOnlyMemory<byte>> ReadOnlyMemoryByteConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
get {
return s_readOnlyMemoryByteConverter ?? (s_readOnlyMemoryByteConverter = new ReadOnlyMemoryByteConverter());
}
}
[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 DefaultObjectConverter());
}
}
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());
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<string> StringConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return 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());
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<Uri> UriConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return s_uriConverter ?? (s_uriConverter = new UriConverter());
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
public static JsonConverter<Version> VersionConverter {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
return 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 CreateCore(options, collectionInfo, new ArrayConverter<TElement[], TElement>(), null, 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 CreateCore(options, collectionInfo, new ListOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new DictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>(), null, 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 CreateCore(options, collectionInfo, new ImmutableDictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>(), createRangeFunc, 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 CreateCore(options, collectionInfo, new IDictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>(), null, 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 CreateCore(options, collectionInfo, new IReadOnlyDictionaryOfTKeyTValueConverter<TCollection, TKey, TValue>(), null, 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 CreateCore(options, collectionInfo, new ImmutableEnumerableOfTConverter<TCollection, TElement>(), createRangeFunc, null);
}
public static JsonTypeInfo<TCollection> CreateIListInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IList
{
return CreateCore(options, collectionInfo, new IListConverter<TCollection>(), null, 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 CreateCore(options, collectionInfo, new IListOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new ISetOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new ICollectionOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new StackOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new QueueOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new ConcurrentStackOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new ConcurrentQueueOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new IEnumerableOfTConverter<TCollection, TElement>(), null, 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 CreateCore(options, collectionInfo, new IAsyncEnumerableOfTConverter<TCollection, TElement>(), null, null);
}
public static JsonTypeInfo<TCollection> CreateIDictionaryInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IDictionary
{
return CreateCore(options, collectionInfo, new IDictionaryConverter<TCollection>(), null, 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 CreateCore(options, collectionInfo, new StackOrQueueConverter<TCollection>(), null, addFunc);
}
public static JsonTypeInfo<TCollection> CreateIEnumerableInfo<[System.Runtime.CompilerServices.Nullable(0)] TCollection>(JsonSerializerOptions options, JsonCollectionInfoValues<TCollection> collectionInfo) where TCollection : IEnumerable
{
return CreateCore(options, collectionInfo, new IEnumerableConverter<TCollection>(), null, null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1
})]
public static JsonTypeInfo<Memory<TElement>> CreateMemoryInfo<[System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1
})] JsonCollectionInfoValues<Memory<TElement>> collectionInfo)
{
return CreateCore(options, collectionInfo, new MemoryConverter<TElement>(), null, null);
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1
})]
public static JsonTypeInfo<ReadOnlyMemory<TElement>> CreateReadOnlyMemoryInfo<[System.Runtime.CompilerServices.Nullable(2)] TElement>(JsonSerializerOptions options, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1
})] JsonCollectionInfoValues<ReadOnlyMemory<TElement>> collectionInfo)
{
return CreateCore(options, collectionInfo, new ReadOnlyMemoryConverter<TElement>(), null, null);
}
public static JsonConverter<T> GetUnsupportedTypeConverter<[System.Runtime.CompilerServices.Nullable(2)] T>()
{
return new UnsupportedTypeConverter<T>((string)null);
}
[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;
}
private static JsonTypeInfo<T> CreateCore<T>(JsonConverter converter, JsonSerializerOptions options)
{
JsonTypeInfo<T> jsonTypeInfo = new JsonTypeInfo<T>(converter, options);
jsonTypeInfo.PopulatePolymorphismMetadata();
jsonTypeInfo.MapInterfaceTypesToCallbacks();
converter.ConfigureJsonTypeInfo(jsonTypeInfo, options);
return jsonTypeInfo;
}
private static JsonTypeInfo<T> CreateCore<T>(JsonSerializerOptions options, JsonObjectInfoValues<T> objectInfo)
{
JsonConverter<T> converter = GetConverter(objectInfo);
JsonTypeInfo<T> jsonTypeInfo = new JsonTypeInfo<T>((JsonConverter)converter, options);
if (objectInfo.ObjectWithParameterizedConstructorCreator != null) {
jsonTypeInfo.CreateObjectWithArgs = objectInfo.ObjectWithParameterizedConstructorCreator;
PopulateParameterInfoValues(jsonTypeInfo, objectInfo.ConstructorParameterMetadataInitializer);
} else {
jsonTypeInfo.SetCreateObjectIfCompatible(objectInfo.ObjectCreator);
jsonTypeInfo.CreateObjectForExtensionDataProperty = ((JsonTypeInfo)jsonTypeInfo).CreateObject;
}
if (objectInfo.PropertyMetadataInitializer != null)
jsonTypeInfo.SourceGenDelayedPropertyInitializer = objectInfo.PropertyMetadataInitializer;
else
jsonTypeInfo.PropertyMetadataSerializationNotSupported = true;
jsonTypeInfo.SerializeHandler = objectInfo.SerializeHandler;
jsonTypeInfo.NumberHandling = objectInfo.NumberHandling;
jsonTypeInfo.PopulatePolymorphismMetadata();
jsonTypeInfo.MapInterfaceTypesToCallbacks();
converter.ConfigureJsonTypeInfo(jsonTypeInfo, options);
return jsonTypeInfo;
}
private static JsonTypeInfo<T> CreateCore<T>(JsonSerializerOptions options, JsonCollectionInfoValues<T> collectionInfo, JsonConverter<T> converter, object createObjectWithArgs = null, object addFunc = null)
{
if (collectionInfo == null)
ThrowHelper.ThrowArgumentNullException("collectionInfo");
converter = ((collectionInfo.SerializeHandler != null) ? new JsonMetadataServicesConverter<T>(converter) : converter);
JsonTypeInfo<T> jsonTypeInfo = new JsonTypeInfo<T>((JsonConverter)converter, options);
jsonTypeInfo.KeyTypeInfo = collectionInfo.KeyInfo;
jsonTypeInfo.ElementTypeInfo = collectionInfo.ElementInfo;
jsonTypeInfo.NumberHandling = collectionInfo.NumberHandling;
jsonTypeInfo.SerializeHandler = collectionInfo.SerializeHandler;
jsonTypeInfo.CreateObjectWithArgs = createObjectWithArgs;
jsonTypeInfo.AddMethodDelegate = addFunc;
jsonTypeInfo.SetCreateObjectIfCompatible(collectionInfo.ObjectCreator);
jsonTypeInfo.PopulatePolymorphismMetadata();
jsonTypeInfo.MapInterfaceTypesToCallbacks();
converter.ConfigureJsonTypeInfo(jsonTypeInfo, options);
return jsonTypeInfo;
}
private static JsonConverter<T> GetConverter<T>(JsonObjectInfoValues<T> objectInfo)
{
JsonConverter<T> jsonConverter = (objectInfo.ObjectWithParameterizedConstructorCreator != null) ? new LargeObjectWithParameterizedConstructorConverter<T>() : new ObjectDefaultConverter<T>();
if (objectInfo.SerializeHandler == null)
return jsonConverter;
return new JsonMetadataServicesConverter<T>(jsonConverter);
}
private static void PopulateParameterInfoValues(JsonTypeInfo typeInfo, Func<JsonParameterInfoValues[]> paramFactory)
{
JsonParameterInfoValues[] array = paramFactory?.Invoke();
if (array != null)
typeInfo.ParameterInfoValues = array;
else
typeInfo.PropertyMetadataSerializationNotSupported = true;
}
internal static void PopulateProperties(JsonTypeInfo typeInfo, JsonTypeInfo.JsonPropertyInfoList propertyList, Func<JsonSerializerContext, JsonPropertyInfo[]> propInitFunc)
{
JsonSerializerContext arg = typeInfo.Options.TypeInfoResolver as JsonSerializerContext;
JsonPropertyInfo[] array = propInitFunc(arg);
JsonTypeInfo.PropertyHierarchyResolutionState state = new JsonTypeInfo.PropertyHierarchyResolutionState(typeInfo.Options);
JsonPropertyInfo[] array2 = array;
foreach (JsonPropertyInfo jsonPropertyInfo in array2) {
if (!jsonPropertyInfo.SrcGen_IsPublic) {
if (jsonPropertyInfo.SrcGen_HasJsonInclude)
ThrowHelper.ThrowInvalidOperationException_JsonIncludeOnInaccessibleProperty(jsonPropertyInfo.MemberName, jsonPropertyInfo.DeclaringType);
} else if (jsonPropertyInfo.MemberType != MemberTypes.Field || jsonPropertyInfo.SrcGen_HasJsonInclude || typeInfo.Options.IncludeFields) {
propertyList.AddPropertyWithConflictResolution(jsonPropertyInfo, ref state);
}
}
if (state.IsPropertyOrderSpecified)
propertyList.SortProperties();
}
private static JsonPropertyInfo<T> CreatePropertyInfoCore<T>(JsonPropertyInfoValues<T> propertyInfoValues, JsonSerializerOptions options)
{
JsonPropertyInfo<T> jsonPropertyInfo = new JsonPropertyInfo<T>(propertyInfoValues.DeclaringType, (JsonTypeInfo)null, options);
DeterminePropertyName(jsonPropertyInfo, propertyInfoValues.PropertyName, propertyInfoValues.JsonPropertyName);
jsonPropertyInfo.MemberName = propertyInfoValues.PropertyName;
jsonPropertyInfo.MemberType = (propertyInfoValues.IsProperty ? MemberTypes.Property : MemberTypes.Field);
jsonPropertyInfo.SrcGen_IsPublic = propertyInfoValues.IsPublic;
jsonPropertyInfo.SrcGen_HasJsonInclude = propertyInfoValues.HasJsonInclude;
jsonPropertyInfo.IsExtensionData = propertyInfoValues.IsExtensionData;
jsonPropertyInfo.CustomConverter = propertyInfoValues.Converter;
if (jsonPropertyInfo.IgnoreCondition.GetValueOrDefault() != JsonIgnoreCondition.Always) {
jsonPropertyInfo.Get = propertyInfoValues.Getter;
jsonPropertyInfo.Set = propertyInfoValues.Setter;
}
jsonPropertyInfo.IgnoreCondition = propertyInfoValues.IgnoreCondition;
jsonPropertyInfo.JsonTypeInfo = propertyInfoValues.PropertyTypeInfo;
jsonPropertyInfo.NumberHandling = propertyInfoValues.NumberHandling;
return jsonPropertyInfo;
}
private static void DeterminePropertyName(JsonPropertyInfo propertyInfo, string declaredPropertyName, string declaredJsonPropertyName)
{
string text = (declaredJsonPropertyName != null) ? declaredJsonPropertyName : ((propertyInfo.Options.PropertyNamingPolicy != null) ? propertyInfo.Options.PropertyNamingPolicy.ConvertName(declaredPropertyName) : declaredPropertyName);
if (text == null)
ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameNull(propertyInfo);
propertyInfo.Name = text;
}
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 CreatePropertyInfoCore(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 CreateCore(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 CreateCore<T>(converter, options);
}
}
}