hiranthaR / Json-to-Dart-Model

Json to Dart Model extension can convert JSON objects into Dart data classes. It supports pure Dart class conversion, Flutter-recommended JSON serialization using annotations, Freezed support, Effective Dart:Style, and many more features. Currently, it has more than 135,000 installs.
https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart
MIT License
93 stars 18 forks source link

[BUG] Class name problem if have same value in JSON (for example value 'list') #100

Open davaidev opened 1 year ago

davaidev commented 1 year ago

Is there an existing issue for this?

Current Behavior

First problem - when I try to convert JSON to dart model, I have a problem with class name, because one of JSON value is 'list' and plugin create a class with name 'List' that cannot work

screen

Second problem its a name of main class of converting JSON, for example I want to call it Weather, but in my JSON have a value 'weather' so when plugin create model files it creates only one file weather.dart and it misses create class weather from JSON value 'weather'

weather

Expected Behavior

To fix first problem plugin must check if value inside JSON is named already in dart like 'list' so create class with name "WeatherList" like "name_main_class+name_of_json_value"

forecast

To fix second problem plugin must check if value inside JSON is named like the main class like 'weather' so rename to create both files of classes

classes

Steps To Reproduce

  1. copy JSON from https://api.openweathermap.org/data/2.5/forecast/daily?lat=44.34&lon=10.99&cnt=7&appid=1369dd6b5ae78fc9952261ab9aa236b4
  2. paste it in VS Code JSON to dart model
  3. name main class 'Weather' and see this 2 errors
  4. fix it and update plugin
  5. be thankful from community :-)

Version

Json to Dart Model v3.5.8 (VS Code 1.72.2)
Flutter 3.3.4
Dart 2.18.2

Relevant JSON syntax

{"city":{"id":3163858,"name":"Zocca","coord":{"lon":10.99,"lat":44.34},"country":"IT","population":4593,"timezone":7200},"cod":"200","message":15.0402978,"cnt":7,"list":[{"dt":1665658800,"sunrise":1665638877,"sunset":1665678982,"temp":{"day":290.56,"min":282.53,"max":290.98,"night":284.53,"eve":288.15,"morn":282.53},"feels_like":{"day":289.84,"night":283.68,"eve":287.43,"morn":282.53},"pressure":1020,"humidity":57,"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"speed":2.57,"deg":45,"gust":2.53,"clouds":30,"pop":0.03},{"dt":1665745200,"sunrise":1665725353,"sunset":1665765278,"temp":{"day":290.64,"min":282.43,"max":291.11,"night":283.91,"eve":285.76,"morn":282.43},"feels_like":{"day":289.8,"night":283.1,"eve":284.98,"morn":282.12},"pressure":1018,"humidity":52,"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"speed":2.4,"deg":28,"gust":1.73,"clouds":39,"pop":0},{"dt":1665831600,"sunrise":1665811828,"sunset":1665851575,"temp":{"day":292.55,"min":282.66,"max":292.55,"night":284.87,"eve":286.2,"morn":282.66},"feels_like":{"day":291.72,"night":284.11,"eve":285.39,"morn":282.66},"pressure":1016,"humidity":45,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":2.07,"deg":48,"gust":1.93,"clouds":4,"pop":0},{"dt":1665918000,"sunrise":1665898304,"sunset":1665937872,"temp":{"day":292.12,"min":283.55,"max":292.12,"night":284.89,"eve":285.88,"morn":283.55},"feels_like":{"day":291.35,"night":284.23,"eve":285.22,"morn":282.71},"pressure":1021,"humidity":49,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"speed":1.81,"deg":221,"gust":1.68,"clouds":86,"pop":0},{"dt":1666004400,"sunrise":1665984780,"sunset":1666024171,"temp":{"day":293.02,"min":283.76,"max":293.02,"night":285.51,"eve":286.19,"morn":283.81},"feels_like":{"day":292.34,"night":284.89,"eve":285.59,"morn":283.12},"pressure":1027,"humidity":49,"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"speed":1.89,"deg":44,"gust":1.85,"clouds":80,"pop":0},{"dt":1666090800,"sunrise":1666071257,"sunset":1666110471,"temp":{"day":293.38,"min":284.29,"max":293.38,"night":285.82,"eve":286.79,"morn":284.29},"feels_like":{"day":292.71,"night":285.23,"eve":286.19,"morn":283.68},"pressure":1026,"humidity":48,"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"speed":1.89,"deg":31,"gust":1.7,"clouds":29,"pop":0},{"dt":1666177200,"sunrise":1666157734,"sunset":1666196771,"temp":{"day":293.37,"min":284.25,"max":293.37,"night":285.81,"eve":286.69,"morn":284.25},"feels_like":{"day":292.65,"night":285.04,"eve":285.95,"morn":283.64},"pressure":1020,"humidity":46,"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"speed":2.14,"deg":15,"gust":1.76,"clouds":4,"pop":0}]}

Anything else?

No response

iamarnas commented 1 year ago

@davaidev Thank you for reporting this. This is known issue, because this generator do not rename reserved types by system. For now you can rename your JSON list key and generator will generate all as expected.

davaidev commented 1 year ago

@davaidev Thank you for reporting this. This is known issue, because this generator do not rename reserved types by system. For now you can rename your JSON list key and generator will generate all as expected.

For me is ok, I fixed, just let u know guys about this 2 issues, will be good if u will fix it by adding renaming. Thx for your product!