Esempio n. 1
0
 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.ImmutableDictionary)
     {
         JsonPropertyInfo      = JsonClassInfo.GetPolicyProperty();
         IsImmutableDictionary = true;
     }
 }
Esempio n. 2
0
 public void InitializeJsonPropertyInfo()
 {
     if (JsonClassInfo.ClassType == ClassType.Value ||
         JsonClassInfo.ClassType == ClassType.Enumerable ||
         JsonClassInfo.ClassType == ClassType.Dictionary ||
         JsonClassInfo.ClassType == ClassType.ImmutableDictionary)
     {
         JsonPropertyInfo = JsonClassInfo.GetPolicyProperty();
     }
 }
Esempio n. 3
0
 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++;
     }
 }
Esempio n. 4
0
 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();
     }
 }
Esempio n. 5
0
        internal JsonPropertyInfo GetJsonPropertyInfoFromClassInfo(JsonClassInfo classInfo, JsonSerializerOptions options)
        {
            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);
                }
            }
        }
Esempio n. 7
0
        private static bool HandleDictionary(
            JsonClassInfo elementClassInfo,
            JsonSerializerOptions options,
            Utf8JsonWriter writer,
            ref WriteStack state)
        {
            JsonPropertyInfo jsonPropertyInfo = state.Current.JsonPropertyInfo;

            if (state.Current.Enumerator == null)
            {
                IEnumerable enumerable;

                enumerable = (IEnumerable)jsonPropertyInfo.GetValueAsObject(state.Current.CurrentValue);
                if (enumerable == null)
                {
                    // Write a null object or enumerable.
                    state.Current.WriteObjectOrArrayStart(ClassType.Dictionary, writer, writeNull: true);
                    return(true);
                }

                state.Current.Enumerator = ((IDictionary)enumerable).GetEnumerator();
                state.Current.WriteObjectOrArrayStart(ClassType.Dictionary, writer);
            }

            if (state.Current.Enumerator.MoveNext())
            {
                // Handle DataExtension.
                if (ReferenceEquals(jsonPropertyInfo, state.Current.JsonClassInfo.DataExtensionProperty))
                {
                    WriteExtensionData(writer, ref state.Current);
                }
                else
                {
                    // Check for polymorphism.
                    if (elementClassInfo.ClassType == ClassType.Unknown)
                    {
                        object currentValue = ((IDictionaryEnumerator)state.Current.Enumerator).Entry.Value;
                        GetRuntimeClassInfo(currentValue, ref elementClassInfo, options);
                    }

                    if (elementClassInfo.ClassType == ClassType.Value)
                    {
                        elementClassInfo.GetPolicyProperty().WriteDictionary(ref state.Current, writer);
                    }
                    else if (state.Current.Enumerator.Current == null)
                    {
                        writer.WriteNull(jsonPropertyInfo.Name);
                    }
                    else
                    {
                        // An object or another enumerator requires a new stack frame.
                        var    enumerator = (IDictionaryEnumerator)state.Current.Enumerator;
                        object value      = enumerator.Value;
                        state.Push(elementClassInfo, value);
                        state.Current.KeyName = (string)enumerator.Key;
                    }
                }

                return(false);
            }

            // We are done enumerating.
            writer.WriteEndObject();

            if (state.Current.PopStackOnEnd)
            {
                state.Pop();
            }
            else
            {
                state.Current.EndDictionary();
            }

            return(true);
        }
        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, 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);
                }

                return(false);
            }

            // We are done enumerating.
            writer.WriteEndArray();

            if (state.Current.PopStackOnEnd)
            {
                state.Pop();
            }
            else
            {
                state.Current.EndArray();
            }

            return(true);
        }