Example #1
0
        /// <summary>
        /// Initializes the state for polymorphic cases and returns the appropriate converter.
        /// </summary>
        public JsonConverter?ResolvePolymorphicConverter(object value, Type typeToConvert, JsonSerializerOptions options)
        {
            Debug.Assert(value != null);
            Debug.Assert(PolymorphicSerializationState != PolymorphicSerializationState.PolymorphicReEntryStarted);

            if (PolymorphicSerializationState == PolymorphicSerializationState.PolymorphicReEntrySuspended)
            {
                // Quickly retrieve the polymorphic converter in case of a re-entrant continuation
                Debug.Assert(PolymorphicJsonTypeInfo != null && value.GetType() == PolymorphicJsonTypeInfo.PropertyType);
                return(PolymorphicJsonTypeInfo.ConverterBase);
            }

            Type runtimeType = value.GetType();

            if (runtimeType == typeToConvert)
            {
                return(null);
            }

            // For perf, avoid the dictionary lookup in GetOrAddJsonTypeInfo() for every element of a collection
            // if the current element is the same type as the previous element.
            if (PolymorphicJsonTypeInfo?.PropertyType != runtimeType)
            {
                JsonTypeInfo typeInfo = options.GetOrAddJsonTypeInfo(runtimeType);
                PolymorphicJsonTypeInfo = typeInfo.PropertyInfoForTypeInfo;
            }

            return(PolymorphicJsonTypeInfo.ConverterBase);
        }
Example #2
0
        /// <summary>
        /// Initializes the state for polymorphic cases and returns the appropriate converter.
        /// </summary>
        public JsonConverter InitializeReEntry(Type type, JsonSerializerOptions options)
        {
            // For perf, avoid the dictionary lookup in GetOrAddClass() for every element of a collection
            // if the current element is the same type as the previous element.
            if (PolymorphicJsonPropertyInfo?.PropertyType != type)
            {
                JsonTypeInfo typeInfo = options.GetOrAddJsonTypeInfo(type);
                PolymorphicJsonPropertyInfo = typeInfo.PropertyInfoForTypeInfo;
            }

            return(PolymorphicJsonPropertyInfo.ConverterBase);
        }
Example #3
0
        /// <summary>
        /// Configures the next stack frame for a polymorphic converter.
        /// </summary>
        public JsonConverter InitializePolymorphicReEntry(Type runtimeType, JsonSerializerOptions options)
        {
            Debug.Assert(PolymorphicSerializationState == PolymorphicSerializationState.None);

            // For perf, avoid the dictionary lookup in GetOrAddJsonTypeInfo() for every element of a collection
            // if the current element is the same type as the previous element.
            if (PolymorphicJsonTypeInfo?.PropertyType != runtimeType)
            {
                JsonTypeInfo typeInfo = options.GetOrAddJsonTypeInfo(runtimeType);
                PolymorphicJsonTypeInfo = typeInfo.PropertyInfoForTypeInfo;
            }

            PolymorphicSerializationState = PolymorphicSerializationState.PolymorphicReEntryStarted;
            return(PolymorphicJsonTypeInfo.ConverterBase);
        }