コード例 #1
0
        internal sealed override bool WriteCoreAsObject(
            BinaryWriter writer,
            object value,
            BinarySerializerOptions options,
            ref WriteStack state)
        {
            Debug.Fail("We should never get here.");

            throw new InvalidOperationException();
        }
コード例 #2
0
        internal override bool GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
        {
            T value = Get !(obj);

            if (Options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.IgnoreCycles &&
                !Converter.IsValueType && value != null &&
                state.ReferenceResolver.ContainsReferenceForCycleDetection(value))
            {
                // If a reference cycle is detected, treat value as null.
                value = default !;
コード例 #3
0
        protected internal override bool OnWriteResume(
            Utf8JsonWriter writer,
            TDictionary value,
            JsonSerializerOptions options,
            ref WriteStack state)
        {
            IEnumerator <KeyValuePair <TKey, TValue> > enumerator;

            if (state.Current.CollectionEnumerator == null)
            {
                enumerator = value.GetEnumerator();
                if (!enumerator.MoveNext())
                {
                    enumerator.Dispose();
                    return(true);
                }
            }
            else
            {
                enumerator = (IEnumerator <KeyValuePair <TKey, TValue> >)state.Current.CollectionEnumerator;
            }

            JsonTypeInfo typeInfo = state.Current.JsonTypeInfo;

            _keyConverter ??= GetConverter <TKey>(typeInfo.KeyTypeInfo !);
            _valueConverter ??= GetConverter <TValue>(typeInfo.ElementTypeInfo !);

            do
            {
                if (ShouldFlush(writer, ref state))
                {
                    state.Current.CollectionEnumerator = enumerator;
                    return(false);
                }

                if (state.Current.PropertyState < StackFramePropertyState.Name)
                {
                    state.Current.PropertyState = StackFramePropertyState.Name;
                    TKey key = enumerator.Current.Key;
                    _keyConverter.WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionDataProperty);
                }

                TValue element = enumerator.Current.Value;
                if (!_valueConverter.TryWrite(writer, element, options, ref state))
                {
                    state.Current.CollectionEnumerator = enumerator;
                    return(false);
                }

                state.Current.EndDictionaryElement();
            } while (enumerator.MoveNext());

            enumerator.Dispose();
            return(true);
        }
コード例 #4
0
        public override sealed void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
        {
            // Bridge from resumable to value converters.
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            WriteStack state = default;

            state.Initialize(typeof(T), options, supportContinuation: false);
            TryWrite(writer, value, options, ref state);
        }
コード例 #5
0
        public override sealed void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
        {
            // Bridge from resumable to value converters.
            if (options == null)
            {
                options = JsonSerializerOptions.s_defaultOptions;
            }

            WriteStack state = default;

            state.InitializeRoot(typeof(T), options, supportContinuation: false);
            TryWrite(writer, value, options, ref state);
        }
コード例 #6
0
        internal override bool GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
        {
            T value = Get !(obj);

            if (Options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.IgnoreCycles &&
                value != null &&
                // .NET types that are serialized as JSON primitive values don't need to be tracked for cycle detection e.g: string.
                // However JsonConverter<object> that uses ConverterStrategy == Value is an exception.
                (Converter.CanBePolymorphic || (!Converter.IsValueType && ConverterStrategy != ConverterStrategy.Value)) &&
                state.ReferenceResolver.ContainsReferenceForCycleDetection(value))
            {
                // If a reference cycle is detected, treat value as null.
                value = default !;
コード例 #7
0
        protected string GetKeyName(string key, ref WriteStack state, JsonSerializerOptions options)
        {
            if (options.DictionaryKeyPolicy != null && !state.Current.IgnoreDictionaryKeyPolicy)
            {
                key = options.DictionaryKeyPolicy.ConvertName(key);

                if (key == null)
                {
                    ThrowHelper.ThrowInvalidOperationException_NamingPolicyReturnNull(options.DictionaryKeyPolicy);
                }
            }

            return(key);
        }
コード例 #8
0
        public sealed override void Write(BinaryWriter writer, T value, BinarySerializerOptions options)
        {
            // Bridge from resumable to value converters.
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            WriteStack state     = default;
            Type       inputType = value == null ? typeof(T) : value.GetType();

            state.Initialize(inputType, options, supportContinuation: false);
            TryWrite(writer, value, options, ref state);
        }
コード例 #9
0
        internal override bool OnTryWrite(
            Utf8JsonWriter writer,
            T value,
            JsonSerializerOptions options,
            ref WriteStack state)
        {
            JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;

            if (jsonTypeInfo.PropertyCacheArray == null)
            {
                jsonTypeInfo.InitializeSerializePropCache();
            }

            return(base.OnTryWrite(writer, value, options, ref state));
        }
コード例 #10
0
        internal sealed override bool WriteCoreAsObject(
            Utf8JsonWriter writer,
            object?value,
            JsonSerializerOptions options,
            ref WriteStack state)
        {
            // Value types can never have a null except for Nullable<T>.
            if (value == null && IsValueType && Nullable.GetUnderlyingType(TypeToConvert) == null)
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(TypeToConvert);
            }

            T actualValue = (T)value !;

            return(WriteCore(writer, actualValue, options, ref state));
        }
コード例 #11
0
ファイル: JsonPropertyInfoOfT.cs プロジェクト: naricc/runtime
        internal override bool GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
        {
            T value = Get !(obj);

            if (
#if NETCOREAPP
                !typeof(T).IsValueType && // treated as a constant by recent versions of the JIT.
#else
                !Converter.IsValueType &&
#endif
                Options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.IgnoreCycles &&
                value is not null &&
                !state.IsContinuation &&
                // .NET types that are serialized as JSON primitive values don't need to be tracked for cycle detection e.g: string.
                ConverterStrategy != ConverterStrategy.Value &&
                state.ReferenceResolver.ContainsReferenceForCycleDetection(value))
            {
                // If a reference cycle is detected, treat value as null.
                value = default !;
コード例 #12
0
        protected override bool OnWriteResume(
            Utf8JsonWriter writer,
            TCollection value,
            JsonSerializerOptions options,
            ref WriteStack state)
        {
            IEnumerator <TElement> enumerator;

            if (state.Current.CollectionEnumerator == null)
            {
                enumerator = value.GetEnumerator();
                if (!enumerator.MoveNext())
                {
                    return(true);
                }
            }
            else
            {
                Debug.Assert(state.Current.CollectionEnumerator is IEnumerator <TElement>);
                enumerator = (IEnumerator <TElement>)state.Current.CollectionEnumerator;
            }

            JsonConverter <TElement> converter = GetElementConverter(ref state);

            do
            {
                if (ShouldFlush(writer, ref state))
                {
                    state.Current.CollectionEnumerator = enumerator;
                    return(false);
                }

                TElement element = enumerator.Current;
                if (!converter.TryWrite(writer, element, options, ref state))
                {
                    state.Current.CollectionEnumerator = enumerator;
                    return(false);
                }
            } while (enumerator.MoveNext());

            return(true);
        }
コード例 #13
0
        internal sealed override bool OnTryWrite(
            Utf8JsonWriter writer,
            TCollection dictionary,
            JsonSerializerOptions options,
            ref WriteStack state)
        {
            if (dictionary == null)
            {
                writer.WriteNullValue();
                return(true);
            }

            if (!state.Current.ProcessedStartToken)
            {
                state.Current.ProcessedStartToken = true;
                writer.WriteStartObject();

                if (options.ReferenceHandler != null)
                {
                    if (JsonSerializer.WriteReferenceForObject(this, dictionary, ref state, writer) == MetadataPropertyName.Ref)
                    {
                        return(true);
                    }
                }

                state.Current.DeclaredJsonPropertyInfo = state.Current.JsonClassInfo.ElementClassInfo !.PropertyInfoForClassInfo;
            }

            bool success = OnWriteResume(writer, dictionary, options, ref state);

            if (success)
            {
                if (!state.Current.ProcessedEndToken)
                {
                    state.Current.ProcessedEndToken = true;
                    writer.WriteEndObject();
                }
            }

            return(success);
        }
コード例 #14
0
        public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
        {
            if (options is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(options));
            }

            // Bridge from resumable to value converters.

            WriteStack state = default;

            state.Initialize(typeof(T), options, supportContinuation: false, supportAsync: false);
            try
            {
                TryWrite(writer, value, options, ref state);
            }
            catch
            {
                state.DisposePendingDisposablesOnException();
                throw;
            }
        }
コード例 #15
0
 internal abstract bool GetMemberAndWriteJsonExtensionData(object obj, ref WriteStack state, Utf8JsonWriter writer);
コード例 #16
0
        internal sealed override bool OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
        {
            // Minimize boxing for structs by only boxing once here
            object objectValue = value !;

            if (!state.SupportContinuation)
            {
                writer.WriteStartObject();

                if (options.ReferenceHandler != null)
                {
                    if (JsonSerializer.WriteReferenceForObject(this, objectValue, ref state, writer) == MetadataPropertyName.Ref)
                    {
                        return(true);
                    }
                }

                JsonPropertyInfo?dataExtensionProperty = state.Current.JsonClassInfo.DataExtensionProperty;

                int propertyCount = 0;
                JsonPropertyInfo[]? propertyCacheArray = state.Current.JsonClassInfo.PropertyCacheArray;
                if (propertyCacheArray != null)
                {
                    propertyCount = propertyCacheArray.Length;
                }

                for (int i = 0; i < propertyCount; i++)
                {
                    JsonPropertyInfo jsonPropertyInfo = propertyCacheArray ![i];
コード例 #17
0
 internal override bool OnTryWrite(Utf8JsonWriter writer, TValueOption value, JsonSerializerOptions options, ref WriteStack state)
 {
     if (value.Equals(default))
コード例 #18
0
 protected abstract bool OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state);
コード例 #19
0
        internal sealed override bool OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
        {
            bool success;

            if (value == null)
            {
                writer.WriteNullValue();
                success = true;
            }
            else
            {
                bool shouldWritePreservedReferences = options.ReferenceHandling.ShouldWritePreservedReferences();

                if (!state.Current.ProcessedStartToken)
                {
                    state.Current.ProcessedStartToken = true;

                    if (!shouldWritePreservedReferences)
                    {
                        writer.WriteStartArray();
                    }
                    else
                    {
                        MetadataPropertyName metadata = JsonSerializer.WriteReferenceForCollection(this, value, ref state, writer);
                        if (metadata == MetadataPropertyName.Ref)
                        {
                            return(true);
                        }

                        state.Current.MetadataPropertyName = metadata;
                    }

                    state.Current.DeclaredJsonPropertyInfo = state.Current.JsonClassInfo.ElementClassInfo !.PropertyInfoForClassInfo;
                }

                success = OnWriteResume(writer, value, options, ref state);
                if (success)
                {
                    if (!state.Current.ProcessedEndToken)
                    {
                        state.Current.ProcessedEndToken = true;
                        writer.WriteEndArray();

                        if (state.Current.MetadataPropertyName == MetadataPropertyName.Id)
                        {
                            // Write the EndObject for $values.
                            writer.WriteEndObject();
                        }
                    }
                }
            }

            return(success);
        }
コード例 #20
0
 internal override bool OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
 => _sourceConverter.OnTryWrite(writer, CastOnWrite(value), options, ref state);
コード例 #21
0
        // This non-generic API is sealed as it just forwards to the generic version.
        internal sealed override bool TryWriteAsObject(BinaryWriter writer, object value, BinarySerializerOptions options, ref WriteStack state)
        {
            T valueOfT = (T)value !;

            return(TryWrite(writer, valueOfT, options, ref state));
        }
コード例 #22
0
 // Provide a default implementation for value converters.
 internal virtual bool OnTryWrite(BinaryWriter writer, T value, BinarySerializerOptions options, ref WriteStack state)
 {
     Write(writer, value, options);
     return(true);
 }
コード例 #23
0
        protected internal override bool OnWriteResume(BinaryWriter writer, TCollection dictionary, BinarySerializerOptions options, ref WriteStack state)
        {
            IEnumerator <KeyValuePair <TKey, TValue> > enumerator;

            if (state.Current.CollectionEnumerator == null)
            {
                enumerator = dictionary.GetEnumerator();
                if (!enumerator.MoveNext())
                {
                    return(true);
                }
            }
            else
            {
                enumerator = (IEnumerator <KeyValuePair <TKey, TValue> >)state.Current.CollectionEnumerator;
            }

            if (!state.SupportContinuation)
            {
                do
                {
                    WriteKey(writer, enumerator.Current.Key, options, ref state);
                    WriteValue(writer, enumerator.Current.Value, options, ref state);

                    state.Current.EndDictionaryElement();
                } while (enumerator.MoveNext());
            }
            else
            {
                do
                {
                    if (ShouldFlush(writer, ref state))
                    {
                        state.Current.CollectionEnumerator = enumerator;
                        return(false);
                    }

                    if (!WriteKey(writer, enumerator.Current.Key, options, ref state))
                    {
                        state.Current.CollectionEnumerator = enumerator;
                        return(false);
                    }

                    if (!WriteValue(writer, enumerator.Current.Value, options, ref state))
                    {
                        state.Current.CollectionEnumerator = enumerator;
                        return(false);
                    }

                    state.Current.EndDictionaryElement();
                } while (enumerator.MoveNext());
            }

            return(true);
        }
コード例 #24
0
        internal sealed override bool OnTryWrite(
            Utf8JsonWriter writer,
            T value,
            JsonSerializerOptions options,
            ref WriteStack state)
        {
            JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;

            object obj = value; // box once

            if (!state.SupportContinuation)
            {
                writer.WriteStartObject();
                if (options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.Preserve)
                {
                    if (JsonSerializer.WriteReferenceForObject(this, obj, ref state, writer) == MetadataPropertyName.Ref)
                    {
                        return(true);
                    }
                }

                if (obj is IJsonOnSerializing onSerializing)
                {
                    onSerializing.OnSerializing();
                }

                List <KeyValuePair <string, JsonPropertyInfo?> > properties = jsonTypeInfo.PropertyCache !.List;
                for (int i = 0; i < properties.Count; i++)
                {
                    JsonPropertyInfo jsonPropertyInfo = properties[i].Value !;
                    if (jsonPropertyInfo.ShouldSerialize)
                    {
                        // Remember the current property for JsonPath support if an exception is thrown.
                        state.Current.DeclaredJsonPropertyInfo = jsonPropertyInfo;
                        state.Current.NumberHandling           = jsonPropertyInfo.NumberHandling;

                        bool success = jsonPropertyInfo.GetMemberAndWriteJson(obj, ref state, writer);
                        // Converters only return 'false' when out of data which is not possible in fast path.
                        Debug.Assert(success);

                        state.Current.EndProperty();
                    }
                }

                // Write extension data after the normal properties.
                JsonPropertyInfo?dataExtensionProperty = jsonTypeInfo.DataExtensionProperty;
                if (dataExtensionProperty?.ShouldSerialize == true)
                {
                    // Remember the current property for JsonPath support if an exception is thrown.
                    state.Current.DeclaredJsonPropertyInfo = dataExtensionProperty;
                    state.Current.NumberHandling           = dataExtensionProperty.NumberHandling;

                    bool success = dataExtensionProperty.GetMemberAndWriteJsonExtensionData(obj, ref state, writer);
                    Debug.Assert(success);

                    state.Current.EndProperty();
                }

                writer.WriteEndObject();
            }
            else
            {
                if (!state.Current.ProcessedStartToken)
                {
                    writer.WriteStartObject();
                    if (options.ReferenceHandlingStrategy == ReferenceHandlingStrategy.Preserve)
                    {
                        if (JsonSerializer.WriteReferenceForObject(this, obj, ref state, writer) == MetadataPropertyName.Ref)
                        {
                            return(true);
                        }
                    }

                    if (obj is IJsonOnSerializing onSerializing)
                    {
                        onSerializing.OnSerializing();
                    }

                    state.Current.ProcessedStartToken = true;
                }

                List <KeyValuePair <string, JsonPropertyInfo?> >?propertyList = jsonTypeInfo.PropertyCache !.List !;
                while (state.Current.EnumeratorIndex < propertyList.Count)
                {
                    JsonPropertyInfo?jsonPropertyInfo = propertyList ![state.Current.EnumeratorIndex].Value;
コード例 #25
0
ファイル: JsonConverter.cs プロジェクト: lateralusX/runtime
 internal abstract bool TryWriteAsObject(Utf8JsonWriter writer, object?value, JsonSerializerOptions options, ref WriteStack state);
コード例 #26
0
        internal override bool OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
        {
            JsonTypeInfo jsonTypeInfo = state.Current.JsonTypeInfo;

            Debug.Assert(options == jsonTypeInfo.Options);

            if (!state.SupportContinuation &&
                jsonTypeInfo is JsonTypeInfo <T> info &&
                info.SerializeHandler != null &&
                info.Options._context?.CanUseSerializationLogic == true)
            {
                info.SerializeHandler(writer, value);
                return(true);
            }

            if (_converterStrategy == ConverterStrategy.Object && jsonTypeInfo.PropertyCache == null)
            {
                jsonTypeInfo.InitializePropCache();
            }

            return(Converter.OnTryWrite(writer, value, options, ref state));
        }
コード例 #27
0
 internal override void WriteAsPropertyName(Utf8JsonWriter writer, short value, JsonSerializerOptions options, ref WriteStack state)
 {
     writer.WritePropertyName(value);
 }
コード例 #28
0
ファイル: CharConverter.cs プロジェクト: JulieLeeMSFT/runtime
        internal override void WriteAsPropertyName(Utf8JsonWriter writer, char value, JsonSerializerOptions options, ref WriteStack state)
        {
            writer.WritePropertyName(
#if BUILDING_INBOX_LIBRARY
                MemoryMarshal.CreateSpan(ref value, 1)
#else
                value.ToString()
#endif
                );
        }
コード例 #29
0
 internal override void WriteWithQuotes(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options, ref WriteStack state)
 {
     writer.WritePropertyName(value);
 }
コード例 #30
0
ファイル: JsonConverter.cs プロジェクト: lateralusX/runtime
 internal bool ShouldFlush(Utf8JsonWriter writer, ref WriteStack state)
 {
     // If surpassed flush threshold then return false which will flush stream.
     return(state.FlushThreshold > 0 && writer.BytesPending > state.FlushThreshold);
 }