| | | 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.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Reflection; |
| | | 7 | | using System.Text.Json.Schema; |
| | | 8 | | using System.Text.Json.Serialization.Converters; |
| | | 9 | | using System.Text.Json.Serialization.Metadata; |
| | | 10 | | |
| | | 11 | | namespace System.Text.Json.Serialization |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Converts an object or value to or from JSON. |
| | | 15 | | /// </summary> |
| | | 16 | | public abstract partial class JsonConverter |
| | | 17 | | { |
| | 3152 | 18 | | internal JsonConverter() |
| | 3152 | 19 | | { |
| | 3152 | 20 | | IsInternalConverter = GetType().Assembly == typeof(JsonConverter).Assembly; |
| | 3152 | 21 | | ConverterStrategy = GetDefaultConverterStrategy(); |
| | 3152 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the type being converted by the current converter instance. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <remarks> |
| | | 28 | | /// For instances of type <see cref="JsonConverter{T}"/> returns typeof(T), |
| | | 29 | | /// and for instances of type <see cref="JsonConverterFactory"/> returns <see langword="null" />. |
| | | 30 | | /// </remarks> |
| | | 31 | | public abstract Type? Type { get; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Determines whether the type can be converted. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="typeToConvert">The type is checked as to whether it can be converted.</param> |
| | | 37 | | /// <returns>True if the type can be converted, false otherwise.</returns> |
| | | 38 | | public abstract bool CanConvert(Type typeToConvert); |
| | | 39 | | |
| | | 40 | | internal ConverterStrategy ConverterStrategy |
| | | 41 | | { |
| | 73139 | 42 | | get => _converterStrategy; |
| | | 43 | | init |
| | 3152 | 44 | | { |
| | 3152 | 45 | | CanUseDirectReadOrWrite = value == ConverterStrategy.Value && IsInternalConverter; |
| | 3152 | 46 | | RequiresReadAhead = value == ConverterStrategy.Value; |
| | 3152 | 47 | | _converterStrategy = value; |
| | 3152 | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | private ConverterStrategy _converterStrategy; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Invoked by the base contructor to populate the initial value of the <see cref="ConverterStrategy"/> property |
| | | 55 | | /// Used for declaring the default strategy for specific converter hierarchies without explicitly setting in a c |
| | | 56 | | /// </summary> |
| | | 57 | | private protected abstract ConverterStrategy GetDefaultConverterStrategy(); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Indicates that the converter can consume the <see cref="JsonTypeInfo.CreateObject"/> delegate. |
| | | 61 | | /// Needed because certain collection converters cannot support arbitrary delegates. |
| | | 62 | | /// TODO remove once https://github.com/dotnet/runtime/pull/73395/ and |
| | | 63 | | /// https://github.com/dotnet/runtime/issues/71944 have been addressed. |
| | | 64 | | /// </summary> |
| | 9953 | 65 | | internal virtual bool SupportsCreateObjectDelegate => false; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Indicates that the converter is compatible with <see cref="JsonObjectCreationHandling.Populate"/>. |
| | | 69 | | /// </summary> |
| | 0 | 70 | | internal virtual bool CanPopulate => false; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Can direct Read or Write methods be called (for performance). |
| | | 74 | | /// </summary> |
| | 3152 | 75 | | internal bool CanUseDirectReadOrWrite { get; set; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// The converter supports writing and reading metadata. |
| | | 79 | | /// </summary> |
| | 0 | 80 | | internal virtual bool CanHaveMetadata => false; |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// The converter supports polymorphic writes; only reserved for System.Object types. |
| | | 84 | | /// </summary> |
| | 2640 | 85 | | internal bool CanBePolymorphic { get; init; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// The serializer must read ahead all contents of the next JSON value |
| | | 89 | | /// before calling into the converter for deserialization. |
| | | 90 | | /// </summary> |
| | 59299 | 91 | | internal bool RequiresReadAhead { get; private protected set; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Whether the converter is a special root-level value streaming converter. |
| | | 95 | | /// </summary> |
| | 55924 | 96 | | internal bool IsRootLevelMultiContentStreamingConverter { get; init; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Used to support JsonObject as an extension property in a loosely-typed, trimmable manner. |
| | | 100 | | /// </summary> |
| | | 101 | | internal virtual void ReadElementAndSetProperty( |
| | | 102 | | object obj, |
| | | 103 | | string propertyName, |
| | | 104 | | ref Utf8JsonReader reader, |
| | | 105 | | JsonSerializerOptions options, |
| | | 106 | | scoped ref ReadStack state) |
| | 0 | 107 | | { |
| | 0 | 108 | | Debug.Fail("Should not be reachable."); |
| | | 109 | | |
| | | 110 | | throw new InvalidOperationException(); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | internal virtual JsonTypeInfo CreateJsonTypeInfo(JsonSerializerOptions options) |
| | 0 | 114 | | { |
| | 0 | 115 | | Debug.Fail("Should not be reachable."); |
| | | 116 | | |
| | | 117 | | throw new InvalidOperationException(); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | internal JsonConverter<TTarget> CreateCastingConverter<TTarget>() |
| | 12254 | 121 | | { |
| | 12254 | 122 | | Debug.Assert(this is not JsonConverterFactory); |
| | | 123 | | |
| | 12254 | 124 | | if (this is JsonConverter<TTarget> conv) |
| | 12254 | 125 | | { |
| | 12254 | 126 | | return conv; |
| | | 127 | | } |
| | | 128 | | else |
| | 0 | 129 | | { |
| | 0 | 130 | | JsonSerializerOptions.CheckConverterNullabilityIsSameAsPropertyType(this, typeof(TTarget)); |
| | | 131 | | |
| | | 132 | | // Avoid layering casting converters by consulting any source converters directly. |
| | 0 | 133 | | return |
| | 0 | 134 | | SourceConverterForCastingConverter?.CreateCastingConverter<TTarget>() |
| | 0 | 135 | | ?? new CastingConverter<TTarget>(this); |
| | | 136 | | } |
| | 12254 | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Tracks whether the JsonConverter<T>.HandleNull property has been overridden by a derived converter. |
| | | 141 | | /// </summary> |
| | 3441 | 142 | | internal bool UsesDefaultHandleNull { get; private protected set; } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Does the converter want to be called when reading null tokens. |
| | | 146 | | /// When JsonConverter<T>.HandleNull isn't overridden this can still be true for non-nullable structs. |
| | | 147 | | /// </summary> |
| | 573 | 148 | | internal bool HandleNullOnRead { get; private protected init; } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Does the converter want to be called for null values. |
| | | 152 | | /// Should always match the precise value of the JsonConverter<T>.HandleNull virtual property. |
| | | 153 | | /// </summary> |
| | 573 | 154 | | internal bool HandleNullOnWrite { get; private protected init; } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Set if this converter is itself a casting converter. |
| | | 158 | | /// </summary> |
| | 0 | 159 | | internal virtual JsonConverter? SourceConverterForCastingConverter => null; |
| | | 160 | | |
| | 11252 | 161 | | internal virtual Type? ElementType => null; |
| | | 162 | | |
| | 11890 | 163 | | internal virtual Type? KeyType => null; |
| | | 164 | | |
| | 1299 | 165 | | internal virtual JsonConverter? NullableElementConverter => null; |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Cached value of TypeToConvert.IsValueType, which is an expensive call. |
| | | 169 | | /// </summary> |
| | 10137 | 170 | | internal bool IsValueType { get; init; } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Whether the converter is built-in. |
| | | 174 | | /// </summary> |
| | 29394 | 175 | | internal bool IsInternalConverter { get; init; } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Whether the converter is built-in and handles a number type. |
| | | 179 | | /// </summary> |
| | 39086 | 180 | | internal bool IsInternalConverterForNumberType { get; init; } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Whether the converter handles collection deserialization by converting from |
| | | 184 | | /// an intermediate buffer such as immutable collections, arrays or memory types. |
| | | 185 | | /// Used in conjunction with <see cref="JsonCollectionConverter{TCollection, TElement}.ConvertCollection(ref Rea |
| | | 186 | | /// </summary> |
| | 0 | 187 | | internal virtual bool IsConvertibleCollection => false; |
| | | 188 | | |
| | | 189 | | internal static bool ShouldFlush(ref WriteStack state, Utf8JsonWriter writer) |
| | 0 | 190 | | { |
| | 0 | 191 | | Debug.Assert(state.FlushThreshold == 0 || (state.PipeWriter is { CanGetUnflushedBytes: true }), |
| | 0 | 192 | | "ShouldFlush should only be called by resumable serializers, all of which use the PipeWriter abstraction |
| | | 193 | | // If surpassed flush threshold then return true which will flush stream. |
| | 0 | 194 | | if (state.PipeWriter is { } pipeWriter) |
| | 0 | 195 | | { |
| | 0 | 196 | | return state.FlushThreshold > 0 && pipeWriter.UnflushedBytes > state.FlushThreshold - writer.BytesPendin |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | return false; |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | internal abstract object? ReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions opti |
| | | 203 | | internal abstract bool OnTryReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions op |
| | | 204 | | internal abstract bool TryReadAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions opti |
| | | 205 | | internal abstract object? ReadAsPropertyNameAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializ |
| | | 206 | | internal abstract object? ReadAsPropertyNameCoreAsObject(ref Utf8JsonReader reader, Type typeToConvert, JsonSeri |
| | | 207 | | internal abstract object? ReadNumberWithCustomHandlingAsObject(ref Utf8JsonReader reader, JsonNumberHandling han |
| | | 208 | | |
| | | 209 | | internal abstract void WriteAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions options); |
| | | 210 | | internal abstract bool OnTryWriteAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions options, r |
| | | 211 | | internal abstract bool TryWriteAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions options, ref |
| | | 212 | | internal abstract void WriteAsPropertyNameAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptions o |
| | | 213 | | internal abstract void WriteAsPropertyNameCoreAsObject(Utf8JsonWriter writer, object? value, JsonSerializerOptio |
| | | 214 | | internal abstract void WriteNumberWithCustomHandlingAsObject(Utf8JsonWriter writer, object? value, JsonNumberHan |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Gets a schema from the type being converted |
| | | 218 | | /// </summary> |
| | 0 | 219 | | internal virtual JsonSchema? GetSchema(JsonNumberHandling numberHandling) => null; |
| | | 220 | | |
| | | 221 | | // Whether a type (ConverterStrategy.Object) is deserialized using a parameterized constructor. |
| | 18007 | 222 | | internal virtual bool ConstructorIsParameterized { get; } |
| | | 223 | | |
| | 18948 | 224 | | internal ConstructorInfo? ConstructorInfo { get; set; } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Used for hooking custom configuration to a newly created associated JsonTypeInfo instance. |
| | | 228 | | /// </summary> |
| | 23970 | 229 | | internal virtual void ConfigureJsonTypeInfo(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions options) { } |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Additional reflection-specific configuration required by certain collection converters. |
| | | 233 | | /// </summary> |
| | | 234 | | [RequiresUnreferencedCode(JsonSerializer.SerializationUnreferencedCodeMessage)] |
| | | 235 | | [RequiresDynamicCode(JsonSerializer.SerializationRequiresDynamicCodeMessage)] |
| | 23010 | 236 | | internal virtual void ConfigureJsonTypeInfoUsingReflection(JsonTypeInfo jsonTypeInfo, JsonSerializerOptions opti |
| | | 237 | | } |
| | | 238 | | } |