예제 #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);
        }
예제 #2
0
        private static bool HandleNull(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
        {
            if (state.Current.Skip())
            {
                return(false);
            }

            // If we don't have a valid property, that means we read "null" for a root object so just return.
            if (state.Current.JsonPropertyInfo == null)
            {
                Debug.Assert(state.IsLastFrame);
                Debug.Assert(state.Current.ReturnValue == null);
                return(true);
            }

            JsonPropertyInfo propertyInfo = state.Current.JsonPropertyInfo;

            if (!propertyInfo.CanBeNull)
            {
                ThrowHelper.ThrowJsonReaderException_DeserializeCannotBeNull(reader, state);
            }

            if (state.Current.IsEnumerable() || state.Current.IsPropertyEnumerable())
            {
                ReadStackFrame.SetReturnValue(null, options, ref state.Current);
                return(false);
            }

            if (state.Current.ReturnValue == null)
            {
                Debug.Assert(state.IsLastFrame);
                return(true);
            }

            if (!propertyInfo.IgnoreNullPropertyValueOnRead(options))
            {
                state.Current.JsonPropertyInfo.SetValueAsObject(state.Current.ReturnValue, null, options);
            }

            return(false);
        }