Usage of custom class with JSONValue
arturg via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Mar 24 04:39:13 PDT 2016
On Thursday, 24 March 2016 at 08:24:46 UTC, Edwin van Leeuwen
wrote:
> JSONValue only works with the build in types, not with user
> defined types. Either you define a specific function for the
> class that returns a JSONValue. Easiest way to do that would be
> to build an associative array with strings as keys with the
> names, and JSONValues as values and turn that into JSONValue,
> i.e. (untested):
> class MyClass
> {
> string[] _data;
> alias _data this;
> // ...
> JSONValue toJSON()
> {
> JSONValue[string] aa;
> JSONValue[] dataJSON = _data.map!((a) =>
> JSONValue(a)).array;
> aa["data"] = JSONValue(dataJSON);
> return JSONValue(aa);
> }
> }
>
isnt alias this supposed to do this implicitly?
convert this
auto jsValue = JSONValue(new MyClass());
into this
auto jsValue = JSONValue((new MyClass())._data);
More information about the Digitalmars-d-learn
mailing list