| | | 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.Text.Json.Serialization.Metadata; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Base class for dictionary converters such as IDictionary, Hashtable, Dictionary{,} IDictionary{,} and SortedList |
| | | 12 | | /// </summary> |
| | | 13 | | internal abstract class JsonDictionaryConverter<TDictionary> : JsonResumableConverter<TDictionary> |
| | | 14 | | { |
| | | 15 | | internal override bool SupportsCreateObjectDelegate => true; |
| | | 16 | | private protected sealed override ConverterStrategy GetDefaultConverterStrategy() => ConverterStrategy.Dictionar |
| | | 17 | | internal sealed override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.Object; |
| | | 18 | | |
| | | 19 | | protected internal abstract bool OnWriteResume(Utf8JsonWriter writer, TDictionary dictionary, JsonSerializerOpti |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Base class for dictionary converters such as IDictionary, Hashtable, Dictionary{,} IDictionary{,} and SortedList |
| | | 24 | | /// </summary> |
| | | 25 | | internal abstract class JsonDictionaryConverter<TDictionary, TKey, TValue> : JsonDictionaryConverter<TDictionary> |
| | | 26 | | where TKey : notnull |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// When overridden, adds the value to the collection. |
| | | 30 | | /// </summary> |
| | | 31 | | protected abstract void Add(TKey key, in TValue value, JsonSerializerOptions options, ref ReadStack state); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// When overridden, converts the temporary collection held in state.Current.ReturnValue to the final collection |
| | | 35 | | /// This is used with immutable collections. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | protected virtual void ConvertCollection(ref ReadStack state, JsonSerializerOptions options) { } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// When overridden, create the collection. It may be a temporary collection or the final collection. |
| | | 41 | | /// </summary> |
| | | 42 | | protected virtual void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state) |
| | 0 | 43 | | { |
| | 0 | 44 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 45 | | { |
| | 0 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | JsonTypeInfo typeInfo = state.Current.JsonTypeInfo; |
| | | 50 | | |
| | 0 | 51 | | if (typeInfo.CreateObject is null) |
| | 0 | 52 | | { |
| | 0 | 53 | | ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(typeInfo, ref reader, ref state); |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | state.Current.ReturnValue = typeInfo.CreateObject(); |
| | 0 | 57 | | Debug.Assert(state.Current.ReturnValue is TDictionary); |
| | 0 | 58 | | } |
| | | 59 | | |
| | 1919 | 60 | | internal override Type ElementType => typeof(TValue); |
| | | 61 | | |
| | 271 | 62 | | internal override Type KeyType => typeof(TKey); |
| | | 63 | | |
| | | 64 | | |
| | | 65 | | protected JsonConverter<TKey>? _keyConverter; |
| | | 66 | | protected JsonConverter<TValue>? _valueConverter; |
| | | 67 | | |
| | | 68 | | protected static JsonConverter<T> GetConverter<T>(JsonTypeInfo typeInfo) |
| | 0 | 69 | | { |
| | 0 | 70 | | return ((JsonTypeInfo<T>)typeInfo).EffectiveConverter; |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | internal sealed override bool OnTryRead( |
| | | 74 | | ref Utf8JsonReader reader, |
| | | 75 | | Type typeToConvert, |
| | | 76 | | JsonSerializerOptions options, |
| | | 77 | | scoped ref ReadStack state, |
| | | 78 | | [MaybeNullWhen(false)] out TDictionary value) |
| | 12 | 79 | | { |
| | 12 | 80 | | JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; |
| | 12 | 81 | | JsonTypeInfo keyTypeInfo = jsonTypeInfo.KeyTypeInfo!; |
| | 12 | 82 | | JsonTypeInfo elementTypeInfo = jsonTypeInfo.ElementTypeInfo!; |
| | | 83 | | |
| | 12 | 84 | | if (!state.SupportContinuation && !state.Current.CanContainMetadata) |
| | 0 | 85 | | { |
| | | 86 | | // Fast path that avoids maintaining state variables and dealing with preserved references. |
| | | 87 | | |
| | 0 | 88 | | if (reader.TokenType != JsonTokenType.StartObject) |
| | 0 | 89 | | { |
| | 0 | 90 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | CreateCollection(ref reader, ref state); |
| | | 94 | | |
| | 0 | 95 | | jsonTypeInfo.OnDeserializing?.Invoke(state.Current.ReturnValue!); |
| | | 96 | | |
| | 0 | 97 | | _keyConverter ??= GetConverter<TKey>(keyTypeInfo); |
| | 0 | 98 | | _valueConverter ??= GetConverter<TValue>(elementTypeInfo); |
| | | 99 | | |
| | 0 | 100 | | if (_valueConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling is null) |
| | 0 | 101 | | { |
| | | 102 | | // Process all elements. |
| | 0 | 103 | | while (true) |
| | 0 | 104 | | { |
| | | 105 | | // Read the key name. |
| | 0 | 106 | | reader.ReadWithVerify(); |
| | | 107 | | |
| | 0 | 108 | | if (reader.TokenType == JsonTokenType.EndObject) |
| | 0 | 109 | | { |
| | 0 | 110 | | break; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | // Read method would have thrown if otherwise. |
| | 0 | 114 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | | 115 | | |
| | 0 | 116 | | state.Current.JsonPropertyInfo = keyTypeInfo.PropertyInfoForTypeInfo; |
| | 0 | 117 | | TKey key = ReadDictionaryKey(_keyConverter, ref reader, ref state, options); |
| | | 118 | | |
| | | 119 | | // Read the value and add. |
| | 0 | 120 | | reader.ReadWithVerify(); |
| | 0 | 121 | | state.Current.JsonPropertyInfo = elementTypeInfo.PropertyInfoForTypeInfo; |
| | 0 | 122 | | TValue? element = _valueConverter.Read(ref reader, ElementType, options); |
| | 0 | 123 | | Add(key, element!, options, ref state); |
| | 0 | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | else |
| | 0 | 127 | | { |
| | | 128 | | // Process all elements. |
| | 0 | 129 | | while (true) |
| | 0 | 130 | | { |
| | | 131 | | // Read the key name. |
| | 0 | 132 | | reader.ReadWithVerify(); |
| | | 133 | | |
| | 0 | 134 | | if (reader.TokenType == JsonTokenType.EndObject) |
| | 0 | 135 | | { |
| | 0 | 136 | | break; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | // Read method would have thrown if otherwise. |
| | 0 | 140 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | 0 | 141 | | state.Current.JsonPropertyInfo = keyTypeInfo.PropertyInfoForTypeInfo; |
| | 0 | 142 | | TKey key = ReadDictionaryKey(_keyConverter, ref reader, ref state, options); |
| | | 143 | | |
| | 0 | 144 | | reader.ReadWithVerify(); |
| | | 145 | | |
| | | 146 | | // Get the value from the converter and add it. |
| | 0 | 147 | | state.Current.JsonPropertyInfo = elementTypeInfo.PropertyInfoForTypeInfo; |
| | 0 | 148 | | _valueConverter.TryRead(ref reader, ElementType, options, ref state, out TValue? element, out _) |
| | 0 | 149 | | Add(key, element!, options, ref state); |
| | 0 | 150 | | } |
| | 0 | 151 | | } |
| | 0 | 152 | | } |
| | | 153 | | else |
| | 12 | 154 | | { |
| | | 155 | | // Slower path that supports continuation and reading metadata. |
| | 12 | 156 | | if (state.Current.ObjectState == StackFrameObjectState.None) |
| | 12 | 157 | | { |
| | 12 | 158 | | if (reader.TokenType != JsonTokenType.StartObject) |
| | 12 | 159 | | { |
| | 12 | 160 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | state.Current.ObjectState = StackFrameObjectState.StartToken; |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | // Handle the metadata properties. |
| | 0 | 167 | | if (state.Current.CanContainMetadata && state.Current.ObjectState < StackFrameObjectState.ReadMetadata) |
| | 0 | 168 | | { |
| | 0 | 169 | | if (!JsonSerializer.TryReadMetadata(this, jsonTypeInfo, ref reader, ref state)) |
| | 0 | 170 | | { |
| | 0 | 171 | | value = default; |
| | 0 | 172 | | return false; |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | if (state.Current.MetadataPropertyNames == MetadataPropertyName.Ref) |
| | 0 | 176 | | { |
| | 0 | 177 | | value = JsonSerializer.ResolveReferenceId<TDictionary>(ref state); |
| | 0 | 178 | | return true; |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | state.Current.ObjectState = StackFrameObjectState.ReadMetadata; |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | // Dispatch to any polymorphic converters: should always be entered regardless of ObjectState progress |
| | 0 | 185 | | if (((state.Current.MetadataPropertyNames & MetadataPropertyName.Type) != 0 || state.PolymorphicResolved |
| | 0 | 186 | | state.Current.PolymorphicSerializationState != PolymorphicSerializationState.PolymorphicReEntryStart |
| | 0 | 187 | | ResolvePolymorphicConverter(jsonTypeInfo, ref state) is JsonConverter polymorphicConverter) |
| | 0 | 188 | | { |
| | 0 | 189 | | Debug.Assert(!IsValueType); |
| | 0 | 190 | | bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.Type!, option |
| | 0 | 191 | | value = (TDictionary)objectResult!; |
| | 0 | 192 | | state.ExitPolymorphicConverter(success); |
| | 0 | 193 | | return success; |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | // Create the dictionary. |
| | 0 | 197 | | if (state.Current.ObjectState < StackFrameObjectState.CreatedObject) |
| | 0 | 198 | | { |
| | 0 | 199 | | if (state.Current.CanContainMetadata) |
| | 0 | 200 | | { |
| | 0 | 201 | | JsonSerializer.ValidateMetadataForObjectConverter(ref state); |
| | 0 | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | CreateCollection(ref reader, ref state); |
| | | 205 | | |
| | 0 | 206 | | if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Id) != 0) |
| | 0 | 207 | | { |
| | 0 | 208 | | Debug.Assert(state.ReferenceId is not null); |
| | 0 | 209 | | Debug.Assert(options.ReferenceHandlingStrategy == JsonKnownReferenceHandler.Preserve); |
| | 0 | 210 | | Debug.Assert(state.Current.ReturnValue is TDictionary); |
| | 0 | 211 | | state.ReferenceResolver.AddReference(state.ReferenceId, state.Current.ReturnValue); |
| | 0 | 212 | | state.ReferenceId = null; |
| | 0 | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | jsonTypeInfo.OnDeserializing?.Invoke(state.Current.ReturnValue!); |
| | | 216 | | |
| | 0 | 217 | | state.Current.ObjectState = StackFrameObjectState.CreatedObject; |
| | 0 | 218 | | } |
| | | 219 | | |
| | | 220 | | // Process all elements. |
| | 0 | 221 | | _keyConverter ??= GetConverter<TKey>(keyTypeInfo); |
| | 0 | 222 | | _valueConverter ??= GetConverter<TValue>(elementTypeInfo); |
| | 0 | 223 | | while (true) |
| | 0 | 224 | | { |
| | 0 | 225 | | if (state.Current.PropertyState == StackFramePropertyState.None) |
| | 0 | 226 | | { |
| | | 227 | | // Read the key name. |
| | 0 | 228 | | if (!reader.Read()) |
| | 0 | 229 | | { |
| | 0 | 230 | | value = default; |
| | 0 | 231 | | return false; |
| | | 232 | | } |
| | | 233 | | |
| | 0 | 234 | | state.Current.PropertyState = StackFramePropertyState.ReadName; |
| | 0 | 235 | | } |
| | | 236 | | |
| | | 237 | | // Determine the property. |
| | | 238 | | TKey key; |
| | 0 | 239 | | if (state.Current.PropertyState < StackFramePropertyState.Name) |
| | 0 | 240 | | { |
| | 0 | 241 | | if (reader.TokenType == JsonTokenType.EndObject) |
| | 0 | 242 | | { |
| | 0 | 243 | | break; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | // Read method would have thrown if otherwise. |
| | 0 | 247 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | | 248 | | |
| | 0 | 249 | | state.Current.PropertyState = StackFramePropertyState.Name; |
| | | 250 | | |
| | 0 | 251 | | if (state.Current.CanContainMetadata) |
| | 0 | 252 | | { |
| | 0 | 253 | | ReadOnlySpan<byte> propertyName = reader.GetUnescapedSpan(); |
| | 0 | 254 | | if (JsonSerializer.IsMetadataPropertyName(propertyName, state.Current.BaseJsonTypeInfo.Polym |
| | 0 | 255 | | { |
| | 0 | 256 | | if (options.AllowOutOfOrderMetadataProperties) |
| | 0 | 257 | | { |
| | 0 | 258 | | reader.SkipWithVerify(); |
| | 0 | 259 | | state.Current.EndElement(); |
| | 0 | 260 | | continue; |
| | | 261 | | } |
| | | 262 | | else |
| | 0 | 263 | | { |
| | 0 | 264 | | ThrowHelper.ThrowUnexpectedMetadataException(propertyName, ref reader, ref state); |
| | | 265 | | } |
| | | 266 | | } |
| | 0 | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | state.Current.JsonPropertyInfo = keyTypeInfo.PropertyInfoForTypeInfo; |
| | 0 | 270 | | key = ReadDictionaryKey(_keyConverter, ref reader, ref state, options); |
| | 0 | 271 | | } |
| | | 272 | | else |
| | 0 | 273 | | { |
| | | 274 | | // DictionaryKey is assigned before all return false cases, null value is unreachable |
| | 0 | 275 | | key = (TKey)state.Current.DictionaryKey!; |
| | 0 | 276 | | } |
| | | 277 | | |
| | 0 | 278 | | if (state.Current.PropertyState < StackFramePropertyState.ReadValue) |
| | 0 | 279 | | { |
| | 0 | 280 | | if (!reader.TryAdvanceWithOptionalReadAhead(_valueConverter.RequiresReadAhead)) |
| | 0 | 281 | | { |
| | 0 | 282 | | state.Current.DictionaryKey = key; |
| | 0 | 283 | | value = default; |
| | 0 | 284 | | return false; |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | state.Current.PropertyState = StackFramePropertyState.ReadValue; |
| | 0 | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | if (state.Current.PropertyState < StackFramePropertyState.TryRead) |
| | 0 | 291 | | { |
| | | 292 | | // Get the value from the converter and add it. |
| | 0 | 293 | | state.Current.JsonPropertyInfo = elementTypeInfo.PropertyInfoForTypeInfo; |
| | 0 | 294 | | bool success = _valueConverter.TryRead(ref reader, typeof(TValue), options, ref state, out TValu |
| | 0 | 295 | | if (!success) |
| | 0 | 296 | | { |
| | 0 | 297 | | state.Current.DictionaryKey = key; |
| | 0 | 298 | | value = default; |
| | 0 | 299 | | return false; |
| | | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | Add(key, element!, options, ref state); |
| | 0 | 303 | | state.Current.EndElement(); |
| | 0 | 304 | | } |
| | 0 | 305 | | } |
| | 0 | 306 | | } |
| | | 307 | | |
| | 0 | 308 | | ConvertCollection(ref state, options); |
| | 0 | 309 | | object result = state.Current.ReturnValue!; |
| | 0 | 310 | | jsonTypeInfo.OnDeserialized?.Invoke(result); |
| | 0 | 311 | | value = (TDictionary)result; |
| | | 312 | | |
| | 0 | 313 | | return true; |
| | | 314 | | |
| | | 315 | | static TKey ReadDictionaryKey(JsonConverter<TKey> keyConverter, ref Utf8JsonReader reader, scoped ref ReadSt |
| | 0 | 316 | | { |
| | | 317 | | TKey key; |
| | 0 | 318 | | string unescapedPropertyNameAsString = reader.GetString()!; |
| | 0 | 319 | | state.Current.JsonPropertyNameAsString = unescapedPropertyNameAsString; // Copy key name for JSON Path s |
| | | 320 | | |
| | | 321 | | // Special case string to avoid calling GetString twice and save one allocation. |
| | 0 | 322 | | if (keyConverter.IsInternalConverter && keyConverter.Type == typeof(string)) |
| | 0 | 323 | | { |
| | 0 | 324 | | key = (TKey)(object)unescapedPropertyNameAsString; |
| | 0 | 325 | | } |
| | | 326 | | else |
| | 0 | 327 | | { |
| | 0 | 328 | | key = keyConverter.ReadAsPropertyNameCore(ref reader, keyConverter.Type, options); |
| | 0 | 329 | | } |
| | | 330 | | |
| | 0 | 331 | | return key; |
| | 0 | 332 | | } |
| | 0 | 333 | | } |
| | | 334 | | |
| | | 335 | | internal sealed override bool OnTryWrite( |
| | | 336 | | Utf8JsonWriter writer, |
| | | 337 | | TDictionary dictionary, |
| | | 338 | | JsonSerializerOptions options, |
| | | 339 | | ref WriteStack state) |
| | 0 | 340 | | { |
| | 0 | 341 | | if (dictionary is null) |
| | 0 | 342 | | { |
| | 0 | 343 | | writer.WriteNullValue(); |
| | 0 | 344 | | return true; |
| | | 345 | | } |
| | | 346 | | |
| | 0 | 347 | | JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; |
| | | 348 | | |
| | 0 | 349 | | if (!state.Current.ProcessedStartToken) |
| | 0 | 350 | | { |
| | 0 | 351 | | state.Current.ProcessedStartToken = true; |
| | | 352 | | |
| | 0 | 353 | | jsonTypeInfo.OnSerializing?.Invoke(dictionary); |
| | | 354 | | |
| | 0 | 355 | | writer.WriteStartObject(); |
| | | 356 | | |
| | 0 | 357 | | if (state.CurrentContainsMetadata && CanHaveMetadata) |
| | 0 | 358 | | { |
| | 0 | 359 | | JsonSerializer.WriteMetadataForObject(this, ref state, writer); |
| | 0 | 360 | | } |
| | | 361 | | |
| | 0 | 362 | | state.Current.JsonPropertyInfo = jsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo; |
| | 0 | 363 | | } |
| | | 364 | | |
| | 0 | 365 | | bool success = OnWriteResume(writer, dictionary, options, ref state); |
| | 0 | 366 | | if (success) |
| | 0 | 367 | | { |
| | 0 | 368 | | if (!state.Current.ProcessedEndToken) |
| | 0 | 369 | | { |
| | 0 | 370 | | state.Current.ProcessedEndToken = true; |
| | 0 | 371 | | writer.WriteEndObject(); |
| | 0 | 372 | | } |
| | | 373 | | |
| | 0 | 374 | | jsonTypeInfo.OnSerialized?.Invoke(dictionary); |
| | 0 | 375 | | } |
| | | 376 | | |
| | 0 | 377 | | return success; |
| | 0 | 378 | | } |
| | | 379 | | } |
| | | 380 | | } |
| | | 381 | | |