< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 55
Coverable lines: 55
Total lines: 120
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 32
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\JsonSerializerContext.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Text.Json.Serialization.Metadata;
 6
 7namespace System.Text.Json.Serialization
 8{
 9    /// <summary>
 10    /// Provides metadata about a set of types that is relevant to JSON serialization.
 11    /// </summary>
 12    public abstract partial class JsonSerializerContext : IJsonTypeInfoResolver, IBuiltInJsonTypeInfoResolver
 13    {
 14        private JsonSerializerOptions? _options;
 15
 16        /// <summary>
 17        /// Gets the run time specified options of the context. If no options were passed
 18        /// when instantiating the context, then a new instance is bound and returned.
 19        /// </summary>
 20        /// <remarks>
 21        /// The options instance cannot be mutated once it is bound to the context instance.
 22        /// </remarks>
 23        public JsonSerializerOptions Options
 24        {
 25            get
 026            {
 027                JsonSerializerOptions? options = _options;
 28
 029                if (options is null)
 030                {
 031                    options = new JsonSerializerOptions { TypeInfoResolver = this };
 032                    options.MakeReadOnly();
 033                    _options = options;
 034                }
 35
 036                return options;
 037            }
 38        }
 39
 40        internal void AssociateWithOptions(JsonSerializerOptions options)
 041        {
 042            Debug.Assert(!options.IsReadOnly);
 043            options.TypeInfoResolver = this;
 044            options.MakeReadOnly();
 045            _options = options;
 046        }
 47
 48        /// <summary>
 49        /// Indicates whether pre-generated serialization logic for types in the context
 50        /// is compatible with the run time specified <see cref="JsonSerializerOptions"/>.
 51        /// </summary>
 52        bool IBuiltInJsonTypeInfoResolver.IsCompatibleWithOptions(JsonSerializerOptions options)
 053        {
 054            Debug.Assert(options != null);
 55
 056            JsonSerializerOptions? generatedSerializerOptions = GeneratedSerializerOptions;
 57
 058            return
 059                generatedSerializerOptions is not null &&
 060                // Guard against unsupported features
 061                options.Converters.Count == 0 &&
 062                options.Encoder is null &&
 063                // Disallow custom number handling we'd need to honor when writing.
 064                // AllowReadingFromString and Strict are fine since there's no action to take when writing.
 065                !JsonHelpers.RequiresSpecialNumberHandlingOnWrite(options.NumberHandling) &&
 066                options.ReferenceHandlingStrategy == JsonKnownReferenceHandler.Unspecified &&
 067#pragma warning disable SYSLIB0020
 068                !options.IgnoreNullValues && // This property is obsolete.
 069#pragma warning restore SYSLIB0020
 070
 071                // Ensure options values are consistent with expected defaults.
 072                options.DefaultIgnoreCondition == generatedSerializerOptions.DefaultIgnoreCondition &&
 073                options.RespectNullableAnnotations == generatedSerializerOptions.RespectNullableAnnotations &&
 074                options.IgnoreReadOnlyFields == generatedSerializerOptions.IgnoreReadOnlyFields &&
 075                options.IgnoreReadOnlyProperties == generatedSerializerOptions.IgnoreReadOnlyProperties &&
 076                options.IncludeFields == generatedSerializerOptions.IncludeFields &&
 077                options.PropertyNamingPolicy == generatedSerializerOptions.PropertyNamingPolicy &&
 078                options.DictionaryKeyPolicy is null;
 079        }
 80
 81        /// <summary>
 82        /// The default run time options for the context. Its values are defined at design-time via <see cref="JsonSourc
 83        /// </summary>
 84        protected abstract JsonSerializerOptions? GeneratedSerializerOptions { get; }
 85
 86        /// <summary>
 87        /// Creates an instance of <see cref="JsonSerializerContext"/> and binds it with the indicated <see cref="JsonSe
 88        /// </summary>
 89        /// <param name="options">The run time provided options for the context instance.</param>
 90        /// <remarks>
 91        /// If no instance options are passed, then no options are set until the context is bound using <see cref="JsonS
 92        /// or until <see cref="Options"/> is called, where a new options instance is created and bound.
 93        /// </remarks>
 094        protected JsonSerializerContext(JsonSerializerOptions? options)
 095        {
 096            if (options != null)
 097            {
 098                options.VerifyMutable();
 099                AssociateWithOptions(options);
 0100            }
 0101        }
 102
 103        /// <summary>
 104        /// Returns a <see cref="JsonTypeInfo"/> instance representing the given type.
 105        /// </summary>
 106        /// <param name="type">The type to fetch metadata about.</param>
 107        /// <returns>The metadata for the specified type, or <see langword="null" /> if the context has no metadata for 
 108        public abstract JsonTypeInfo? GetTypeInfo(Type type);
 109
 110        JsonTypeInfo? IJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options)
 0111        {
 0112            if (options != null && options != _options)
 0113            {
 0114                ThrowHelper.ThrowInvalidOperationException_ResolverTypeInfoOptionsNotCompatible();
 115            }
 116
 0117            return GetTypeInfo(type);
 0118        }
 119    }
 120}