dart2.class.mustache Maven / Gradle / Ivy
class {{classname}} {
{{#vars}}
{{#description}}/* {{{description}}} */{{/description}}
{{{dataType}}} {{name}} = {{{defaultValue}}};
{{#allowableValues}}
{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{
{{/allowableValues}}
{{/vars}}
{{classname}}();
@override
String toString() {
return '{{classname}}[{{#vars}}{{name}}=${{name}}, {{/vars}}]';
}
{{classname}}.fromJson(Map json) {
if (json == null) return;
{{#vars}}
{{#isDateTime}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
DateTime.parse(json['{{baseName}}']);
{{/isDateTime}}
{{#isDate}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
DateTime.parse(json['{{baseName}}']);
{{/isDate}}
{{^isDateTime}}
{{^isDate}}
{{#complexType}}
{{#isListContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
{{complexType}}.listFromJson(json['{{baseName}}']);
{{/isListContainer}}
{{^isListContainer}}
{{#isMapContainer}}
{{#items.isListContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
{{items.complexType}}.mapListFromJson(json['{{baseName}}']);
{{/items.isListContainer}}
{{^items.isListContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
{{complexType}}.mapFromJson(json['{{baseName}}']);
{{/items.isListContainer}}
{{/isMapContainer}}
{{^isMapContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
{{complexType}}.fromJson(json['{{baseName}}']);
{{/isMapContainer}}
{{/isListContainer}}
{{/complexType}}
{{^complexType}}
{{#isListContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
(json['{{baseName}}'] as List).cast<{{items.datatype}}>();
{{/isListContainer}}
{{^isListContainer}}
{{#isMapContainer}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
(json['{{baseName}}'] as Map).cast();
{{/isMapContainer}}
{{^isMapContainer}}
{{#isDouble}}
{{name}} = (json['{{baseName}}'] == null) ?
null :
json['{{baseName}}'].toDouble();
{{/isDouble}}
{{^isDouble}}
{{name}} = json['{{baseName}}'];
{{/isDouble}}
{{/isMapContainer}}
{{/isListContainer}}
{{/complexType}}
{{/isDate}}
{{/isDateTime}}
{{/vars}}
}
Map toJson() {
Map json = {};
{{#vars}}
{{^isNullable}}
if ({{name}} != null)
{{/isNullable}}
{{#isDateTime}}
json['{{baseName}}'] = {{name}} == null ? null : {{name}}.toUtc().toIso8601String();
{{/isDateTime}}
{{#isDate}}
json['{{baseName}}'] = {{name}} == null ? null : {{name}}.toUtc().toIso8601String();
{{/isDate}}
{{^isDateTime}}
{{^isDate}}
json['{{baseName}}'] = {{name}};
{{/isDate}}
{{/isDateTime}}
{{/vars}}
return json;
}
static List<{{classname}}> listFromJson(List json) {
return json == null ? List<{{classname}}>() : json.map((value) => {{classname}}.fromJson(value)).toList();
}
static Map mapFromJson(Map json) {
var map = Map();
if (json != null && json.isNotEmpty) {
json.forEach((String key, dynamic value) => map[key] = {{classname}}.fromJson(value));
}
return map;
}
// maps a json object with a list of {{classname}}-objects as value to a dart map
static Map> mapListFromJson(Map json) {
var map = Map>();
if (json != null && json.isNotEmpty) {
json.forEach((String key, dynamic value) {
map[key] = {{classname}}.listFromJson(value);
});
}
return map;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy