1

Does anyone have any experience with the System.Runtime.Serialization.Json and DataContracts? I have a Perl script that I'm converting to C# and I cannot seem to figure out how to craft this DataContract to account for the fact that the response from the website is a bunch of objects. I've tried to pretend that the objects are not there and I've tried to define a contract specifying the first object string that comes out but I cannot get this to parse ANY individual bits of data. Here is a sample of the 2 of the 200 some objects that get returned as a response from the request.

CURRENT DATA CONTRACT ATTEMPT:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(JsonMachines));
JsonMachines machines = (JsonMachines)ser.ReadObject(resp.GetResponseStream());

[DataContract]
class JsonMachines
{
    [DataMember]
    public JsonMachine dc54806f4fe34cf5a83b0676555f8658;

}
[DataContract]
class JsonMachine
{
    [DataMember]
    public string guid;
    [DataMember]
    public int lost_contact;
    [DataMember]
    public string org;
    [DataMember]
    public string timezone;
}

JSON RESPONSE FROM SERVER:

{
   "dc54806f4fe34cf5a83b0676555f8658" : {
      "shadowprotect" : {
         "jobs" : [],
         "version" : {
            "is_expired" : false,
            "is_running" : true,
            "lang" : "en",
            "is_installed" : true,
            "version" : "6.0.8",
            "is_trial" : false,
            "name" : "ShadowProtect SPX",
            "is_msp" : false,
            "company" : ""
         }
      },
      "lost_contact" : 0,
      "org" : "site : name",
      "timezone" : -18000,
      "status" : "ok",
      "name" : "MACHINENAME2",
      "tags" : [],
      "machine_details" : {
         "last_boot" : "2016-01-21T10:54:11.557000",
         "volumes" : [
            {
               "mountpoint" : "C:\\",
               "device" : "\\\\?\\Volume{07897f98-6618-11e5-8055-806e6f6e6963}\\",
               "size" : 305242,
               "boot" : false,
               "readonly" : false,
               "removable" : false,
               "os_vol" : true,
               "used" : 57787,
               "label" : ""
            },
            {
               "mountpoint" : null,
               "device" : "\\\\?\\Volume{07897f99-6618-11e5-8055-806e6f6e6963}\\",
               "size" : 99,
               "boot" : false,
               "readonly" : false,
               "removable" : false,
               "os_vol" : false,
               "used" : 28,
               "label" : "System Reserved"
            }
         ],
         "ram" : 3945
      },
      "imagemanager" : {
         "folders" : []
      }
   },
   "c947116fc62c40c2932e850944f78550" : {
      "shadowprotect" : {
         "jobs" : [],
         "version" : {
            "is_expired" : true,
            "is_running" : true,
            "days_to_expire" : 0,
            "lang" : "en",
            "is_installed" : true,
            "version" : "4.2.7.19756",
            "name" : "ShadowProtect",
            "is_msp" : true,
            "company" : null
         }
      },
      "lost_contact" : 0,
      "org" : "site : name2",
      "timezone" : -18000,
      "status" : "ok",
      "name" : "MACHINENAME",
      "tags" : [],
      "machine_details" : {
         "last_boot" : "2016-01-28T08:34:54.486000",
         "volumes" : [
            {
               "mountpoint" : "C:\\",
               "device" : "\\\\?\\Volume{bcbc546f-291f-11e2-9a3f-806e6f6e6963}\\",
               "size" : 476153,
               "boot" : false,
               "readonly" : false,
               "removable" : false,
               "os_vol" : true,
               "used" : 145434,
               "label" : "OS"
            },
            {
               "mountpoint" : null,
               "device" : "\\\\?\\Volume{bcbc546e-291f-11e2-9a3f-806e6f6e6963}\\",
               "size" : 745,
               "boot" : false,
               "readonly" : false,
               "removable" : false,
               "os_vol" : false,
               "used" : 224,
               "label" : "RECOVERY"
            }
         ],
         "ram" : 4052
      },
      "imagemanager" : {
         "folders" : []
      }
   }
}

2 Answers 2

0

It could make the difference that you have JsonMachine.timezone defined to be a string, but in your JSON it is coming over as an integer.

Also, keep in mind that the DataContractSerializer is order dependent. From what I can see, it should pick up "org" and "timezone". Since you aren't using [DataMember(Order = 1)] it is looking in alphabetical order.

There is no "guid" property or "lost_contact" property on the dc54806f4fe34cf5a83b0676555f8658 object, so "org" is the first thing it looks for. Once it finds it, it never looks at the previous properties in future lookups. Supposedly it's a performance boost.

You can use the above attribute to set a specific expectation of order yourself. But the order will have to be consistent, because if the objects come over jumbled, you'll have problems with the DataContractSerializer and may have to move to Newtonsoft.Json which is a nice library.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the insight on this jeromeyers, this is something I didn't even know I had to look at. Coming from Perl it put everything into a hash that I could just query bits of at will. I took the bits of code that Michael put up but it doesn't solve the first issue i'm running into of not being able to read the object. You will notice that he labled the class as the name of the first object but since that changes between EVERY object, I don't know how to account for it.
0

possible dupe...

How to deserialize a dynamically named root node with json.NET

2 Comments

So there isn't any error. The JsonMachines machines object just always comes back null. I see that you defined a public class of Dc54806f4fe34cf5a83b0676555f8658 which is just one of the strings, will that work for the next one in the response c947116fc62c40c2932e850944f78550 or do I have to redefine the public class 200 times once for each string(object), and then if a new one gets added to the list it won't serialize because I don't have a class for it? Also, i modified the code to test this and the serialization object (machines) is still coming back as null.
ok now i see the issue. I have not crossed this before, its a very odd response that the object gets a different name, but yet its the same in terms of children. I would say you want to ingore the RootName found this , i hope its helpful stackoverflow.com/questions/23648193/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.