Skip to content

Instantly share code, notes, and snippets.

@layomia
Created October 29, 2019 23:42
Show Gist options
  • Save layomia/6f835e6362d1769ce78f0dc72d3f9b81 to your computer and use it in GitHub Desktop.
Save layomia/6f835e6362d1769ce78f0dc72d3f9b81 to your computer and use it in GitHub Desktop.
private static void HandleStartDictionary(JsonSerializerOptions options, ref ReadStack state)
{
Debug.Assert(!state.Current.IsProcessingEnumerable());
JsonPropertyInfo jsonPropertyInfo = state.Current.JsonPropertyInfo;
if (jsonPropertyInfo == null)
{
jsonPropertyInfo = state.Current.JsonClassInfo.CreateRootProperty(options);
}
Debug.Assert(jsonPropertyInfo != null);
// A nested object or dictionary so push new frame.
if (state.Current.CollectionPropertyInitialized)
{
state.Push();
state.Current.JsonClassInfo = jsonPropertyInfo.ElementClassInfo;
state.Current.InitializeJsonPropertyInfo();
state.Current.CollectionPropertyInitialized = true;
JsonClassInfo classInfo = state.Current.JsonClassInfo;
if (state.Current.IsProcessingDictionary())
{
object dictValue = ReadStackFrame.CreateDictionaryValue(ref state);
// If value is not null, then we don't have a converter so apply the value.
if (dictValue != null)
{
state.Current.ReturnValue = dictValue;
state.Current.DetermineIfDictionaryCanBePopulated(state.Current.ReturnValue);
}
}
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);
}
return;
}
state.Current.CollectionPropertyInitialized = true;
object value = ReadStackFrame.CreateDictionaryValue(ref state);
if (value != null)
{
state.Current.DetermineIfDictionaryCanBePopulated(value);
if (state.Current.ReturnValue != null)
{
state.Current.JsonPropertyInfo.SetValueAsObject(state.Current.ReturnValue, value);
}
else
{
// A dictionary is being returned directly, or a nested dictionary.
state.Current.ReturnValue = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment