示例#1
0
        /// <summary>
        /// Copies the options from a <see cref="JsonSerializerOptions"/> instance to a new instance.
        /// </summary>
        /// <param name="options">The <see cref="JsonSerializerOptions"/> instance to copy options from.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="options"/> is <see langword="null"/>.
        /// </exception>
        public JsonSerializerOptions(JsonSerializerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _memberAccessorStrategy   = options._memberAccessorStrategy;
            _dictionaryKeyPolicy      = options._dictionaryKeyPolicy;
            _jsonPropertyNamingPolicy = options._jsonPropertyNamingPolicy;
            _readCommentHandling      = options._readCommentHandling;
            _referenceHandler         = options._referenceHandler;
            _encoder = options._encoder;
            _defaultIgnoreCondition = options._defaultIgnoreCondition;

            _defaultBufferSize           = options._defaultBufferSize;
            _maxDepth                    = options._maxDepth;
            _allowTrailingCommas         = options._allowTrailingCommas;
            _ignoreNullValues            = options._ignoreNullValues;
            _ignoreReadOnlyProperties    = options._ignoreReadOnlyProperties;
            _ignoreReadonlyFields        = options._ignoreReadonlyFields;
            _includeFields               = options._includeFields;
            _propertyNameCaseInsensitive = options._propertyNameCaseInsensitive;
            _writeIndented               = options._writeIndented;

            Converters        = new ConverterList(this, (ConverterList)options.Converters);
            EffectiveMaxDepth = options.EffectiveMaxDepth;

            // _classes is not copied as sharing the JsonClassInfo and JsonPropertyInfo caches can result in
            // unnecessary references to type metadata, potentially hindering garbage collection on the source options.

            // _haveTypesBeenCreated is not copied; it's okay to make changes to this options instance as (de)serialization has not occurred.
        }
示例#2
0
        /// <summary>
        /// Copies the options from a <see cref="JsonSerializerOptions"/> instance to a new instance.
        /// </summary>
        /// <param name="options">The <see cref="JsonSerializerOptions"/> instance to copy options from.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="options"/> is <see langword="null"/>.
        /// </exception>
        public JsonSerializerOptions(JsonSerializerOptions options)
        {
            if (options is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(options));
            }

            _memberAccessorStrategy   = options._memberAccessorStrategy;
            _dictionaryKeyPolicy      = options._dictionaryKeyPolicy;
            _jsonPropertyNamingPolicy = options._jsonPropertyNamingPolicy;
            _readCommentHandling      = options._readCommentHandling;
            _referenceHandler         = options._referenceHandler;
            _converters             = new ConverterList(this, options._converters);
            _encoder                = options._encoder;
            _defaultIgnoreCondition = options._defaultIgnoreCondition;
            _numberHandling         = options._numberHandling;
            _unknownTypeHandling    = options._unknownTypeHandling;

            _defaultBufferSize           = options._defaultBufferSize;
            _maxDepth                    = options._maxDepth;
            _allowTrailingCommas         = options._allowTrailingCommas;
            _ignoreNullValues            = options._ignoreNullValues;
            _ignoreReadOnlyProperties    = options._ignoreReadOnlyProperties;
            _ignoreReadonlyFields        = options._ignoreReadonlyFields;
            _includeFields               = options._includeFields;
            _propertyNameCaseInsensitive = options._propertyNameCaseInsensitive;
            _writeIndented               = options._writeIndented;
            // Preserve backward compatibility with .NET 6
            // This should almost certainly be changed, cf. https://github.com/dotnet/aspnetcore/issues/38720
            _typeInfoResolver         = options._typeInfoResolver is JsonSerializerContext ? null : options._typeInfoResolver;
            EffectiveMaxDepth         = options.EffectiveMaxDepth;
            ReferenceHandlingStrategy = options.ReferenceHandlingStrategy;

            // _cachingContext is not copied as sharing the JsonTypeInfo and JsonPropertyInfo caches can result in
            // unnecessary references to type metadata, potentially hindering garbage collection on the source options.

            TrackOptionsInstance(this);
        }