3 ms·
D also has serialization libraries based on introspection capabilities. Orange is an example of such library https://code.dlang.org/packages/orange https://code
by skocznymroczny 7y ago
D also has serialization libraries based on introspection capabilities. Orange is an example of such library https://code.dlang.org/packages/orange 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 https://bradley.chatha.dev/Home/Blog?post=JsonSerialiser1
- JoshMcguigan 7y agoThanks 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?
- skocznymroczny 7y agoYes. 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.