All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dart.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}}
    if (json['{{baseName}}'] == null) {
      {{name}} = null;
    } else {
  {{#isDateTime}}
      {{name}} = DateTime.parse(json['{{baseName}}']);
  {{/isDateTime}}
  {{#isDate}}
      {{name}} = DateTime.parse(json['{{baseName}}']);
  {{/isDate}}
  {{^isDateTime}}
  {{^isDate}}
    {{#complexType}}
      {{#isListContainer}}
      {{name}} = {{complexType}}.listFromJson(json['{{baseName}}']);
      {{/isListContainer}}
      {{^isListContainer}}
      {{#isMapContainer}}
      {{name}} = {{complexType}}.mapFromJson(json['{{baseName}}']);
      {{/isMapContainer}}
      {{^isMapContainer}}
      {{name}} = new {{complexType}}.fromJson(json['{{baseName}}']);
      {{/isMapContainer}}
      {{/isListContainer}}
    {{/complexType}}
    {{^complexType}}
      {{#isListContainer}}
      {{name}} = (json['{{baseName}}'] as List).map((item) => item as {{items.datatype}}).toList();
      {{/isListContainer}}
      {{^isListContainer}}
      {{name}} = json['{{baseName}}'];
      {{/isListContainer}}
    {{/complexType}}
  {{/isDate}}
  {{/isDateTime}}
    }
  {{/vars}}
  }

  Map toJson() {
    return {
    {{#vars}}
      {{#isDateTime}}
      '{{baseName}}': {{name}} == null ? '' : {{name}}.toUtc().toIso8601String(){{^-last}},{{/-last}}
      {{/isDateTime}}
      {{#isDate}}
      '{{baseName}}': {{name}} == null ? '' : {{name}}.toUtc().toIso8601String(){{^-last}},{{/-last}}
      {{/isDate}}
      {{^isDateTime}}
      {{^isDate}}
      '{{baseName}}': {{name}}{{^-last}},{{/-last}}
      {{/isDate}}
      {{/isDateTime}}
    {{/vars}}
    };
  }

  static List<{{classname}}> listFromJson(List json) {
    return json == null ? new List<{{classname}}>() : json.map((value) => new {{classname}}.fromJson(value)).toList();
  }

  static Map mapFromJson(Map> json) {
    var map = new Map();
    if (json != null && json.length > 0) {
      json.forEach((String key, Map value) => map[key] = new {{classname}}.fromJson(value));
    }
    return map;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy