rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
230 stars 29 forks source link

Deserializing issue with legacy JSON array dictionary #180

Closed jigneshkvp closed 6 years ago

jigneshkvp commented 6 years ago

Hello,

I am unable to deserialize the following json string to the mentioned class:

{
   "OrgDict":[{
                 "Key": 10000
                 "Value":{
                         "children":[10001,10002],
                         "id": 1,
                          "active":false
                           "name": "abc"
                   }
}, {
                 "Key": 20000
                 "Value":{
                         "children":[20001,20002],
                         "id": 2,
                          "active":true
                           "name": "xyz"
                   }
 }]
}

The class looks like this:

[DataContract]
public class Heirarchy
{
    [DataMember]
    public Dictionary<ulong, Org> OrgDict{ get; set; }
}
[DataContract]
public class Org
{
    [DataMember]
    public ulong id {get; set;}

    [DataMember]
    public ulong[] children {get; set;}

    [DataMember]
    public string name {get; set;}

    [DataMember]
    public bool active {get; set;}
}

When I perform, var response = NetJSON.NetJSON.Deserialize(jsonString);

I get the output as a heirarchy object with properties "dict": somenumber, "Value" : null

I was earlier using Newtonsoft.Json and there it has a JConverter class which I extended to deserialize the legacy json array dictionary (which is the output of a wcf service method using DataContractJsonSerializer class).

I am in aww with NetJSON performance (respect!) and I want to use it for deseriliazing the legacy array dicitionary string.

Could you please help in resolving this issue.

rpgmaker commented 6 years ago

Sure, let me look into it and get back to you.

Thanks,

rpgmaker commented 6 years ago

I do have a custom serialization option for primitive types. The best I can do is extend to support any type then you can implement your custom logic.

rpgmaker commented 6 years ago

After looking much at it. It will be difficult to support custom deserialization since I read forward only and I don't read a full graph before deserialization. The only way to have it is if you are ok with having to implement a class that we get a stream of data such as {, [, Key and Value then you can handle the rest of it yourself in your custom code.

Is the option above viable? If it is then I can expose a structure to hook into the deserialization step .

jigneshkvp commented 6 years ago

Thanks for your input!

I can certainly look into that option. However, it would be also great if you could send me a small example around it.

Even, if I could do something like this, then it would be great:

JObject o = JObject.Parse(@"{
   'CPU': 'Intel',
   'Drives': [
     'DVD read/writer',
     '500 gigabyte hard drive'
   ]
 }");

 string cpu = (string)o["CPU"];
// Intel
string firstDrive = (string)o["Drives"][0];
// DVD read/writer
IList<string> allDrives = o["Drives"].Select(t => (string)t).ToList();

Probably in the future, we may have support for custom deserialization.

rpgmaker commented 6 years ago

You can already deserialize to dictionary (string, object) and that will give you same result. Just call the deserializeObject method.

Thanks,

rpgmaker commented 6 years ago

Have you tried to use the dictionary option I suggested?

jigneshkvp commented 6 years ago

Hello @rpgmaker, I was unable to work with it due to the other obligations. I kept my old implementation for now but will work with your suggestion somewhere next week

rpgmaker commented 6 years ago

I am going to close this issue for now assuming my suggestion works. If not you can reopen it.

Thanks,