abstract class Language { String language; List targets; Map website; }
abstract class Language { String language; List targets; Website website; }
class LanguageImpl extends JsonObject implements Language { LanguageImpl(); /* I'm not sure if final is what I really want here */ final Website _website; /* If someone external asks for the website, give him the internal one */ Website get website => _website; /* When JsonObject.fromJsonString tries to set the website it will * examine the JSON and see it as a map. This setter picks that * up and creates a version that conforms to the Website interface */ Website set website(Map map) => _website = new WebsiteImpl.fromMap(map); factory LanguageImpl.fromJsonString(string) { return new JsonObject.fromJsonString(string, new LanguageImpl()); } }
abstract class Website {
String name;
String url;
}class WebsiteImpl extends JsonObject implements Website { WebsiteImpl(); factory WebsiteImpl.fromMap(map) { return new JsonObject.fromMap(map); } }
* The optional [revivier] function, if provided, is called once for each
* object or list property parsed. The arguments are the property name
* ([String]) or list index ([int]), and the value is the parsed value.
* The return value of the revivier will be used as the value of that property
* instead the parsed value.
external parse(String json, [reviver(var key, var value)]);
>>>
I haven't tried it, but sounds like it could help you to convert your Map to a Website during the translation without having to hack getters and setters.
Unfortunately json_object doesn't allow you to specify any reviver:
<<<
factory JsonObject.fromJsonString(String _jsonString, [JsonObject t]) {
...
t._objectData = JSON.parse(t._jsonString);
...
}
>>>
I don't know if json_object could be changed (if it makes sense) in some way to take advantage of all this.
Just another thing more to investigate ;-). If you do it I'll be interested in knowing what you finally got. I'm also working with JSON objects for REST interfaces but I haven't yet arrived to the implementation of any 1-N or N-N relations, I'm just working with plain objects with only "primitive" properties as of now.
@Alan: any data on performance of serializer?E.g. take some JSON, deserialize it into object, serialize it back (so output=input), compute output in MB/sec.
@Alan: any data on performance of serializer?E.g. take some JSON, deserialize it into object, serialize it back (so output=input), compute output in MB/sec.