Re:Understanding Serde
by skocznymroczny on Monday November 18, 2019 @06:10AM
(#21563367)
D also has serialization libraries based on introspection capabilities. Orange is an example of such library https://code.dlang.org/packages/orange . Here's a blog series (in progress) showing how to write a serializer/deserializer from scratch in D: https://bradley.chatha.dev/Home/Blog?post=JsonSerialiser1
[ Parent
| on HN ]
Re:Understanding Serde
by JoshMcguigan on Monday November 18, 2019 @07:36AM
(#21563814)
Thanks for sharing. I'm not familiar with D, but it looks like compile time type introspection is something which is built into the language, which removes the need for something like the serde derive macro. Is that correct?
Re:Understanding Serde
by skocznymroczny on Monday November 18, 2019 @09:33AM
(#21564557)
Yes. D metaprogramming is mostly based around template and it gives you compile-time traits which you can use to query structs/classes for their fields, determining if something is an array or a pointer etc.
This way, you can write a serialize(foo) function which will work for any type, without needing any derives or marking the type as Serializable like in Java.
The closest equivalent to derive in D would be a mixin. You could declare a mixin which autogenerates the toJSON() fromJSON() methods based on the fields of a struct.