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