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

dart.class.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
class {{classname}} {
  {{#vars}}
  {{#description}}/* {{{description}}} */{{/description}}
  {{^defaultValue}}{{{dataType}}} {{name}} = null;{{/defaultValue}}{{#defaultValue}}{{{dataType}}} {{name}} = {{defaultValue}};{{/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}}
      {{#isArray}}
      {{name}} = {{complexType}}.listFromJson(json['{{baseName}}']);
      {{/isArray}}
      {{^isArray}}
      {{#isMap}}
      {{name}} = {{complexType}}.mapFromJson(json['{{baseName}}']);
      {{/isMap}}
      {{^isMap}}
      {{name}} = new {{complexType}}.fromJson(json['{{baseName}}']);
      {{/isMap}}
      {{/isArray}}
    {{/complexType}}
    {{^complexType}}
      {{#isArray}}
      {{name}} = (json['{{baseName}}'] as List).map((item) => item as {{items.datatype}}).toList();
      {{/isArray}}
      {{^isArray}}
      {{name}} = json['{{baseName}}'];
      {{/isArray}}
    {{/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}}
        {{#complexType}}
          {{#isArray}}
            '{{baseName}}': {{name}} == null ? null : {{name}}.map((item) => item.toJson()).toList(){{^-last}},{{/-last}}
          {{/isArray}}
          {{^isArray}}
            '{{baseName}}': {{name}} == null ? null : {{name}}.toJson(){{^-last}},{{/-last}}
          {{/isArray}}
        {{/complexType}}
        {{^complexType}}
          '{{baseName}}': {{name}}{{^-last}},{{/-last}}
        {{/complexType}}
      {{/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