www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Deserializing JSON as an abstract type

reply Chris <cawatson1993 gmail.com> writes:
So I'm trying to make a D wrapper for Telegram's JSON API using 
libtdjson. All results coming from the JSON API take the 
following structure:

{
   " type": "className",
   "foo": "bar",
   "baz" {
     " type": "otherClass"
   }
}

where every object, including nested ones, has a " type" field 
which will correspond to a D class. There will be over 770 
possible types. So what I'm trying to figure out is how to 
deserialize the JSON so that it automatically gets parsed into 
the correct type.

I'm looking at asdf right now, but it doesn't seem like it has 
anything like that built in. Any ideas?
Nov 18 2019
parent Heromyth <bitworld qq.com> writes:
On Tuesday, 19 November 2019 at 00:40:16 UTC, Chris wrote:
 So I'm trying to make a D wrapper for Telegram's JSON API using 
 libtdjson. All results coming from the JSON API take the 
 following structure:

 {
   " type": "className",
   "foo": "bar",
   "baz" {
     " type": "otherClass"
   }
 }

 where every object, including nested ones, has a " type" field 
 which will correspond to a D class. There will be over 770 
 possible types. So what I'm trying to figure out is how to 
 deserialize the JSON so that it automatically gets parsed into 
 the correct type.

 I'm looking at asdf right now, but it doesn't seem like it has 
 anything like that built in. Any ideas?
Can you have a look at this : https://github.com/huntlabs/hunt/blob/master/examples/UnitTest/source/test/JsonSerializerTest.d The JsonSerializer can serialize and deserialize a complex class.
Nov 18 2019