public override void Initialize(
            Type parentClassType,
            Type declaredPropertyType,
            Type runtimePropertyType,
            PropertyInfo propertyInfo,
            Type elementType,
            JsonSerializerOptions options)
        {
            base.Initialize(parentClassType, declaredPropertyType, runtimePropertyType, propertyInfo, elementType, options);

            if (propertyInfo != null)
            {
                if (propertyInfo.GetMethod?.IsPublic == true)
                {
                    HasGetter = true;
                    Get       = (Func <TClass, TDeclaredProperty>)Delegate.CreateDelegate(typeof(Func <TClass, TDeclaredProperty>), propertyInfo.GetGetMethod());
                }

                if (propertyInfo.SetMethod?.IsPublic == true)
                {
                    HasSetter = true;
                    Set       = (Action <TClass, TDeclaredProperty>)Delegate.CreateDelegate(typeof(Action <TClass, TDeclaredProperty>), propertyInfo.GetSetMethod());
                }
            }
            else
            {
                IsPropertyPolicy = true;
                HasGetter        = true;
                HasSetter        = true;
                ValueConverter   = DefaultConverters <TRuntimeProperty> .s_converter;
            }

            GetPolicies(options);
        }
Beispiel #2
0
        public override void Initialize(
            Type parentClassType,
            Type declaredPropertyType,
            Type runtimePropertyType,
            PropertyInfo propertyInfo,
            Type elementType,
            JsonSerializerOptions options)
        {
            base.Initialize(parentClassType, declaredPropertyType, runtimePropertyType, propertyInfo, elementType, options);

            if (propertyInfo != null)
            {
                if (propertyInfo.GetMethod?.IsPublic == true)
                {
                    HasGetter = true;
                    Get       = MemberAccessor.CreatePropertyGetter <TClass, TDeclaredProperty>(propertyInfo);
                }

                if (propertyInfo.SetMethod?.IsPublic == true)
                {
                    HasSetter = true;
                    Set       = MemberAccessor.CreatePropertySetter <TClass, TDeclaredProperty>(propertyInfo);
                }
            }
            else
            {
                IsPropertyPolicy = true;
                HasGetter        = true;
                HasSetter        = true;
                ValueConverter   = DefaultConverters <TRuntimeProperty> .s_converter;
            }

            GetPolicies(options);
        }
        internal static void WriteDictionary <TProperty>(
            JsonValueConverter <TProperty> converter,
            JsonSerializerOptions options,
            ref WriteStackFrame current,
            Utf8JsonWriter writer)
        {
            if (converter == null)
            {
                return;
            }

            Debug.Assert(current.Enumerator != null);

            string    key;
            TProperty value;

            if (current.Enumerator is IEnumerator <KeyValuePair <string, TProperty> > enumerator)
            {
                // Avoid boxing for strongly-typed enumerators such as returned from IDictionary<string, TRuntimeProperty>
                value = enumerator.Current.Value;
                key   = enumerator.Current.Key;
            }
            else
            {
                // Todo: support non-generic Dictionary here (IDictionaryEnumerator)
                throw new NotSupportedException();
            }

            if (value == null)
            {
                writer.WriteNull(key);
            }
            else
            {
#if true
                // temporary behavior until the writer can accept escaped string.
                byte[] utf8Key = Encoding.UTF8.GetBytes(key);
                converter.Write(utf8Key, value, writer);
#else
                byte[] pooledKey = null;
                byte[] utf8Key   = Encoding.UTF8.GetBytes(key);
                int    length    = JsonWriterHelper.GetMaxEscapedLength(utf8Key.Length, 0);

                Span <byte> escapedKey = length <= JsonConstants.StackallocThreshold ?
                                         stackalloc byte[length] :
                                         (pooledKey = ArrayPool <byte> .Shared.Rent(length));

                JsonWriterHelper.EscapeString(utf8Key, escapedKey, 0, out int written);

                converter.Write(escapedKey.Slice(0, written), value, writer);

                if (pooledKey != null)
                {
                    // We clear the array because it is "user data" (although a property name).
                    new Span <byte>(pooledKey, 0, written).Clear();
                    ArrayPool <byte> .Shared.Return(pooledKey);
                }
#endif
            }
        }
Beispiel #4
0
        internal static void WriteDictionary <TProperty>(
            JsonValueConverter <TProperty> converter,
            JsonSerializerOptions options,
            ref WriteStackFrame current,
            Utf8JsonWriter writer)
        {
            if (converter == null)
            {
                return;
            }

            Debug.Assert(current.Enumerator != null);

            string    key;
            TProperty value;

            if (current.Enumerator is IEnumerator <KeyValuePair <string, TProperty> > enumerator)
            {
                // Avoid boxing for strongly-typed enumerators such as returned from IDictionary<string, TRuntimeProperty>
                value = enumerator.Current.Value;
                key   = enumerator.Current.Key;
            }
            else if (current.Enumerator is IEnumerator <KeyValuePair <string, object> > polymorphicEnumerator)
            {
                value = (TProperty)polymorphicEnumerator.Current.Value;
                key   = polymorphicEnumerator.Current.Key;
            }
            else if (current.IsImmutableDictionary || current.IsImmutableDictionaryProperty)
            {
                value = (TProperty)((DictionaryEntry)current.Enumerator.Current).Value;
                key   = (string)((DictionaryEntry)current.Enumerator.Current).Key;
            }
            else
            {
                // Todo: support non-generic Dictionary here (IDictionaryEnumerator)
                throw new NotSupportedException();
            }

            if (value == null)
            {
                writer.WriteNull(key);
            }
            else
            {
                JsonEncodedText escapedKey = JsonEncodedText.Encode(key);
                converter.Write(escapedKey, value, writer);
            }
        }
        internal override void GetPolicies(JsonSerializerOptions options)
        {
            Type propertyType = PropertyType;
            bool isNullable   = propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>);

            if (isNullable)
            {
                propertyType = Nullable.GetUnderlyingType(propertyType);
            }

            // For Enums, support both the type Enum plus strongly-typed Enums.
            if (propertyType.IsEnum || propertyType == typeof(Enum))
            {
                ValueConverter = s_enumConverter;
            }
            else
            {
                ValueConverter = (JsonValueConverter <TProperty>)DefaultConverters.GetDefaultPropertyValueConverter(propertyType, isNullable);
            }

            base.GetPolicies(options);
        }
Beispiel #6
0
        internal JsonPropertyInfoCommon(
            Type parentClassType,
            Type declaredPropertyType,
            Type runtimePropertyType,
            PropertyInfo propertyInfo,
            Type elementType,
            JsonSerializerOptions options) :
            base(parentClassType, declaredPropertyType, runtimePropertyType, propertyInfo, elementType, options)
        {
            if (propertyInfo != null)
            {
                if (propertyInfo.GetMethod?.IsPublic == true)
                {
                    HasGetter = true;
                    Get       = (Func <TClass, TDeclaredProperty>)Delegate.CreateDelegate(typeof(Func <TClass, TDeclaredProperty>), propertyInfo.GetGetMethod());
                }

                if (propertyInfo.SetMethod?.IsPublic == true)
                {
                    HasSetter = true;
                    Set       = (Action <TClass, TDeclaredProperty>)Delegate.CreateDelegate(typeof(Action <TClass, TDeclaredProperty>), propertyInfo.GetSetMethod());
                }
            }
            else
            {
                _isPropertyPolicy = true;
                HasGetter         = true;
                HasSetter         = true;

                if (ClassType == ClassType.Dictionary)
                {
                    ValueConverter = DefaultConverters <TRuntimeProperty> .s_converter;
                }
            }

            GetPolicies(options);
        }
 internal override void GetPolicies(JsonSerializerOptions options)
 {
     ValueConverter = DefaultConverters <TRuntimeProperty> .s_converter;
     base.GetPolicies(options);
 }
Beispiel #8
0
 public override void GetPolicies()
 {
     ValueConverter = DefaultConverters <TRuntimeProperty> .s_converter;
     base.GetPolicies();
 }
        internal override void GetPolicies(JsonSerializerOptions options)
        {
            ValueConverter = DefaultConverters <TUnderlying> .s_converter;

            base.GetPolicies(options);
        }