Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling = JsonSerializerSettings.DefaultReferenceLoopHandling;
       _missingMemberHandling = JsonSerializerSettings.DefaultMissingMemberHandling;
       _nullValueHandling = JsonSerializerSettings.DefaultNullValueHandling;
       _defaultValueHandling = JsonSerializerSettings.DefaultDefaultValueHandling;
       _objectCreationHandling = JsonSerializerSettings.DefaultObjectCreationHandling;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
     _referenceLoopHandling = JsonSerializerSettings.DefaultReferenceLoopHandling;
       _missingMemberHandling = JsonSerializerSettings.DefaultMissingMemberHandling;
       _nullValueHandling = JsonSerializerSettings.DefaultNullValueHandling;
       _defaultValueHandling = JsonSerializerSettings.DefaultDefaultValueHandling;
       _objectCreationHandling = JsonSerializerSettings.DefaultObjectCreationHandling;
       _preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
       _typeNameHandling = JsonSerializerSettings.DefaultTypeNameHandling;
       _binder = DefaultSerializationBinder.Instance;
 }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
        /// </summary>
        public JsonSerializer()
        {
            _referenceLoopHandling      = JsonSerializerSettings.DefaultReferenceLoopHandling;
            _missingMemberHandling      = JsonSerializerSettings.DefaultMissingMemberHandling;
            _nullValueHandling          = JsonSerializerSettings.DefaultNullValueHandling;
            _defaultValueHandling       = JsonSerializerSettings.DefaultDefaultValueHandling;
            _objectCreationHandling     = JsonSerializerSettings.DefaultObjectCreationHandling;
            _preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
            _constructorHandling        = JsonSerializerSettings.DefaultConstructorHandling;
            _typeNameHandling           = JsonSerializerSettings.DefaultTypeNameHandling;
            _context = JsonSerializerSettings.DefaultContext;
            _binder  = DefaultSerializationBinder.Instance;

            _culture          = JsonSerializerSettings.DefaultCulture;
            _contractResolver = DefaultContractResolver.Instance;
        }
Example #4
0
        private void SetPropertyValue(JsonProperty property, JsonReader reader, object target)
        {
            if (property.Ignored)
            {
                reader.Skip();
                return;
            }
            object obj  = null;
            bool   flag = false;
            bool   gottenCurrentValue             = false;
            ObjectCreationHandling valueOrDefault = property.ObjectCreationHandling.GetValueOrDefault(base.Serializer.ObjectCreationHandling);

            if ((valueOrDefault == ObjectCreationHandling.Auto || valueOrDefault == ObjectCreationHandling.Reuse) && (reader.TokenType == JsonToken.StartArray || reader.TokenType == JsonToken.StartObject) && property.Readable)
            {
                obj = property.ValueProvider.GetValue(target);
                gottenCurrentValue = true;
                flag = obj != null && !property.PropertyType.IsArray && !ReflectionUtils.InheritsGenericDefinition(property.PropertyType, typeof(ReadOnlyCollection <>)) && !property.PropertyType.IsValueType;
            }
            if (!property.Writable && !flag)
            {
                reader.Skip();
                return;
            }
            if (property.NullValueHandling.GetValueOrDefault(base.Serializer.NullValueHandling) == NullValueHandling.Ignore && reader.TokenType == JsonToken.Null)
            {
                reader.Skip();
                return;
            }
            if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(base.Serializer.DefaultValueHandling), DefaultValueHandling.Ignore) && JsonReader.IsPrimitiveToken(reader.TokenType) && MiscellaneousUtils.ValueEquals(reader.Value, property.DefaultValue))
            {
                reader.Skip();
                return;
            }
            object currentValue = (flag ? obj : null);
            object obj2         = CreateValueProperty(reader, property, target, gottenCurrentValue, currentValue);

            if ((!flag || obj2 != obj) && ShouldSetPropertyValue(property, obj2))
            {
                property.ValueProvider.SetValue(target, obj2);
                if (property.SetIsSpecified != null)
                {
                    property.SetIsSpecified(target, true);
                }
            }
        }
        private void SetPropertyValue(JsonProperty property, JsonReader reader, object target)
        {
            if (property.Ignored)
            {
                reader.Skip();
                return;
            }

            object currentValue       = null;
            bool   useExistingValue   = false;
            bool   gottenCurrentValue = false;

            ObjectCreationHandling objectCreationHandling =
                property.ObjectCreationHandling.GetValueOrDefault(Serializer.ObjectCreationHandling);

            if ((objectCreationHandling == ObjectCreationHandling.Auto || objectCreationHandling == ObjectCreationHandling.Reuse) &&
                (reader.TokenType == JsonToken.StartArray || reader.TokenType == JsonToken.StartObject) &&
                property.Readable)
            {
                currentValue       = property.ValueProvider.GetValue(target);
                gottenCurrentValue = true;

                useExistingValue = (currentValue != null && !property.PropertyType.IsArray && !ReflectionUtils.InheritsGenericDefinition(property.PropertyType, typeof(ReadOnlyCollection <>)));
            }

            if (!property.Writable && !useExistingValue)
            {
                reader.Skip();
                return;
            }

            object existingValue = (useExistingValue) ? currentValue : null;
            object value         = CreateValueProperty(reader, property, target, gottenCurrentValue, existingValue);

            // always set the value if useExistingValue is false,
            // otherwise also set it if CreateValue returns a new value compared to the currentValue
            // this could happen because of a JsonConverter against the type
            if ((!useExistingValue || value != currentValue) &&
                ShouldSetPropertyValue(property, value))
            {
                property.ValueProvider.SetValue(target, value);
            }
        }
Example #6
0
        private void SetPropertyValue(JsonProperty property, JsonReader reader, object target)
        {
            if (property.Ignored)
            {
                reader.Skip();
                return;
            }

            object currentValue       = null;
            bool   useExistingValue   = false;
            bool   gottenCurrentValue = false;

            ObjectCreationHandling objectCreationHandling =
                property.ObjectCreationHandling.GetValueOrDefault(Serializer.ObjectCreationHandling);

            if ((objectCreationHandling == ObjectCreationHandling.Auto || objectCreationHandling == ObjectCreationHandling.Reuse) &&
                (reader.TokenType == JsonToken.StartArray || reader.TokenType == JsonToken.StartObject) &&
                property.Readable)
            {
                currentValue       = property.ValueProvider.GetValue(target);
                gottenCurrentValue = true;

                useExistingValue = (currentValue != null &&
                                    !property.PropertyType.IsArray &&
                                    !ReflectionUtils.InheritsGenericDefinition(property.PropertyType, typeof(ReadOnlyCollection <>)) &&
                                    !property.PropertyType.IsValueType);
            }

            if (!property.Writable && !useExistingValue)
            {
                reader.Skip();
                return;
            }

            // test tokentype here because null might not be convertable to some types, e.g. ignoring null when applied to DateTime
            if (property.NullValueHandling.GetValueOrDefault(Serializer.NullValueHandling) == NullValueHandling.Ignore && reader.TokenType == JsonToken.Null)
            {
                reader.Skip();
                return;
            }

            // test tokentype here because default value might not be convertable to actual type, e.g. default of "" for DateTime
            if (property.DefaultValueHandling.GetValueOrDefault(Serializer.DefaultValueHandling) == DefaultValueHandling.Ignore &&
                JsonReader.IsPrimitiveToken(reader.TokenType) &&
                Equals(reader.Value, property.DefaultValue))
            {
                reader.Skip();
                return;
            }

            object existingValue = (useExistingValue) ? currentValue : null;
            object value         = CreateValueProperty(reader, property, target, gottenCurrentValue, existingValue);

            // always set the value if useExistingValue is false,
            // otherwise also set it if CreateValue returns a new value compared to the currentValue
            // this could happen because of a JsonConverter against the type
            if ((!useExistingValue || value != currentValue) &&
                ShouldSetPropertyValue(property, value))
            {
                property.ValueProvider.SetValue(target, value);

                if (property.SetIsSpecified != null)
                {
                    property.SetIsSpecified(target, true);
                }
            }
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 /// </summary>
 public JsonSerializer()
 {
   _referenceLoopHandling = ReferenceLoopHandling.Error;
   _missingMemberHandling = MissingMemberHandling.Error;
   _nullValueHandling = NullValueHandling.Include;
   _objectCreationHandling = ObjectCreationHandling.Auto;
 }