I have a class that has a default constructor, as well as an overloaded constructor that takes a set of parameters. These parameters correspond to the fields of the object and are assigned during construction. At the moment, I need a default constructor for other purposes, so I would like to save it if I can.
My problem: if I delete the default constructor and pass the JSON string, the object is deserialized correctly and passes the constructor parameters without any problems. In the end, I return to the object filled as I expected. However, as soon as I add a default constructor to the object, when I call JsonConvert.DeserializeObject<Result>(jsontext) , the properties no longer populate.
At this point, I tried to add new JsonSerializerSettings(){CheckAdditionalContent = true} to the deserialization call. that did nothing.
One more note. constructor parameters exactly match field names, except that parameters begin with a lowercase letter. I would not think that it would matter, because, as I mentioned, deserialization works fine without a default constructor.
Here is an example of my constructors:
public Result() { } public Result(int? code, string format, Dictionary<string, string> details = null) { Code = code ?? ERROR_CODE; Format = format; if (details == null) Details = new Dictionary<string, string>(); else Details = details; }
kmacdonald Apr 11 '14 at 16:16 2014-04-11 16:16
source share