private static void HandleStartObject(JsonSerializerOptions options, Type returnType, ref ReadObjectState current, ref List <ReadObjectState> previous, ref int arrayIndex) { Type objType; if (current.IsEnumerable() || current.IsPropertyEnumerable()) { // An array of objects either on the current property or on a list objType = current.GetElementType(); JsonPropertyInfo propInfo = current.PropertyInfo; SetPreviousState(ref previous, current, arrayIndex++); current.Reset(); current.ClassInfo = options.GetOrAddClass(objType); current.ReturnValue = current.ClassInfo.CreateObject(); } else if (current.PropertyInfo != null) { // Nested object objType = current.PropertyInfo.PropertyType; SetPreviousState(ref previous, current, arrayIndex++); current.Reset(); current.ClassInfo = options.GetOrAddClass(objType); current.ReturnValue = current.ClassInfo.CreateObject(); } else { // Initial object type objType = returnType; Debug.Assert(current.ClassInfo != null); current.ReturnValue = current.ClassInfo.CreateObject(); } }
private static void HandleStartArray( JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadObjectState current, ref List <ReadObjectState> previous, ref int arrayIndex) { Type arrayType = current.PropertyInfo.PropertyType; if (!typeof(IEnumerable).IsAssignableFrom(arrayType) || (arrayType.IsArray && arrayType.GetArrayRank() > 1)) { throw new JsonReaderException($"todo: type {arrayType.ToString()} is not convertable to array.", 0, 0); } Debug.Assert(current.IsPropertyEnumerable()); if (current.IsPropertyEnumerable()) { if (current.EnumerableCreated) { // A nested json array so push a new stack frame. Type elementType = current.ClassInfo.ElementClassInfo.GetPolicyProperty().PropertyType; SetPreviousState(ref previous, current, arrayIndex++); current.Reset(); current.ClassInfo = options.GetOrAddClass(elementType); current.PropertyInfo = current.ClassInfo.GetPolicyProperty(); current.PopStackOnEndArray = true; } else { current.EnumerableCreated = true; } // If current property is already set (from a constructor, for example) leave as-is if (current.PropertyInfo.GetValueAsObject(current.ReturnValue, options) == null) { // Create the enumerable. object value = ReadObjectState.CreateEnumerableValue(ref current, options); if (value != null) { if (current.ReturnValue != null) { current.PropertyInfo.SetValueAsObject(current.ReturnValue, value, options); } else { // Primitive arrays being returned without object current.SetReturnValue(value, options); } } } } }
private static bool HandleEndObject(JsonSerializerOptions options, ref ReadObjectState current, ref List <ReadObjectState> previous, ref int arrayIndex) { object value = current.ReturnValue; if (arrayIndex > 0) { ReadObjectState previousFrame = default; GetPreviousState(ref previous, ref previousFrame, --arrayIndex); current = previousFrame; } else { current.Reset(); current.ReturnValue = value; return(true); } ReadObjectState.SetReturnValue(value, options, ref current); return(false); }
private static bool HandleEndArray( JsonSerializerOptions options, ref ReadObjectState current, ref List <ReadObjectState> previous, ref int arrayIndex) { IEnumerable value = ReadObjectState.GetEnumerableValue(current); if (value == null) { // We added the items to the list property already. current.ResetProperty(); return(false); } bool lastFrame = (arrayIndex == 0); bool setPropertyDirectly; if (current.TempEnumerableValues != null) { JsonEnumerableConverter converter = current.PropertyInfo.EnumerableConverter; if (converter == null) { converter = current.ClassInfo.EnumerableConverter; } Type elementType = current.GetElementType(); value = converter.CreateFromList(elementType, (IList)value); setPropertyDirectly = true; } else { setPropertyDirectly = false; } if (current.PopStackOnEndArray) { ReadObjectState previousFrame = default; GetPreviousState(ref previous, ref previousFrame, --arrayIndex); current = previousFrame; } if (lastFrame) { if (current.ReturnValue == null) { // Returning a converted list or object. current.Reset(); current.ReturnValue = value; return(true); } else if (current.IsEnumerable()) { // Returning a non-converted list. return(true); } // else there must be an outer object, so we'll return false here. } ReadObjectState.SetReturnValue(value, options, ref current, setPropertyDirectly: setPropertyDirectly); return(false); }