How to parse a json node that contains children of different types?

Bagomot bagomot at gmail.com
Wed Jul 21 06:30:45 UTC 2021


On Wednesday, 21 July 2021 at 03:00:51 UTC, Mathias LANG wrote:
> TL;DR: If you want to use loosely typed data in a strongly 
> typed language, you need to come up with a common type. That 
> common type is usually either a discriminated union (which a 
> JSON object is, essentially) or something that is 
> domain-specific.
>
> Hope this helps. I had a quick look at asdf and couldn't see a 
> support for tagged union, so you would probably need to provide 
> your data structure with a `deserializeFromAsdf` method. If you 
> want to do so, look into providing a wrapper to `SumType`, e.g.:
>
> ```
> struct MyUnion (T...) {
>     SumType!T data;
>     alias data this;
>     SerdeException deserializeFromAsdf(Asdf data) { /* Fill in 
> the SumType */ }
> ```
>
> But I would recommend just using the JSON object if you can.

Thank you! From your answer, I realized that in fact, I would 
rather do the parsing of these arguments myself.

Before using asdf, I tried to do the same with the standard 
std.json. The problem is the same there. I agree that this is 
logical for D.

I was also interested in SumType, I will try to do something with 
it for experience.


More information about the Digitalmars-d-learn mailing list