1

Closed

No constructor should be called during deserialization of a data contract type

description

Json.NET is unable to deserialize a data contract type with no public constructor. Note that DataContractJsonSerializer doesn't call any constructor when deserializing a data contract type:
"During deserialization, an uninitialized object is first created, without calling any constructors on the type. Then all data members are deserialized."
http://msdn.microsoft.com/en-us/library/ms733127.aspx
 
Repro:
[DataContract]
public class Person
{
    private Person()
    { }
 
    public static Person CreatePerson()
    {
        return new Person();
    }
 
    [DataMember]
    public string Name { get; set; }
 
    [DataMember]
    public int Age { get; set; }
}
 
public class Program
{
    static void Main(string[] args)
    {
        var person = Person.CreatePerson();
        person.Name = "John Doe";
        person.Age = 25;
 
        var serializedPerson = JsonConvert.SerializeObject(person);
        var roundtrippedPerson = JsonConvert.DeserializeObject<Person>(serializedPerson);
    }
}
 
Exception:
 
Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Repro.Person. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Line 1, position 8.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateObject(JsonReader reader, JsonObjectContract contract, String id) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 957
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 433
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 23
6
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract, JsonConverter converter) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 221
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 115
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonSerializer.cs:line 420
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonConvert.cs:line 730
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonConvert.cs:line 694
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonConvert.cs:line 651
at Repro.Program.Main(String[] args) in Z:\repro\Repro\Repro.cs:line 45
Press any key to continue . . .
Closed Mar 10, 2012 at 2:30 AM by JamesNK
Json.NET always calls a constructor. I have fixed it so that Json.NET will use a single constructor if it is private.

comments