Search code examples
c#jsonjson.netdeserializationdatacontractserializer

Why did json2sharp generate an extra class from my JSON?


I used json2csharp to generate some classes from my JSON. Everything is working when I deserialize with DataContractJsonSerializer or JsonConvert, but I cannot understand why the generator makes the Message2 class. It does not fit in with my logic, and I want to clearly understand this. I think that Message2 should be used inside of OfflineMessage like List<Message2> messages, and the Message1 class needs to be deleted. What am I missing?

These are the classes generated by json2csharp (note that I renamed Message to Message1 for a clearer question):

public class Message2
{
    public int status { get; set; }
    public string sender { get; set; }
    public string receiver { get; set; }
    public string msg { get; set; }
    public int timeStamp { get; set; }
    public string hash { get; set; }
    public string msgContext { get; set; }
}

public class Message1
{
    public Message2 message { get; set; }
}

public class OfflineMessage
{
    public int sequence { get; set; }
    public List<Message1> messages { get; set; }
}

public class Respons
{
    public OfflineMessage offlineMessage { get; set; }
}

public class RootObject
{
    public int __invalid_name__@pendingMsg { get; set; }
    public int __invalid_name__@syncStatus { get; set; }
    public List<Respons> responses { get; set; }
}

This is how the JSON looks:

{
  "@pendingMsg": 0,
  "@syncStatus": 0,
  "responses": [
    {
      "offlineMessage": {
        "sequence": 2,
        "messages": [
          {
            "message": {
              "status": 6,
              "sender": "storng",
              "receiver": "JoilSp",
              "msg": "Hello my friend",
              "timeStamp": 1465398075,
              "hash": "+eq4cxwICbyNC31X5naQz3Z+R/Lxdw==",
              "msgContext": "+eq4cxwICbyNC31X5naQz3Z+R/Lxdw=="
            }
          },
          {
            "message": {
              "status": 6,
              "sender": "JioS",
              "receiver": "JoilSp",
              "msg": "nice)))",
              "timeStamp": 1465398075,
              "hash": "+eq4cxwICbyNC31X5naQz3Z+R/Lxdw==",
              "msgContext": "+eq4cxwICbyNC31X5naQz3Z+R/Lxdw=="
            }
          }
        ]
      }
    }
  ]
}

Solution

  • It's creating a Message2 class because your messages property is an array of objects, and those objects contain other objects in their message property. It needs one class to represent the members of the messages array, and another to represent the values of the message properties.

    If you are in charge of your JSON format, it may be cleaner to structure your JSON like this:

    {
       "@pendingMsg":0,
       "@syncStatus":0,
       "responses":[
          {
             "offlineMessage":{
                "sequence":2,
                "messages":[
                   {
                      "status":6,
                      "sender":"storng",
                      "receiver":"JoilSp",
                      "msg":"Hello my friend",
                      "timeStamp":1465398075,
                      "hash":"+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw==",
                      "msgContext":"+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw=="
                   },
                   {
                      "status":6,
                      "sender":"JioS",
                      "receiver":"JoilSp",
                      "msg":"nice)))",
                      "timeStamp":1465398075,
                      "hash":"+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw==",
                      "msgContext":"+eq4cxwICbyNC31X5naQz3Z+R\/Lxdw=="
                   }
                ]
             }
          }
       ]
    }
    

    This should produce the kind of class structure that you are expecting.