#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
        /// <summary>
        /// Creates serialization metadata for <see cref="System.Collections.Immutable.ImmutableDictionary{TKey, TValue}"/> and
        /// types assignable to <see cref="System.Collections.Immutable.IImmutableDictionary{TKey, TValue}"/>.
        /// </summary>
        /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
        /// <typeparam name="TKey">The generic definition of the key type.</typeparam>
        /// <typeparam name="TValue">The generic definition of the value type.</typeparam>
        /// <param name="options"></param>
        /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
        /// <param name="createRangeFunc">A method to create an immutable dictionary instance.</param>
        /// <returns>Serialization metadata for the given type.</returns>
        /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
        public static JsonTypeInfo <TCollection> CreateImmutableDictionaryInfo <TCollection, TKey, TValue>(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <TCollection> collectionInfo,
            Func <IEnumerable <KeyValuePair <TKey, TValue> >, TCollection> createRangeFunc)
            where TCollection : IReadOnlyDictionary <TKey, TValue>
            where TKey : notnull
        => new JsonTypeInfoInternal <TCollection>(
Example #2
0
        /// <summary>
        /// Creates serialization metadata for a collection.
        /// </summary>
        public SourceGenJsonTypeInfo(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <T> collectionInfo,
            Func <JsonConverter <T> > converterCreator,
            object?createObjectWithArgs = null,
            object?addFunc = null)
            : base(new JsonMetadataServicesConverter <T>(converterCreator()), options)
        {
            if (collectionInfo is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(collectionInfo));
            }

            KeyTypeInfo     = collectionInfo.KeyInfo;
            ElementTypeInfo = collectionInfo.ElementInfo;
            Debug.Assert(Kind != JsonTypeInfoKind.None);
            NumberHandling       = collectionInfo.NumberHandling;
            SerializeHandler     = collectionInfo.SerializeHandler;
            CreateObjectWithArgs = createObjectWithArgs;
            AddMethodDelegate    = addFunc;
            SetCreateObjectIfCompatible(collectionInfo.ObjectCreator);
            PopulatePolymorphismMetadata();
            MapInterfaceTypesToCallbacks();

            // Plug in any converter configuration -- should be run last.
            Converter.ConfigureJsonTypeInfo(this, options);
        }
Example #3
0
        /// <summary>
        /// Creates serialization metadata for a collection.
        /// </summary>
        public JsonTypeInfoInternal(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <T> collectionInfo,
            Func <JsonConverter <T> > converterCreator,
            object?createObjectWithArgs = null,
            object?addFunc = null)
            : base(typeof(T), options)
        {
            if (collectionInfo == null)
            {
                throw new ArgumentNullException(nameof(collectionInfo));
            }

            ConverterStrategy strategy  = collectionInfo.KeyInfo == null ? ConverterStrategy.Enumerable : ConverterStrategy.Dictionary;
            JsonConverter <T> converter = new JsonMetadataServicesConverter <T>(converterCreator, strategy);

            KeyType                 = converter.KeyType;
            ElementType             = converter.ElementType;
            KeyTypeInfo             = collectionInfo.KeyInfo;
            ElementTypeInfo         = collectionInfo.ElementInfo ?? throw new ArgumentNullException(nameof(collectionInfo.ElementInfo));
            NumberHandling          = collectionInfo.NumberHandling;
            PropertyInfoForTypeInfo = JsonMetadataServices.CreateJsonPropertyInfoForClassInfo(typeof(T), this, converter, options);
            SerializeHandler        = collectionInfo.SerializeHandler;
            CreateObjectWithArgs    = createObjectWithArgs;
            AddMethodDelegate       = addFunc;
            SetCreateObjectFunc(collectionInfo.ObjectCreator);
        }
Example #4
0
 /// <summary>
 /// Creates serialization metadata for types assignable to <see cref="Queue{T}"/>.
 /// </summary>
 /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
 /// <typeparam name="TElement">The generic definition of the element type.</typeparam>
 /// <param name="options"></param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TCollection> CreateConcurrentQueueInfo <TCollection, TElement>(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <TCollection> collectionInfo)
     where TCollection : ConcurrentQueue <TElement>
 => new SourceGenJsonTypeInfo <TCollection>(
     options,
     collectionInfo,
     () => new ConcurrentQueueOfTConverter <TCollection, TElement>());
 /// <summary>
 /// Creates serialization metadata for types assignable to <see cref="List{T}"/>.
 /// </summary>
 /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
 /// <typeparam name="TElement">The generic definition of the element type.</typeparam>
 /// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TCollection> CreateListInfo <TCollection, TElement>(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <TCollection> collectionInfo)
     where TCollection : List <TElement>
 => new JsonTypeInfoInternal <TCollection>(
     options,
     collectionInfo,
     () => new ListOfTConverter <TCollection, TElement>());
Example #6
0
 /// <summary>
 /// Creates serialization metadata for types assignable to <see cref="IDictionary"/>.
 /// </summary>
 /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
 /// <param name="options"></param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TCollection> CreateIDictionaryInfo <TCollection>(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <TCollection> collectionInfo)
     where TCollection : IDictionary
 => new SourceGenJsonTypeInfo <TCollection>(
     options,
     collectionInfo,
     () => new IDictionaryConverter <TCollection>());
Example #7
0
 /// <summary>
 /// Creates serialization metadata for types assignable to <see cref="IList"/>.
 /// </summary>
 /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
 /// <param name="options"></param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TCollection> CreateIEnumerableInfo <TCollection>(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <TCollection> collectionInfo)
     where TCollection : IEnumerable
 => new SourceGenJsonTypeInfo <TCollection>(
     options,
     collectionInfo,
     () => new IEnumerableConverter <TCollection>());
Example #8
0
 /// <summary>
 /// Creates serialization metadata for types assignable to <see cref="Stack{T}"/>.
 /// </summary>
 /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
 /// <typeparam name="TElement">The generic definition of the element type.</typeparam>
 /// <param name="options"></param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TCollection> CreateStackInfo <TCollection, TElement>(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <TCollection> collectionInfo)
     where TCollection : Stack <TElement>
 => new SourceGenJsonTypeInfo <TCollection>(
     options,
     collectionInfo,
     () => new StackOfTConverter <TCollection, TElement>());
 /// <summary>
 /// Creates serialization metadata for types assignable to <see cref="IAsyncEnumerable{T}"/>.
 /// </summary>
 /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
 /// <typeparam name="TElement">The generic definition of the element type.</typeparam>
 /// <param name="options"></param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TCollection> CreateIAsyncEnumerableInfo <TCollection, TElement>(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <TCollection> collectionInfo)
     where TCollection : IAsyncEnumerable <TElement>
 => new SourceGenJsonTypeInfo <TCollection>(
     options,
     collectionInfo,
     () => new IAsyncEnumerableOfTConverter <TCollection, TElement>());
 /// <summary>
 /// Creates serialization metadata for types assignable to <see cref="Dictionary{TKey, TValue}"/>.
 /// </summary>
 /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
 /// <typeparam name="TKey">The generic definition of the key type.</typeparam>
 /// <typeparam name="TValue">The generic definition of the value type.</typeparam>
 /// <param name="options"></param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TCollection> CreateDictionaryInfo <TCollection, TKey, TValue>(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <TCollection> collectionInfo)
     where TCollection : Dictionary <TKey, TValue>
     where TKey : notnull
 => new JsonTypeInfoInternal <TCollection>(
     options,
     collectionInfo,
     () => new DictionaryOfTKeyTValueConverter <TCollection, TKey, TValue>());
Example #11
0
 /// <summary>
 /// Creates serialization metadata for types assignable to <see cref="IReadOnlyDictionary{TKey, TValue}"/>.
 /// </summary>
 /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
 /// <typeparam name="TKey">The generic definition of the key type.</typeparam>
 /// <typeparam name="TValue">The generic definition of the value type.</typeparam>
 /// <param name="options"></param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TCollection> CreateIReadOnlyDictionaryInfo <TCollection, TKey, TValue>(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <TCollection> collectionInfo)
     where TCollection : IReadOnlyDictionary <TKey, TValue>
     where TKey : notnull
 => new SourceGenJsonTypeInfo <TCollection>(
     options,
     collectionInfo,
     () => new IReadOnlyDictionaryOfTKeyTValueConverter <TCollection, TKey, TValue>());
Example #12
0
#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
        /// <summary>
        /// Creates serialization metadata for non-dictionary immutable collection types.
        /// </summary>
        /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
        /// <typeparam name="TElement">The generic definition of the element type.</typeparam>
        /// <param name="options"></param>
        /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
        /// <param name="createRangeFunc">A method to create an immutable dictionary instance.</param>
        /// <returns>Serialization metadata for the given type.</returns>
        /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
        public static JsonTypeInfo <TCollection> CreateImmutableEnumerableInfo <TCollection, TElement>(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <TCollection> collectionInfo,
            Func <IEnumerable <TElement>, TCollection> createRangeFunc)
            where TCollection : IEnumerable <TElement>
        {
            if (createRangeFunc is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(createRangeFunc));
            }

            return(new SourceGenJsonTypeInfo <TCollection>(
                       options,
                       collectionInfo,
                       () => new ImmutableEnumerableOfTConverter <TCollection, TElement>(),
                       createObjectWithArgs: createRangeFunc));
        }
Example #13
0
#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
        /// <summary>
        /// Creates serialization metadata for <see cref="System.Collections.Immutable.ImmutableDictionary{TKey, TValue}"/> and
        /// types assignable to <see cref="System.Collections.Immutable.IImmutableDictionary{TKey, TValue}"/>.
        /// </summary>
        /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
        /// <typeparam name="TKey">The generic definition of the key type.</typeparam>
        /// <typeparam name="TValue">The generic definition of the value type.</typeparam>
        /// <param name="options"></param>
        /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
        /// <param name="createRangeFunc">A method to create an immutable dictionary instance.</param>
        /// <returns>Serialization metadata for the given type.</returns>
        /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
        public static JsonTypeInfo <TCollection> CreateImmutableDictionaryInfo <TCollection, TKey, TValue>(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <TCollection> collectionInfo,
            Func <IEnumerable <KeyValuePair <TKey, TValue> >, TCollection> createRangeFunc)
            where TCollection : IReadOnlyDictionary <TKey, TValue>
            where TKey : notnull
        {
            if (createRangeFunc is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(createRangeFunc));
            }

            return(new SourceGenJsonTypeInfo <TCollection>(
                       options,
                       collectionInfo,
                       () => new ImmutableDictionaryOfTKeyTValueConverter <TCollection, TKey, TValue>(),
                       createObjectWithArgs: createRangeFunc));
        }
Example #14
0
        private static JsonTypeInfo <TCollection> CreateStackOrQueueInfo <TCollection>(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <TCollection> collectionInfo,
            Action <TCollection, object?> addFunc)
            where TCollection : IEnumerable
        {
            if (addFunc is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(addFunc));
            }

            return(new SourceGenJsonTypeInfo <TCollection>(
                       options,
                       collectionInfo,
                       () => new StackOrQueueConverter <TCollection>(),
                       createObjectWithArgs: null,
                       addFunc: addFunc));
        }
Example #15
0
        /// <summary>
        /// Creates serialization metadata for a collection.
        /// </summary>
        public SourceGenJsonTypeInfo(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <T> collectionInfo,
            Func <JsonConverter <T> > converterCreator,
            object?createObjectWithArgs = null,
            object?addFunc = null)
            : base(GetConverter(collectionInfo, converterCreator), options)
        {
            if (collectionInfo is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(collectionInfo));
            }

            KeyTypeInfo          = collectionInfo.KeyInfo;
            ElementTypeInfo      = collectionInfo.ElementInfo ?? throw new ArgumentNullException(nameof(collectionInfo.ElementInfo));
            NumberHandling       = collectionInfo.NumberHandling;
            SerializeHandler     = collectionInfo.SerializeHandler;
            CreateObjectWithArgs = createObjectWithArgs;
            AddMethodDelegate    = addFunc;
            SetCreateObjectFunc(collectionInfo.ObjectCreator);
        }
Example #16
0
        /// <summary>
        /// Creates serialization metadata for a collection.
        /// </summary>
        public SourceGenJsonTypeInfo(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <T> collectionInfo,
            Func <JsonConverter <T> > converterCreator,
            object?createObjectWithArgs = null,
            object?addFunc = null)
            : this(new JsonMetadataServicesConverter <T>(converterCreator()), options)
        {
            if (collectionInfo is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(collectionInfo));
            }

            KeyTypeInfo     = collectionInfo.KeyInfo;
            ElementTypeInfo = collectionInfo.ElementInfo;
            Debug.Assert(Kind != JsonTypeInfoKind.None);
            NumberHandling       = collectionInfo.NumberHandling;
            SerializeHandler     = collectionInfo.SerializeHandler;
            CreateObjectWithArgs = createObjectWithArgs;
            AddMethodDelegate    = addFunc;
            CreateObject         = collectionInfo.ObjectCreator;
        }
 /// <summary>
 /// Creates serialization metadata for an array.
 /// </summary>
 /// <typeparam name="TElement">The generic definition of the element type.</typeparam>
 /// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param>
 /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
 /// <returns>Serialization metadata for the given type.</returns>
 /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
 public static JsonTypeInfo <TElement[]> CreateArrayInfo <TElement>(JsonSerializerOptions options, JsonCollectionInfoValues <TElement[]> collectionInfo)
 => new JsonTypeInfoInternal <TElement[]>(
     options,
     collectionInfo,
     () => new ArrayConverter <TElement[], TElement>());
Example #18
0
 /// <summary>
 /// Creates serialization metadata for a collection.
 /// </summary>
 public JsonTypeInfoInternal(
     JsonSerializerOptions options,
     JsonCollectionInfoValues <T> collectionInfo !!,
#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
        /// <summary>
        /// Creates serialization metadata for <see cref="System.Collections.Immutable.ImmutableDictionary{TKey, TValue}"/> and
        /// types assignable to <see cref="System.Collections.Immutable.IImmutableDictionary{TKey, TValue}"/>.
        /// </summary>
        /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
        /// <typeparam name="TKey">The generic definition of the key type.</typeparam>
        /// <typeparam name="TValue">The generic definition of the value type.</typeparam>
        /// <param name="options"></param>
        /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
        /// <param name="createRangeFunc">A method to create an immutable dictionary instance.</param>
        /// <returns>Serialization metadata for the given type.</returns>
        /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
        public static JsonTypeInfo <TCollection> CreateImmutableDictionaryInfo <TCollection, TKey, TValue>(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <TCollection> collectionInfo,
            Func <IEnumerable <KeyValuePair <TKey, TValue> >, TCollection> createRangeFunc !!)
Example #20
0
#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
        /// <summary>
        /// Creates serialization metadata for <see cref="System.Collections.Queue"/> types.
        /// </summary>
        /// <typeparam name="TCollection">The generic definition of the type.</typeparam>
        /// <param name="options"></param>
        /// <param name="collectionInfo">Provides serialization metadata about the collection type.</param>
        /// <param name="addFunc">A method for adding elements to the collection when using the serializer's code-paths.</param>
        /// <returns>Serialization metadata for the given type.</returns>
        /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
        public static JsonTypeInfo <TCollection> CreateQueueInfo <TCollection>(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <TCollection> collectionInfo,
            Action <TCollection, object?> addFunc)
            where TCollection : IEnumerable
        => CreateStackOrQueueInfo(options, collectionInfo, addFunc);
Example #21
0
        private static JsonConverter GetConverter(JsonCollectionInfoValues <T> collectionInfo, Func <JsonConverter <T> > converterCreator)
        {
            ConverterStrategy strategy = collectionInfo.KeyInfo == null ? ConverterStrategy.Enumerable : ConverterStrategy.Dictionary;

            return(new JsonMetadataServicesConverter <T>(converterCreator, strategy));
        }