public void InitializeJsonPropertyInfo() { if (JsonClassInfo.ClassType == ClassType.Value || JsonClassInfo.ClassType == ClassType.Enumerable || JsonClassInfo.ClassType == ClassType.Dictionary || JsonClassInfo.ClassType == ClassType.IDictionaryConstructible) { JsonPropertyInfo = JsonClassInfo.GetPolicyProperty(); } else if (JsonClassInfo.ClassType == ClassType.KeyValuePair) { JsonPropertyInfo = JsonClassInfo.GetPolicyPropertyOfKeyValuePair(); } }
public void Initialize(Type type, JsonSerializerOptions options) { JsonClassInfo = options.GetOrAddClass(type); if (JsonClassInfo.ClassType == ClassType.Value || JsonClassInfo.ClassType == ClassType.Enumerable || JsonClassInfo.ClassType == ClassType.Dictionary) { JsonPropertyInfo = JsonClassInfo.GetPolicyProperty(); } else if (JsonClassInfo.ClassType == ClassType.IDictionaryConstructible) { JsonPropertyInfo = JsonClassInfo.GetPolicyProperty(); IsIDictionaryConstructible = true; } else if (JsonClassInfo.ClassType == ClassType.KeyValuePair) { JsonPropertyInfo = JsonClassInfo.GetPolicyPropertyOfKeyValuePair(); // Advance to the next property, since the first one is the KeyValuePair type itself, // not its first property (Key or Value). PropertyIndex++; } }
internal JsonPropertyInfo GetJsonPropertyInfoFromClassInfo(JsonClassInfo classInfo, JsonSerializerOptions options) { if (classInfo.ClassType == ClassType.KeyValuePair) { return(classInfo.GetPolicyPropertyOfKeyValuePair()); } if (classInfo.ClassType != ClassType.Object) { return(classInfo.GetPolicyProperty()); } Type objectType = classInfo.Type; if (!_objectJsonProperties.TryGetValue(objectType, out JsonPropertyInfo propertyInfo)) { propertyInfo = JsonClassInfo.CreateProperty(objectType, objectType, null, typeof(object), options); _objectJsonProperties[objectType] = propertyInfo; } return(propertyInfo); }
private static void HandleEndDictionary(JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadStack state) { if (state.Current.IsDictionaryProperty) { // We added the items to the dictionary already. state.Current.ResetProperty(); } else if (state.Current.IsIDictionaryConstructibleProperty) { Debug.Assert(state.Current.TempDictionaryValues != null); JsonDictionaryConverter converter = state.Current.JsonPropertyInfo.DictionaryConverter; state.Current.JsonPropertyInfo.SetValueAsObject(state.Current.ReturnValue, converter.CreateFromDictionary(ref state, state.Current.TempDictionaryValues, options)); state.Current.ResetProperty(); } else if (state.Current.IsKeyValuePairProperty) { JsonClassInfo elementClassInfo = state.Current.JsonPropertyInfo.ElementClassInfo; JsonPropertyInfo propertyInfo; if (elementClassInfo.ClassType == ClassType.KeyValuePair) { propertyInfo = elementClassInfo.GetPolicyPropertyOfKeyValuePair(); } else { propertyInfo = elementClassInfo.GetPolicyProperty(); } Debug.Assert(state.Current.TempDictionaryValues != null); state.Current.JsonPropertyInfo.SetValueAsObject( state.Current.ReturnValue, propertyInfo.CreateKeyValuePairInstance(ref state, state.Current.TempDictionaryValues, options)); state.Current.ResetProperty(); } else { object value; if (state.Current.TempDictionaryValues != null) { if (state.Current.IsKeyValuePair) { JsonClassInfo elementClassInfo = state.Current.JsonClassInfo.ElementClassInfo; JsonPropertyInfo propertyInfo; if (elementClassInfo.ClassType == ClassType.KeyValuePair) { propertyInfo = elementClassInfo.GetPolicyPropertyOfKeyValuePair(); } else { propertyInfo = elementClassInfo.GetPolicyProperty(); } value = propertyInfo.CreateKeyValuePairInstance(ref state, state.Current.TempDictionaryValues, options); } else { JsonDictionaryConverter converter = state.Current.JsonPropertyInfo.DictionaryConverter; value = converter.CreateFromDictionary(ref state, state.Current.TempDictionaryValues, options); } } else { value = state.Current.ReturnValue; } if (state.IsLastFrame) { // Set the return value directly since this will be returned to the user. state.Current.Reset(); state.Current.ReturnValue = value; } else { state.Pop(); ApplyObjectToEnumerable(value, ref state, ref reader); } } }
private static bool HandleEnumerable( JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, ref WriteStack state) { Debug.Assert(state.Current.JsonPropertyInfo.ClassType == ClassType.Enumerable); if (state.Current.Enumerator == null) { IEnumerable enumerable = (IEnumerable)state.Current.JsonPropertyInfo.GetValueAsObject(state.Current.CurrentValue); if (enumerable == null) { if (!state.Current.JsonPropertyInfo.IgnoreNullValues) { // Write a null object or enumerable. state.Current.WriteObjectOrArrayStart(ClassType.Enumerable, writer, writeNull: true); } return(true); } state.Current.Enumerator = enumerable.GetEnumerator(); state.Current.WriteObjectOrArrayStart(ClassType.Enumerable, writer); } if (state.Current.Enumerator.MoveNext()) { // Check for polymorphism. if (elementClassInfo.ClassType == ClassType.Unknown) { object currentValue = state.Current.Enumerator.Current; GetRuntimeClassInfo(currentValue, ref elementClassInfo, options); } if (elementClassInfo.ClassType == ClassType.Value) { elementClassInfo.GetPolicyProperty().WriteEnumerable(ref state.Current, writer); } else if (state.Current.Enumerator.Current == null) { // Write a null object or enumerable. writer.WriteNullValue(); } else { // An object or another enumerator requires a new stack frame. object nextValue = state.Current.Enumerator.Current; state.Push(elementClassInfo, nextValue); if (elementClassInfo.ClassType == ClassType.KeyValuePair) { state.Current.JsonPropertyInfo = elementClassInfo.GetPolicyPropertyOfKeyValuePair(); state.Current.PropertyIndex++; } } return(false); } // We are done enumerating. writer.WriteEndArray(); if (state.Current.PopStackOnEnd) { state.Pop(); } else { state.Current.EndArray(); } return(true); }