Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for externally defined types #28

Open
dhasenan opened this issue Aug 1, 2016 · 1 comment
Open

Support for externally defined types #28

dhasenan opened this issue Aug 1, 2016 · 1 comment

Comments

@dhasenan
Copy link

dhasenan commented Aug 1, 2016

I want to use a std.typecons.Typedef in my code. Or Quantity from experimental.units. That kind of thing.

These are structs defined outside my own code. How do I serialize them to JSON? Well, if I'm only using them in a couple places, I can provide serialization properties, eg:

Quantity!Meter length;
@jsonize {
  double lengthInMeters() { return length.value; }
  void lengthInMeters(double value) { length.value = value; }
}

If I use it everywhere, I can write a wrapper struct:

struct Quant(alias unitValue) {
  typeof(unitValue) val;
  alias val this;
  @jsonize {
    double value() { return val.toValue; }
    void value(double newVal) { val = unitValue * newVal; }
  }
}

// now replace Quantity!Metre with Quant!metre etc

It would be nicer to inject into jsonizer a means of deserializing Quantity and other externally defined types. (Adding a convertToJSON method accessible at the JsonizeMe mixin site doesn't work.)

@rcorre
Copy link
Owner

rcorre commented Aug 7, 2016

I was looking at the docs for ASDF and they use a proxy type and mark an
attribute with serializedAs.

I'm thinking we could do something like this, but declaring a new type is
overkill when you really just need 2 functions:

JSONValue quantToJson(Quantity!T q) { return JSONValue(q.value); }
Quantity!T jsonToQuant(T)(JSONValue j) { return Quantity!T(j.floating); }

@jsonTranslate(quantToJson, jsonToQuant)
Quantity!Meter length;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants