Example #1
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);
        }
Example #2
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();
            }
        }
Example #3
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);
        }
        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);
        }
        private static bool HandleEndArray(
            JsonSerializerOptions options,
            ref ReadObjectState current,
            ref List <ReadObjectState> previous,
            ref int arrayIndex)
        {
            IEnumerable value = ReadObjectState.GetEnumerableValue(current);

            if (value == null)
            {
                // We added the items to the list property already.
                current.ResetProperty();
                return(false);
            }

            bool lastFrame = (arrayIndex == 0);

            bool setPropertyDirectly;

            if (current.TempEnumerableValues != null)
            {
                JsonEnumerableConverter converter = current.PropertyInfo.EnumerableConverter;
                if (converter == null)
                {
                    converter = current.ClassInfo.EnumerableConverter;
                }

                Type elementType = current.GetElementType();
                value = converter.CreateFromList(elementType, (IList)value);
                setPropertyDirectly = true;
            }
            else
            {
                setPropertyDirectly = false;
            }

            if (current.PopStackOnEndArray)
            {
                ReadObjectState previousFrame = default;
                GetPreviousState(ref previous, ref previousFrame, --arrayIndex);
                current = previousFrame;
            }

            if (lastFrame)
            {
                if (current.ReturnValue == null)
                {
                    // Returning a converted list or object.
                    current.Reset();
                    current.ReturnValue = value;
                    return(true);
                }
                else if (current.IsEnumerable())
                {
                    // Returning a non-converted list.
                    return(true);
                }
                // else there must be an outer object, so we'll return false here.
            }

            ReadObjectState.SetReturnValue(value, options, ref current, setPropertyDirectly: setPropertyDirectly);
            return(false);
        }