tags:

views:

516

answers:

2

I'm trying to convert JSON to C# object using Json.NET. The object looks like this in C#:

public class MyObject 
{
   public int? integerValue {get;set;}
   public DateTime? dateTimeValue {get;set;}
}

But when I run JsonConvert.DeserializeObject() on the incoming JSON, I get the following Exception:

Unable to find a constructor to use for type System.Nullable`1[System.Int32]. A class should either have a default constructor or only one constructor with arguments.

--- EDIT ----

Well it turns out that after doing many tests, the problem boils down to that my input for my JSON was like this:

{integerValue:{}, dateTimeValue: {} }

instead of:

{integerValue: null, dateTimeValue: null}

It turns out that the {} is a valid way of representing a null object in JSON but the JSON.Net parser did not know to treat {} tokens the same way as 'null' when de-serializing.

Thanks everyone for your input!

+2  A: 

I dont know is it right answer or not, but at least You can create custom converter for Nullable<>, it helps me a lot with DataRow serializing/deserializing it also does not have default constructor. Here is sample

Sergey Mirvoda
Sergey. Thanks for helping. But it turns out the problem was something different. See my edits in my posted question.
Roberto Sebestyen
+3  A: 

Hi Roberto,

The error is telling you that it cant find a a constructor that it can use for the deserialization.

Try adding a default constructor to the class:

public class MyObject
    {
        public int? integerValue { get; set; }
        public DateTime? dateTimeValue { get; set; }

        public MyObject(){}
    } 

Patrick.

--EDIT--

So I've just created a simple console app using your MyObject, with and without a default constructor and I'm getting no errors. Here is my example:

   class Program
    {
        static void Main(string[] args)
        {
            var mo = new MyObject { integerValue = null, dateTimeValue = null };
            var ser = Newtonsoft.Json.JsonConvert.SerializeObject(mo);
            var deser = Newtonsoft.Json.JsonConvert.DeserializeObject(ser, typeof(MyObject));
        }
    }

    public class MyObject
    {
        public int? integerValue { get; set; }
        public DateTime? dateTimeValue { get; set; }        
    }  

I get no exceptions...

Can you show an example of the JSON that you are trying to deserialize?

Patrick
Sorry Sergey, I see you already commented on the default constructor...
Patrick
Patrick, I do not think it is as simple as that. The error is not that there is no default constructor for MyObject. The error is saying that there is no default constructor for type System.Nullable ! Every class that is user defined gets an implicit default parameter-less constructor by the compiler. Unless the constructor is marked private.
Roberto Sebestyen
You're right, the parameter-less constructor is added by the compiler - But I've had many instances in that past when developing WCF REST services (XML and JSON) where I would get serialization isses if I didn't specifically add a parameterless constructor. So in theory, what you say should work but in practice, I've had a different experience.I'll try to set up a project using the same tools you have to see if I can come up with a better option.
Patrick
Thanks for your efforts. I just found out what the problem was. Turns out my JSON input sring had {} instead of null for the null values. Although {} is still valid in JSON, the JSON.Net converter did not understand to handle {} the same way as null.
Roberto Sebestyen
I'll accept your answer as accepted since it is the one that triggered me to find the solution.
Roberto Sebestyen