google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 397 forks source link

Is it possible to extend a model with additional info #1257

Closed Nedimko123 closed 1 year ago

Nedimko123 commented 1 year ago

This might be confusing...

Lets say we have a model

@JsonSerializable()
class Address {
  String street;
  String city;

  Address(this.street, this.city);

  factory Address.fromJson(Map<String, dynamic> json) =>
      _$AddressFromJson(json);
  Map<String, dynamic> toJson() => _$AddressToJson(this);
}

Is it possible that I can describe a specific field, for example street = "a public road in a city, town, or..." The scenario is like this: Text(Street.name, street.description) //describes what a street is -- Text( address[index].street), //value for a specific address input Text(City.name, city.description ) //describe what a city is -- Text(address[index].city,) //value for a specific address input

the JSON is Map<String, dynamic> = {'street' : 123 , 'city' : 123};

So I would make a helper class thats says street = {'name' : blabla, 'description' : blabla} etc...

In my case I have over 100 json fields, so I think that creating 100+ models for each field might be time consuming

kevmoo commented 1 year ago

@Nedimko123 – it is confusing.

What code do you want to write and what code do you want generated?

Nedimko123 commented 1 year ago

How can I get the field name like I can with regular List<Map<String, dynamic>>

const List<Map<String, dynamic>> jsonFoods = [
  {
    "category": "Human milk",
    "id": 11000000,
    "description": "Milk, human",
    "energy": 70,
    "protein": 1.03,
    "carbohydrate": 6.89,}];
  for (var element in jsonFoods) {
    element.forEach((key, value) {
      print('$key  -  $value');
    });
  }

which then returns

category  -  Human milk
id  -  11000000
description  -  Milk, human
energy  -  70
protein  -  1.03
carbohydrate  -  6.89
kevmoo commented 1 year ago

We have to talk in terms of what code you want to write and what code you want generated.

github-actions[bot] commented 1 year ago

Without additional information we're not able to resolve this issue, so it will be closed at this time. You're still free to add more info and respond to any questions above, though. We'll reopen the case if you do. Thanks for your contribution!