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

class reference in generated classes #45

Closed Miamoto-Musashi closed 2 years ago

Miamoto-Musashi commented 3 years ago

would be nice to have the possibility to put object type in the json

//   USER
  {
    "__className": "User",
    "key": "110afe06-ec31-4d9b-b933-d9e060c7bcb7",
     ....
    "country": null, <---// want it Country class not String
    ....
  },

only way to have the country not String type is put it as null and use the dynamic type on conversion. Would be nice to have the feature to put the Country object instead...

using jsonc generation probably we can use some kind of placeholder //@Country@ ?

just a quick idea

iamarnas commented 3 years ago

Hi @Miamoto-Musashi

I think is bad practice to generate objects as null. It will show lint errors without Object class and it not possible generate class without the properties. And you will be forced to implement everything manually into County class. It is equal to re-implementation of dynamic value.

//   USER
{
    "__className": "User",
    "key": "110afe06-ec31-4d9b-b933-d9e060c7bcb7",
    "country": null, <---// it will show lint error without `dynamic`
},
Miamoto-Musashi commented 3 years ago

How can I then tell the generator that I want the country property to be of type Country class? at them moment the only workaround I found is to put null in the jsonc file so that the generator create the property with a dynamic type and I can compile.

thanks

iamarnas commented 3 years ago

How can I then tell the generator that I want the country property to be of type Country class?

You can tell to the generator by adding empty brackets {} then the generator will generate an empty class without parameters instead of dynamic. But you will need to correct it a little bit to avoid lint errors.

{
    "__className": "User",
    "id": "110",
    "country": {} // <- to tell generate class "Country" instead of dynamic.
}

@Miamoto-Musashi I will look at this how-to it can be improved without lint errors.

iamarnas commented 3 years ago

@Miamoto-Musashi HI.

It looks like a bug. I fill fix it with this solution.

class Country {
  Country();

  factory Country.fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    throw UnimplementedError('Country.fromJson($json) is not implemented');
  }

  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    throw UnimplementedError();
  }
}
Miamoto-Musashi commented 3 years ago

Hi @iamarnas In real life I would probably prefer to have another Country object declared in jsonc file and refer to that one. having an empty Country object can be misleading.

there is another UseCase where reference would be preferred: think about this jsonc file snippet:

  {
    "distance":12.1,
    "positions":null,
    "bike":null
  }

here I have a single object (Bike) referenced and a List of objects (Position); now I'm using null to have dynamic generated so that I can address both cases, in case of strict typing I would prefer something like

  {
    "distance":12.1,
    "positions":[{@Position}],
    "bike":{@Bike}
  }
iamarnas commented 3 years ago

@Miamoto-Musashi It not easier to be better generate an empty object and changes the import from the right mouse button context to navigate to the implemented class.

{
    "positions":[{}], 
    "country": {} // <- you can rename your object how you want be changing key name.
}

Or I do not understand what really you mean!?

Miamoto-Musashi commented 3 years ago

yes it is easier as a workaround but I see this tool to help with automation, boilerplate and portability if I have to adjust every generated class by hand its loosing some of the util

what I think would be useful is some kind of reference from one object in the jsonc file to another object in the same jsonc file

iamarnas commented 3 years ago

You using this generator as tool to speed up coding? Such changes will throw the error by overriding Dart classes from JSON

Miamoto-Musashi commented 3 years ago

not sure to understand; say we have a User object and a Country object and a reference from Country to User like a 1:N relation in RDBMS; we can have the Country generated with a ref to User object,

in json notation would become:

off course if there is already a Country object it's going to be overridden or you can check if already exists and throw a warning but link the 2 objects.

You using this generator as tool to speed up coding? Such changes will throw the error by overriding Dart classes from JSON

I think this generator have great potential in the fields of:

  1. portability - we take take the json to Postman and generate the models for backend
  2. documentation - if well documented the domain models can explain better the project
  3. speed-up coding - generate models starting from json
arcas0803 commented 3 years ago

Hi, Any progress on how to parse refs? I think it's a better solution to use '@ClassName' because sometimes the field name it's not the same as the class name. Example: {photo: @File} Thanks for the amazing plugin.

iamarnas commented 3 years ago

@arcas0803 Hi :wave: Maybe you looking for that. Force change type by splitting with the dot.

{
    photo.file: { // generates: File photo; instead Photo photo;
        /*... */
    }
}
arcas0803 commented 3 years ago

You are right. It would be amazing to have some documentation on the batch Converter (With the .jsonc). Again, amazing work.

iamarnas commented 3 years ago

You are right. It would be amazing to have some documentation on the batch Converter (With the .jsonc). Again, amazing work.

@arcas0803 It is in beta right now when it will be stable. I will update the documentation. You can play with it and test. Any suggestion is welcome. Have fun with coding and I'm so glad you like it 😊

iamarnas commented 2 years ago

Closes it because the generator already supports it and no more reports from users.