| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Text.Json.Serialization.Metadata; |
| | | 6 | | |
| | | 7 | | namespace 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 |
| | 0 | 26 | | { |
| | 0 | 27 | | JsonSerializerOptions? options = _options; |
| | | 28 | | |
| | 0 | 29 | | if (options is null) |
| | 0 | 30 | | { |
| | 0 | 31 | | options = new JsonSerializerOptions { TypeInfoResolver = this }; |
| | 0 | 32 | | options.MakeReadOnly(); |
| | 0 | 33 | | _options = options; |
| | 0 | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | return options; |
| | 0 | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | internal void AssociateWithOptions(JsonSerializerOptions options) |
| | 0 | 41 | | { |
| | 0 | 42 | | Debug.Assert(!options.IsReadOnly); |
| | 0 | 43 | | options.TypeInfoResolver = this; |
| | 0 | 44 | | options.MakeReadOnly(); |
| | 0 | 45 | | _options = options; |
| | 0 | 46 | | } |
| | | 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) |
| | 0 | 53 | | { |
| | 0 | 54 | | Debug.Assert(options != null); |
| | | 55 | | |
| | 0 | 56 | | JsonSerializerOptions? generatedSerializerOptions = GeneratedSerializerOptions; |
| | | 57 | | |
| | 0 | 58 | | return |
| | 0 | 59 | | generatedSerializerOptions is not null && |
| | 0 | 60 | | // Guard against unsupported features |
| | 0 | 61 | | options.Converters.Count == 0 && |
| | 0 | 62 | | options.Encoder is null && |
| | 0 | 63 | | // Disallow custom number handling we'd need to honor when writing. |
| | 0 | 64 | | // AllowReadingFromString and Strict are fine since there's no action to take when writing. |
| | 0 | 65 | | !JsonHelpers.RequiresSpecialNumberHandlingOnWrite(options.NumberHandling) && |
| | 0 | 66 | | options.ReferenceHandlingStrategy == JsonKnownReferenceHandler.Unspecified && |
| | 0 | 67 | | #pragma warning disable SYSLIB0020 |
| | 0 | 68 | | !options.IgnoreNullValues && // This property is obsolete. |
| | 0 | 69 | | #pragma warning restore SYSLIB0020 |
| | 0 | 70 | | |
| | 0 | 71 | | // Ensure options values are consistent with expected defaults. |
| | 0 | 72 | | options.DefaultIgnoreCondition == generatedSerializerOptions.DefaultIgnoreCondition && |
| | 0 | 73 | | options.RespectNullableAnnotations == generatedSerializerOptions.RespectNullableAnnotations && |
| | 0 | 74 | | options.IgnoreReadOnlyFields == generatedSerializerOptions.IgnoreReadOnlyFields && |
| | 0 | 75 | | options.IgnoreReadOnlyProperties == generatedSerializerOptions.IgnoreReadOnlyProperties && |
| | 0 | 76 | | options.IncludeFields == generatedSerializerOptions.IncludeFields && |
| | 0 | 77 | | options.PropertyNamingPolicy == generatedSerializerOptions.PropertyNamingPolicy && |
| | 0 | 78 | | options.DictionaryKeyPolicy is null; |
| | 0 | 79 | | } |
| | | 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> |
| | 0 | 94 | | protected JsonSerializerContext(JsonSerializerOptions? options) |
| | 0 | 95 | | { |
| | 0 | 96 | | if (options != null) |
| | 0 | 97 | | { |
| | 0 | 98 | | options.VerifyMutable(); |
| | 0 | 99 | | AssociateWithOptions(options); |
| | 0 | 100 | | } |
| | 0 | 101 | | } |
| | | 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) |
| | 0 | 111 | | { |
| | 0 | 112 | | if (options != null && options != _options) |
| | 0 | 113 | | { |
| | 0 | 114 | | ThrowHelper.ThrowInvalidOperationException_ResolverTypeInfoOptionsNotCompatible(); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | return GetTypeInfo(type); |
| | 0 | 118 | | } |
| | | 119 | | } |
| | | 120 | | } |