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 // See the LICENSE file in the project root for more information.
5 using System.Diagnostics;
7 namespace System.Text.Json.Serialization
9 public static partial class JsonSerializer
11 private static void HandleStartObject(JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadStack state)
13 if (state.Current.Skip())
16 state.Current.Drain = true;
20 if (state.Current.IsProcessingEnumerable)
22 Type objType = state.Current.GetElementType();
24 state.Current.Initialize(objType, options);
26 else if (state.Current.JsonPropertyInfo != null)
28 if (state.Current.IsDictionary)
30 // Verify that the Dictionary can be deserialized by having <string> as first generic argument.
31 Debug.Assert(state.Current.JsonClassInfo.Type.GetGenericArguments().Length >= 1);
32 if (state.Current.JsonClassInfo.Type.GetGenericArguments()[0].UnderlyingSystemType != typeof(string))
34 ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(state.Current.JsonClassInfo.Type, reader, state.PropertyPath);
37 ClassType classType = state.Current.JsonClassInfo.ElementClassInfo.ClassType;
39 if (state.Current.ReturnValue == null)
41 // The Dictionary created below will be returned to corresponding Parse() etc method.
42 // Ensure any nested array creates a new frame.
43 state.Current.EnumerableCreated = true;
47 Debug.Assert(classType == ClassType.Object || classType == ClassType.Dictionary);
49 // A nested object or dictionary.
50 JsonClassInfo classInfoTemp = state.Current.JsonClassInfo;
52 state.Current.JsonClassInfo = classInfoTemp.ElementClassInfo;
53 state.Current.InitializeJsonPropertyInfo();
59 Type objType = state.Current.JsonPropertyInfo.RuntimePropertyType;
61 state.Current.Initialize(objType, options);
65 JsonClassInfo classInfo = state.Current.JsonClassInfo;
66 state.Current.ReturnValue = classInfo.CreateObject();
69 private static bool HandleEndObject(JsonSerializerOptions options, ref ReadStack state, ref Utf8JsonReader reader)
71 bool isLastFrame = state.IsLastFrame;
72 if (state.Current.Drain)
78 state.Current.JsonClassInfo.UpdateSortedPropertyCache(ref state.Current);
80 object value = state.Current.ReturnValue;
84 state.Current.Reset();
85 state.Current.ReturnValue = value;
90 ApplyObjectToEnumerable(value, options, ref state, ref reader);