Skip to content

Instantly share code, notes, and snippets.

@layomia
Created October 29, 2019 23:59
Show Gist options
  • Save layomia/a331d9d3287ea19195c6ef4994a5dde9 to your computer and use it in GitHub Desktop.
Save layomia/a331d9d3287ea19195c6ef4994a5dde9 to your computer and use it in GitHub Desktop.
private static void HandleStartObject(JsonSerializerOptions options, ref ReadStack state)
{
Debug.Assert(!state.Current.IsProcessingDictionary());
if (state.Current.IsProcessingEnumerable())
{
// A nested object within an enumerable.
Type objType = state.Current.GetElementType();
state.Push();
state.Current.Initialize(objType, options);
}
else if (state.Current.JsonPropertyInfo != null)
{
// Nested object.
Type objType = state.Current.JsonPropertyInfo.RuntimePropertyType;
state.Push();
state.Current.Initialize(objType, options);
}
JsonClassInfo classInfo = state.Current.JsonClassInfo;
if (state.Current.IsProcessingObject(ClassType.Dictionary))
{
object value = ReadStackFrame.CreateDictionaryValue(ref state);
// If value is not null, then we don't have a converter so apply the value.
if (value != null)
{
state.Current.ReturnValue = value;
state.Current.DetermineIfDictionaryCanBePopulated(state.Current.ReturnValue);
}
state.Current.CollectionPropertyInitialized = true;
}
else if (state.Current.IsProcessingObject(ClassType.Object))
{
if (classInfo.CreateObject is null)
{
if (classInfo.Type.IsInterface)
{
ThrowHelper.ThrowNotSupportedException_DeserializePolymorphicInterface(classInfo.Type);
}
else
{
ThrowHelper.ThrowNotSupportedException_DeserializeMissingParameterlessConstructor(classInfo.Type);
}
}
state.Current.ReturnValue = classInfo.CreateObject();
}
else
{
ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(classInfo.Type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment