javiercbk / json_to_dart

Library that generates dart classes from json strings
https://javiercbk.github.io/json_to_dart/
The Unlicense
1.33k stars 373 forks source link

Case conversion failed #35

Open demonskp opened 5 years ago

demonskp commented 5 years ago

The first letter of the word that should have been all capitalized has become a lowercase letter. Is this because of a grammatical limitation or an unresolved bug?

Please pay attention to the class Result and the Attributes NAME。

Thanks。

My Json:

{
    "flag": "T",
    "msg": "success",
    "err": "",
    "result": [
        {
            "NAME": "Angular"
        },
        {
            "NAME": "React"
        },
        {
            "NAME": "Vue"
        },
        {
            "NAME": "Webpack"
        }
    ]
}

Expected

generate:

class Autogenerated {
  String _flag;
  String _msg;
  String _err;
  List<Result> _result;

  Autogenerated({String flag, String msg, String err, List<Result> result}) {
    this._flag = flag;
    this._msg = msg;
    this._err = err;
    this._result = result;
  }

  String get flag => _flag;
  set flag(String flag) => _flag = flag;
  String get msg => _msg;
  set msg(String msg) => _msg = msg;
  String get err => _err;
  set err(String err) => _err = err;
  List<Result> get result => _result;
  set result(List<Result> result) => _result = result;

  Autogenerated.fromJson(Map<String, dynamic> json) {
    _flag = json['flag'];
    _msg = json['msg'];
    _err = json['err'];
    if (json['result'] != null) {
      _result = new List<Result>();
      json['result'].forEach((v) {
        _result.add(new Result.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['flag'] = this._flag;
    data['msg'] = this._msg;
    data['err'] = this._err;
    if (this._result != null) {
      data['result'] = this._result.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Result {
  String _NAME;

  Result({String NAME}) {
    this._NAME = NAME;
  }

  String get NAME => _NAME;
  set NAME(String NAME) => _NAME = NAME;

  Result.fromJson(Map<String, dynamic> json) {
    _NAME = json['NAME'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['NAME'] = this._NAME;
    return data;
  }
}

NOW

generate:

class Autogenerated {
  String _flag;
  String _msg;
  String _err;
  List<Result> _result;

  Autogenerated({String flag, String msg, String err, List<Result> result}) {
    this._flag = flag;
    this._msg = msg;
    this._err = err;
    this._result = result;
  }

  String get flag => _flag;
  set flag(String flag) => _flag = flag;
  String get msg => _msg;
  set msg(String msg) => _msg = msg;
  String get err => _err;
  set err(String err) => _err = err;
  List<Result> get result => _result;
  set result(List<Result> result) => _result = result;

  Autogenerated.fromJson(Map<String, dynamic> json) {
    _flag = json['flag'];
    _msg = json['msg'];
    _err = json['err'];
    if (json['result'] != null) {
      _result = new List<Result>();
      json['result'].forEach((v) {
        _result.add(new Result.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['flag'] = this._flag;
    data['msg'] = this._msg;
    data['err'] = this._err;
    if (this._result != null) {
      data['result'] = this._result.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Result {
  String _nAME;

  Result({String nAME}) {
    this._nAME = nAME;
  }

  String get nAME => _nAME;
  set nAME(String nAME) => _nAME = nAME;

  Result.fromJson(Map<String, dynamic> json) {
    _nAME = json['NAME'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['NAME'] = this._nAME;
    return data;
  }
}
javiercbk commented 4 years ago

Hey, nice find. The tool should generate camelCase property names.

So the new rule should be, if a property name is all UPPERCASE, then lowercase the property name in the dart class.