private static bool HandleEndArray(
            JsonSerializerOptions options,
            ref ReadStack state)
        {
            bool lastFrame = state.IsLastFrame;

            if (state.Current.Drain)
            {
                // The array is not being applied to the object.
                state.Pop();
                return(lastFrame);
            }

            IEnumerable value = ReadStackFrame.GetEnumerableValue(ref state.Current);

            if (state.Current.TempEnumerableValues != null)
            {
                // We have a converter; possibilities:
                // - Add value to current frame's current property or TempEnumerableValues.
                // - Add value to previous frame's current property or TempEnumerableValues.
                // - Set current property on current frame to value.
                // - Set current property on previous frame to value.
                // - Set ReturnValue if root frame and value is the actual return value.
                JsonEnumerableConverter converter = state.Current.JsonPropertyInfo.EnumerableConverter;
                Debug.Assert(converter != null);

                value = converter.CreateFromList(ref state, (IList)value, options);
                state.Current.TempEnumerableValues = null;
            }
            else if (state.Current.IsProcessingProperty(ClassType.Enumerable))
            {
                // We added the items to the list already.
                state.Current.EndProperty();
                return(false);
            }

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

            ApplyObjectToEnumerable(value, ref state);
            return(false);
        }
Example #2
0
 public NewtonsoftJsonSerializationConventions()
 {
     _defaultConverter         = new BlittableJsonConverter(this);
     _jsonEnumerableConverter  = new JsonEnumerableConverter(this);
     JsonContractResolver      = new DefaultRavenContractResolver(this);
     _ignoreByRefMembers       = false;
     _ignoreUnsafeMembers      = false;
     CustomizeJsonSerializer   = _ => { };
     CustomizeJsonDeserializer = _ => { };
 }
Example #3
0
        private static bool HandleEndArray(
            JsonSerializerOptions options,
            ref ReadStack state)
        {
            bool lastFrame = state.IsLastFrame;

            if (state.Current.Drain)
            {
                // The array is not being applied to the object.
                state.Pop();
                return(lastFrame);
            }

            IEnumerable value = ReadStackFrame.GetEnumerableValue(state.Current);

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

            bool setPropertyDirectly;

            if (state.Current.TempEnumerableValues != null)
            {
                JsonEnumerableConverter converter = state.Current.JsonPropertyInfo.EnumerableConverter;
                Debug.Assert(converter != null);

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

            bool valueReturning = state.Current.PopStackOnEndArray;

            if (state.Current.PopStackOnEndArray)
            {
                state.Pop();
            }

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

            ApplyObjectToEnumerable(value, options, ref state.Current, setPropertyDirectly: setPropertyDirectly);

            if (!valueReturning)
            {
                state.Current.ResetProperty();
            }

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