コード例 #1
0
        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);
                        }
                    }
                }
            }
        }
コード例 #2
0
        private static bool HandleNull(ref ReadObjectState current, JsonSerializerOptions options)
        {
            Debug.Assert(current.PropertyInfo != null);

            JsonPropertyInfo propertyInfo = current.PropertyInfo;

            if (!propertyInfo.CanBeNull)
            {
                throw new InvalidOperationException($"todo: {propertyInfo.PropertyType} can't be null");
            }

            if (current.IsEnumerable() || current.IsPropertyEnumerable())
            {
                ReadObjectState.SetReturnValue(null, options, ref current);
                return(false);
            }

            if (current.ReturnValue == null)
            {
                return(true);
            }

            if (!propertyInfo.IgnoreNullPropertyValueOnRead(options))
            {
                current.PropertyInfo.SetValueAsObject(current.ReturnValue, null, options);
            }

            return(false);
        }
コード例 #3
0
        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();
            }
        }
コード例 #4
0
        private static bool HandleValue(JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadObjectState current)
        {
            Debug.Assert(current.PropertyInfo != null);

            bool lastCall = (!current.IsEnumerable() && !current.IsPropertyEnumerable() && current.ReturnValue == null);

            current.PropertyInfo.Read(options, ref current, ref reader);

            return(lastCall);
        }
コード例 #5
0
        private static bool HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadObjectState current)
        {
            if (current.PropertyInfo == null)
            {
                return(false);
            }

            bool lastCall = (!current.IsEnumerable() && !current.IsPropertyEnumerable() && current.ReturnValue == null);

            current.PropertyInfo.Read(tokenType, options, ref current, ref reader);
            return(lastCall);
        }