Deserializing when value properties dont exist in the json

Jun 8, 2011 at 5:35 PM
Edited Jun 8, 2011 at 5:42 PM

If i try and deserialize this JSON:

{

}

onto:

class MyClass{

    public ValueType Val { get; set; }

}

I get:

JsonSerializationException: Error setting value to 'Val'

NullReferenceException: Object reference not set to an instance of an object.
   at SetVal(Object , Object )   at Newtonsoft.Json.Serialization.DynamicValueProvider.SetValue(Object target, Object value) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\DynamicValueProvider.cs:line 82

 

Is there a way to make this not fail, and just leave the property with its default value?

Update: im aware of ObjectCreationHandling, I assume setting this to Replace would help? But what does that do for the rest of my settings:

 

JsonSerializerSettings{

TypeNameHandling = TypeNameHandling.Objects,

ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,

PreserveReferencesHandling = PreserveReferencesHandling.Objects

}

Thanks!