Code review: JSON unmarshaller
Dan
dbdavidson at yahoo.com
Thu Oct 18 04:47:24 PDT 2012
On Monday, 15 October 2012 at 20:35:34 UTC, Tyler Jameson Little
wrote:
> I'm basically trying to reproduce other JSON marshallers, like
> Go's, but using compile-time reflection. Go uses runtime
> reflection, which D notably does not support. I like the idea
> of compile-time reflection better anyway. There are a few
> things that would make it easier (like a __traits call like
> allMembers that excludes functions).
>
> I use a lot of JSON, so a JSON marshaller/unmarshaller is going
> to save a lot of time, and make my code a lot cleaner.
>
I like Go's JSON convenience as well. There is a nice feature
where you can add attributes to the members that are then
available at runtime and therefore used by the serializer. So you
could have:
------
type AcquiredRetired struct {
Acquired tvm.Date `bson:"a"`
Retired tvm.Date `bson:"r"`
}
------
Here it specifies a shortened key for bson, but you can do the
same for json. The size benefit can be significant. A design
choice they made is to only serialize members that are
capitalized which means visible.
There is a nice json serialize/deserialize library in vibed.
When I throw this struct at your marshalJSON I get compile errors.
----------
import std.stdio;
struct X {
class D {
string b = "B";
}
string a = "A";
D d;
}
void main() {
auto c = new X();
auto o = marshalJSON(c);
writeln(o);
}
----------
Thanks
Dan
More information about the Digitalmars-d-learn
mailing list