| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Text.Json.Serialization.Metadata; |
| | | 8 | | |
| | | 9 | | namespace System.Text.Json.Serialization |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Base class for all collections. Collections are assumed to implement <see cref="IEnumerable{T}"/> |
| | | 13 | | /// or a variant thereof e.g. <see cref="IAsyncEnumerable{T}"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | internal abstract class JsonCollectionConverter<TCollection, TElement> : JsonResumableConverter<TCollection> |
| | | 16 | | { |
| | 0 | 17 | | internal override bool SupportsCreateObjectDelegate => true; |
| | 0 | 18 | | private protected sealed override ConverterStrategy GetDefaultConverterStrategy() => ConverterStrategy.Enumerabl |
| | 0 | 19 | | internal sealed override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.Array; |
| | 0 | 20 | | internal override Type ElementType => typeof(TElement); |
| | | 21 | | |
| | | 22 | | protected abstract void Add(in TElement value, ref ReadStack state); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// When overridden, create the collection. It may be a temporary collection or the final collection. |
| | | 26 | | /// </summary> |
| | | 27 | | protected virtual void CreateCollection(ref Utf8JsonReader reader, scoped ref ReadStack state, JsonSerializerOpt |
| | 0 | 28 | | { |
| | 0 | 29 | | if (state.ParentProperty?.TryGetPrePopulatedValue(ref state) == true) |
| | 0 | 30 | | { |
| | 0 | 31 | | return; |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | JsonTypeInfo typeInfo = state.Current.JsonTypeInfo; |
| | | 35 | | |
| | 0 | 36 | | if (typeInfo.CreateObject is null) |
| | 0 | 37 | | { |
| | 0 | 38 | | ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(typeInfo, ref reader, ref state); |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | state.Current.ReturnValue = typeInfo.CreateObject(); |
| | 0 | 42 | | Debug.Assert(state.Current.ReturnValue is TCollection); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// When overridden, converts the temporary collection held in state.Current.ReturnValue to the final collection |
| | | 47 | | /// The <see cref="JsonConverter.IsConvertibleCollection"/> property must also be set to <see langword="true"/>. |
| | | 48 | | /// </summary> |
| | 0 | 49 | | protected virtual void ConvertCollection(ref ReadStack state, JsonSerializerOptions options) { } |
| | | 50 | | |
| | | 51 | | protected static JsonConverter<TElement> GetElementConverter(JsonTypeInfo elementTypeInfo) |
| | 0 | 52 | | { |
| | 0 | 53 | | return ((JsonTypeInfo<TElement>)elementTypeInfo).EffectiveConverter; |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | protected static JsonConverter<TElement> GetElementConverter(ref WriteStack state) |
| | 0 | 57 | | { |
| | 0 | 58 | | Debug.Assert(state.Current.JsonPropertyInfo is not null); |
| | 0 | 59 | | return (JsonConverter<TElement>)state.Current.JsonPropertyInfo.EffectiveConverter; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | internal override bool OnTryRead( |
| | | 63 | | ref Utf8JsonReader reader, |
| | | 64 | | Type typeToConvert, |
| | | 65 | | JsonSerializerOptions options, |
| | | 66 | | scoped ref ReadStack state, |
| | | 67 | | [MaybeNullWhen(false)] out TCollection value) |
| | 0 | 68 | | { |
| | 0 | 69 | | JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; |
| | 0 | 70 | | JsonTypeInfo elementTypeInfo = jsonTypeInfo.ElementTypeInfo!; |
| | | 71 | | |
| | 0 | 72 | | if (!state.SupportContinuation && !state.Current.CanContainMetadata) |
| | 0 | 73 | | { |
| | | 74 | | // Fast path that avoids maintaining state variables and dealing with preserved references. |
| | | 75 | | |
| | 0 | 76 | | if (reader.TokenType != JsonTokenType.StartArray) |
| | 0 | 77 | | { |
| | 0 | 78 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | CreateCollection(ref reader, ref state, options); |
| | | 82 | | |
| | 0 | 83 | | jsonTypeInfo.OnDeserializing?.Invoke(state.Current.ReturnValue!); |
| | | 84 | | |
| | 0 | 85 | | state.Current.JsonPropertyInfo = elementTypeInfo.PropertyInfoForTypeInfo; |
| | 0 | 86 | | JsonConverter<TElement> elementConverter = GetElementConverter(elementTypeInfo); |
| | 0 | 87 | | if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling is null) |
| | 0 | 88 | | { |
| | | 89 | | // Fast path that avoids validation and extra indirection. |
| | 0 | 90 | | while (true) |
| | 0 | 91 | | { |
| | 0 | 92 | | reader.ReadWithVerify(); |
| | 0 | 93 | | if (reader.TokenType == JsonTokenType.EndArray) |
| | 0 | 94 | | { |
| | 0 | 95 | | break; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | // Obtain the CLR value from the JSON and apply to the object. |
| | 0 | 99 | | TElement? element = elementConverter.Read(ref reader, elementConverter.Type, options); |
| | 0 | 100 | | Add(element!, ref state); |
| | 0 | 101 | | } |
| | 0 | 102 | | } |
| | | 103 | | else |
| | 0 | 104 | | { |
| | | 105 | | // Process all elements. |
| | 0 | 106 | | while (true) |
| | 0 | 107 | | { |
| | 0 | 108 | | reader.ReadWithVerify(); |
| | 0 | 109 | | if (reader.TokenType == JsonTokenType.EndArray) |
| | 0 | 110 | | { |
| | 0 | 111 | | break; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Get the value from the converter and add it. |
| | 0 | 115 | | elementConverter.TryRead(ref reader, typeof(TElement), options, ref state, out TElement? element |
| | 0 | 116 | | Add(element!, ref state); |
| | 0 | 117 | | } |
| | 0 | 118 | | } |
| | 0 | 119 | | } |
| | | 120 | | else |
| | 0 | 121 | | { |
| | | 122 | | // Slower path that supports continuation and reading metadata. |
| | 0 | 123 | | if (state.Current.ObjectState == StackFrameObjectState.None) |
| | 0 | 124 | | { |
| | 0 | 125 | | if (reader.TokenType == JsonTokenType.StartArray) |
| | 0 | 126 | | { |
| | 0 | 127 | | state.Current.ObjectState = StackFrameObjectState.ReadMetadata; |
| | 0 | 128 | | } |
| | 0 | 129 | | else if (state.Current.CanContainMetadata) |
| | 0 | 130 | | { |
| | 0 | 131 | | if (reader.TokenType != JsonTokenType.StartObject) |
| | 0 | 132 | | { |
| | 0 | 133 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | state.Current.ObjectState = StackFrameObjectState.StartToken; |
| | 0 | 137 | | } |
| | | 138 | | else |
| | 0 | 139 | | { |
| | 0 | 140 | | ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type); |
| | | 141 | | } |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | // Handle the metadata properties. |
| | 0 | 145 | | if (state.Current.CanContainMetadata && state.Current.ObjectState < StackFrameObjectState.ReadMetadata) |
| | 0 | 146 | | { |
| | 0 | 147 | | if (!JsonSerializer.TryReadMetadata(this, jsonTypeInfo, ref reader, ref state)) |
| | 0 | 148 | | { |
| | 0 | 149 | | value = default; |
| | 0 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | if (state.Current.MetadataPropertyNames == MetadataPropertyName.Ref) |
| | 0 | 154 | | { |
| | 0 | 155 | | value = JsonSerializer.ResolveReferenceId<TCollection>(ref state); |
| | 0 | 156 | | return true; |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | state.Current.ObjectState = StackFrameObjectState.ReadMetadata; |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | // Dispatch to any polymorphic converters: should always be entered regardless of ObjectState progress |
| | 0 | 163 | | if (((state.Current.MetadataPropertyNames & MetadataPropertyName.Type) != 0 || state.PolymorphicResolved |
| | 0 | 164 | | state.Current.PolymorphicSerializationState != PolymorphicSerializationState.PolymorphicReEntryStart |
| | 0 | 165 | | ResolvePolymorphicConverter(jsonTypeInfo, ref state) is JsonConverter polymorphicConverter) |
| | 0 | 166 | | { |
| | 0 | 167 | | Debug.Assert(!IsValueType); |
| | 0 | 168 | | bool success = polymorphicConverter.OnTryReadAsObject(ref reader, polymorphicConverter.Type!, option |
| | 0 | 169 | | value = (TCollection)objectResult!; |
| | 0 | 170 | | state.ExitPolymorphicConverter(success); |
| | 0 | 171 | | return success; |
| | | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | if (state.Current.ObjectState < StackFrameObjectState.CreatedObject) |
| | 0 | 175 | | { |
| | 0 | 176 | | if (state.Current.CanContainMetadata) |
| | 0 | 177 | | { |
| | 0 | 178 | | JsonSerializer.ValidateMetadataForArrayConverter(this, ref reader, ref state); |
| | 0 | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | CreateCollection(ref reader, ref state, options); |
| | | 182 | | |
| | 0 | 183 | | if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Id) != 0) |
| | 0 | 184 | | { |
| | 0 | 185 | | Debug.Assert(state.ReferenceId is not null); |
| | 0 | 186 | | Debug.Assert(options.ReferenceHandlingStrategy == JsonKnownReferenceHandler.Preserve); |
| | 0 | 187 | | Debug.Assert(state.Current.ReturnValue is TCollection); |
| | 0 | 188 | | state.ReferenceResolver.AddReference(state.ReferenceId, state.Current.ReturnValue); |
| | 0 | 189 | | state.ReferenceId = null; |
| | 0 | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | jsonTypeInfo.OnDeserializing?.Invoke(state.Current.ReturnValue!); |
| | | 193 | | |
| | 0 | 194 | | state.Current.ObjectState = StackFrameObjectState.CreatedObject; |
| | 0 | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | if (state.Current.ObjectState < StackFrameObjectState.ReadElements) |
| | 0 | 198 | | { |
| | 0 | 199 | | JsonConverter<TElement> elementConverter = GetElementConverter(elementTypeInfo); |
| | 0 | 200 | | state.Current.JsonPropertyInfo = elementTypeInfo.PropertyInfoForTypeInfo; |
| | | 201 | | |
| | | 202 | | // Process all elements. |
| | 0 | 203 | | while (true) |
| | 0 | 204 | | { |
| | 0 | 205 | | if (state.Current.PropertyState < StackFramePropertyState.ReadValue) |
| | 0 | 206 | | { |
| | 0 | 207 | | if (!reader.TryAdvanceWithOptionalReadAhead(elementConverter.RequiresReadAhead)) |
| | 0 | 208 | | { |
| | 0 | 209 | | value = default; |
| | 0 | 210 | | return false; |
| | | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | state.Current.PropertyState = StackFramePropertyState.ReadValue; |
| | 0 | 214 | | } |
| | | 215 | | |
| | 0 | 216 | | if (state.Current.PropertyState < StackFramePropertyState.ReadValueIsEnd) |
| | 0 | 217 | | { |
| | 0 | 218 | | if (reader.TokenType == JsonTokenType.EndArray) |
| | 0 | 219 | | { |
| | 0 | 220 | | break; |
| | | 221 | | } |
| | | 222 | | |
| | 0 | 223 | | state.Current.PropertyState = StackFramePropertyState.ReadValueIsEnd; |
| | 0 | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | if (state.Current.PropertyState < StackFramePropertyState.TryRead) |
| | 0 | 227 | | { |
| | | 228 | | // Get the value from the converter and add it. |
| | 0 | 229 | | if (!elementConverter.TryRead(ref reader, typeof(TElement), options, ref state, out TElement |
| | 0 | 230 | | { |
| | 0 | 231 | | value = default; |
| | 0 | 232 | | return false; |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | Add(element!, ref state); |
| | | 236 | | |
| | | 237 | | // No need to set PropertyState to TryRead since we're done with this element now. |
| | 0 | 238 | | state.Current.EndElement(); |
| | 0 | 239 | | } |
| | 0 | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | state.Current.ObjectState = StackFrameObjectState.ReadElements; |
| | 0 | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | if (state.Current.ObjectState < StackFrameObjectState.EndToken) |
| | 0 | 246 | | { |
| | | 247 | | // Array payload is nested inside a $values metadata property. |
| | 0 | 248 | | if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Values) != 0) |
| | 0 | 249 | | { |
| | 0 | 250 | | if (!reader.Read()) |
| | 0 | 251 | | { |
| | 0 | 252 | | value = default; |
| | 0 | 253 | | return false; |
| | | 254 | | } |
| | 0 | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | state.Current.ObjectState = StackFrameObjectState.EndToken; |
| | 0 | 258 | | } |
| | | 259 | | |
| | 0 | 260 | | if (state.Current.ObjectState < StackFrameObjectState.EndTokenValidation) |
| | 0 | 261 | | { |
| | | 262 | | // Array payload is nested inside a $values metadata property. |
| | 0 | 263 | | if ((state.Current.MetadataPropertyNames & MetadataPropertyName.Values) != 0) |
| | 0 | 264 | | { |
| | 0 | 265 | | if (reader.TokenType != JsonTokenType.EndObject) |
| | 0 | 266 | | { |
| | 0 | 267 | | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); |
| | 0 | 268 | | if (options.AllowOutOfOrderMetadataProperties) |
| | 0 | 269 | | { |
| | 0 | 270 | | Debug.Assert(JsonSerializer.IsMetadataPropertyName(reader.GetUnescapedSpan(), (state.Cur |
| | 0 | 271 | | bool result = reader.TrySkipPartial(reader.CurrentDepth - 1); // skip to the end of the |
| | 0 | 272 | | Debug.Assert(result, "Metadata reader must have buffered all contents."); |
| | 0 | 273 | | Debug.Assert(reader.TokenType is JsonTokenType.EndObject); |
| | 0 | 274 | | } |
| | | 275 | | else |
| | 0 | 276 | | { |
| | 0 | 277 | | ThrowHelper.ThrowJsonException_MetadataInvalidPropertyInArrayMetadata(ref state, typeToC |
| | | 278 | | } |
| | 0 | 279 | | } |
| | 0 | 280 | | } |
| | 0 | 281 | | } |
| | 0 | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | ConvertCollection(ref state, options); |
| | 0 | 285 | | object returnValue = state.Current.ReturnValue!; |
| | 0 | 286 | | jsonTypeInfo.OnDeserialized?.Invoke(returnValue); |
| | 0 | 287 | | value = (TCollection)returnValue; |
| | | 288 | | |
| | 0 | 289 | | return true; |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | internal override bool OnTryWrite( |
| | | 293 | | Utf8JsonWriter writer, |
| | | 294 | | TCollection value, |
| | | 295 | | JsonSerializerOptions options, |
| | | 296 | | ref WriteStack state) |
| | 0 | 297 | | { |
| | | 298 | | bool success; |
| | | 299 | | |
| | 0 | 300 | | if (value is null) |
| | 0 | 301 | | { |
| | 0 | 302 | | writer.WriteNullValue(); |
| | 0 | 303 | | success = true; |
| | 0 | 304 | | } |
| | | 305 | | else |
| | 0 | 306 | | { |
| | 0 | 307 | | JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo; |
| | | 308 | | |
| | 0 | 309 | | if (!state.Current.ProcessedStartToken) |
| | 0 | 310 | | { |
| | 0 | 311 | | state.Current.ProcessedStartToken = true; |
| | | 312 | | |
| | 0 | 313 | | jsonTypeInfo.OnSerializing?.Invoke(value); |
| | | 314 | | |
| | 0 | 315 | | if (state.CurrentContainsMetadata && CanHaveMetadata) |
| | 0 | 316 | | { |
| | 0 | 317 | | state.Current.MetadataPropertyName = JsonSerializer.WriteMetadataForCollection(this, ref state, |
| | 0 | 318 | | } |
| | | 319 | | |
| | | 320 | | // Writing the start of the array must happen after any metadata |
| | 0 | 321 | | writer.WriteStartArray(); |
| | 0 | 322 | | state.Current.JsonPropertyInfo = jsonTypeInfo.ElementTypeInfo!.PropertyInfoForTypeInfo; |
| | 0 | 323 | | } |
| | | 324 | | |
| | 0 | 325 | | success = OnWriteResume(writer, value, options, ref state); |
| | 0 | 326 | | if (success) |
| | 0 | 327 | | { |
| | 0 | 328 | | if (!state.Current.ProcessedEndToken) |
| | 0 | 329 | | { |
| | 0 | 330 | | state.Current.ProcessedEndToken = true; |
| | 0 | 331 | | writer.WriteEndArray(); |
| | | 332 | | |
| | 0 | 333 | | if (state.Current.MetadataPropertyName != 0) |
| | 0 | 334 | | { |
| | | 335 | | // Write the EndObject for $values. |
| | 0 | 336 | | writer.WriteEndObject(); |
| | 0 | 337 | | } |
| | 0 | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | jsonTypeInfo.OnSerialized?.Invoke(value); |
| | 0 | 341 | | } |
| | 0 | 342 | | } |
| | | 343 | | |
| | 0 | 344 | | return success; |
| | 0 | 345 | | } |
| | | 346 | | |
| | | 347 | | protected abstract bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, r |
| | | 348 | | } |
| | | 349 | | } |
| | | 350 | | |