Site icon Flutter Packages | Pub dev Packages – Flutter Mobile App World

From JSON to Advanced Dart Model

From JSON to Dart Advanced

Table of Contents

  1. Features
    • Convert from clipboard
    • Convert from selection
    • Convert from clipboard to code generation
    • Convert from selection to code generation
    • Convert from file
    • Annotations
    • Speed up converting
    • Enhanced file names
    • Context actions
  2. The Syntax
    • toString method
    • copyWith method
    • Equality Operator
    • Equatable
    • Null safety
  3. Supported Generators
    • Freezed
    • JSON Serializable
  4. How to use
    • Customize
    • Key bindings
    • Freezed documentation
    • Converter
    • Known Issues

Given a JSON string, this library will generate all the necessary Dart classes to parse and generate JSON. Also designed to generate Flutter-friendly model classes following the Flutter’s doc recommendation and Effective Dart: Style. Extention supports for both Serializing JSON manually and Serializing JSON using code generation libraries like Freezed and Json Serializable.

Note: when you use Freezed or Json Serializable then Json to Dart Model generates only types and everything that happens after, then Dart Build System builders takes care of the rest and is responsible for generated code.

How it Works

Dart to Json Model Generator creates your JSON object into separate files and thanks to this if similar structures are detected generator will create them into different files and merge them with path (import) no matter how named your objects are. In this way, you can keep your code cleaner and more readable. The pathname in the first will be renamed with the class name added as a prefix to show from which class the objects are. If the names continue to be duplicated then will be marked with the index for infinity renaming.

Customize

To customize your classes is very easy. If you want fast to create a simple class then just click enter continue to skip all methods. Otherwise, build your own. To generate Freezed class and Json Serializable choose Code Generation. Of course, you can set up your configuration in the Settings/Extensions/JSON To Dart Model

Features

Convert from clipboard to manual model classes

Convert from selection to manual model classes

Convert from clipboard to code generation libraries supported model classes

Convert from selection to code generation libraries supported model classes

Convert from file

Json to Dart Model generator keeps all your JSON objects in the file with the name models.jsonc and allows you to configure your classes according to your preferences. models.jsonc content is a list that contains all of your JSON objects that will later be converted to Dart classes. The jsonc format allows you to comment on your JSON objects to easily find them later or make it easier to explain to your team. To create the models.jsonc file you can run command in the command palette Build Models or use keys binging Shift + Ctrl + Alt + B and you will be asked if you want to create a file, hit Enter to add the file. To configure options for output, go to the Settings/Extensions/JSON To Dart Model

Create file manually. Just add a new file to your app directory my_app/models.jsonc and put all JSON objects to the list object separated by commas. Note that you add base class names to each object with key "__className": "MyClass", the class name will be removed from the object and used as the root class name for your code syntax. Duplicate class names are not allowed to avoid overwriting the files. Inside your models.jsonc. By adding key __path you can override the default path and navigate your models where you want and how your want in your workspace. File should look like this:

[
  {
    "__className": "UserPost", // <- The base class name of the object.
    "__path": "/lib/models/user_post", // <- override default path with a new one by adding '__path' key.
    "userId": 1,
    "id": 1, // To mark as required value, change "id" to "r@id".
    "title": "Json To Dart Model", // To mark as a default value, change "title" to "d@title".
    "body": "Json to Dart advanced..."
  }
]

After adding the object and convert to Dart classes just run a command from the command palette or simpler use key binding Shift + Ctrl + Alt + B. If you want to update some class, just delete the class folder from the directory and run again Build Models and Json to Dart Model will generate the missing directory.

Enhanced File Names

Enhanced file names like user.model.dart can make you code friendly with other converters such as like Hive Object Converter. To create it you just need to separate your class name with a dot and after the dot, everything will be added as an enhancement name. Example: user.model result user.model.dart. Just that.

Context Actions

Convert JSON from the right mouse button context menu.

Annotations

It is possible to mark JSON values as default or required. Everything that do you need to do, just add to your JSON key d@ or r@ and Json to Dart Model will generate them for you.

{
  "r@id": 1,
  "d@title": "Json To Dart Model"
}

Result:

class Example {
  int id;
  String title;

  Example({@required this.id, this.title = 'Json To Dart Model'});
  
  // The rest...
}

This also works with Freezed and JSON Serializable, also initializes non-constant values like DateTime if marked as default.

Note: what happens if I use multiple annotations "r@d@key" then the generator will prioritize the default value and generate value as the default since only named parameters without a default value can be with required.

Speed Up Converting

If you work a lot with JSON files and get tired every time customize your models in the command palette. Then you can go to the Settings/Extensions/JSON To Dart Model and set the primary configuration to true. And Json to Dart Model will use settings configuration everywhere and never ask you about input. Just choose any command from the selection or clipboard and pick the directory. Set fast mode to true for faster converting to the default directory.

JSON Serializable

Add serializing JSON using code generation libraries to pubspec.yaml

structure of the pubspec.yaml

dependencies:
  # Your other regular dependencies here
  json_annotation: <latest_version>

dev_dependencies:
  # Your other dev_dependencies here
  build_runner: <latest_version>
  json_serializable: <latest_version>

Freezed

Freezed supports both old versions to 0.12.7 and new from 0.14.0 and higher. Freezed requires three packages to generate JSON files to Freezed classes with a few clicks.

structure of the pubspec.yaml

 dependencies:
   # Your other regular dependencies here
   freezed_annotation: <latest_version>

 dev_dependencies:
   # Your other dev_dependencies here
   build_runner: <latest_version>
   freezed: <latest_version>

Read more about how to install Freezed.

All generated classes with Freezed will be @immutable and support all methods like copyWithtoString, equality operator==… See example:

@freezed
class Todo with _$Todo {
  factory Todo({
    @JsonKey(name: 'todo_id') int? todoId,
    String? description,
    bool? completed,
  }) = _Todo;

  factory Todo.fromJson(Map<String, dynamic> json) => _$TodoFromJson(json);
}

Freezed generator is useful for those who work daily with coding. All you have to do is upgrade some values and Freezed will take care of the rest. You don’t need to worry that you have forgotten to update the parser to some method. More what you can do with Freezed read Freezed documentation.

TIP: If you think that you have too many generated files you can look at tips by Freezed on how to ignore lint warnings on generated files.

Equatable

Equatable is the immutable class with the ability to compare your generated models in a better way. You can check if 2 classes, which are different instances, are equals without a single line of extra code. Of course, you can add toString method and copyWith for a better experience.

class Todo extends Equatable {
  final int? id;
  final String? description;
  final bool? completed;

  const Todo({this.id, this.description, this.completed});

  factory Todo.fromJson(Map<String, dynamic> json) => Todo(
        id: json['id'] as int?,
        description: json['description'] as String?,
        completed: json['completed'] as bool?,
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'description': description,
        'completed': completed,
      };

  // Here will be more methods after your customization.
  // toString();
  // copyWith();

  @override
  List<Object?> get props => [id, description, completed];
}

To add Equatable support you just have to select Equatable equality support when the process of parsing your JSON to Code has started and the extension will take care of setting up the advanced code equality check-in your Dart models.

Equality Operator

If you don’t want to install the Equatable package and work with @immutable classes and values then you can add equality operator and customize your class as mutable.

@override
bool operator ==(Object other) {
  if(identical(other, this)) return true;
  return other is Todos &&
    other.userId == userId &&
    other.id == id &&
    other.title == title &&
    other.completed == completed;
}

@override
int get hashCode => userId.hashCode ^ id.hashCode ^ title.hashCode ^ completed.hashCode;

To String Method

You can add toString method in your classes to improve the debugging experience.

@override
String toString() {
  return 'Todos(userId: $userId, id: $id, title: $title, completed: $completed)';
}

Equatable can implement toString method including all the given props. If you want that behaviour, just have to select Stringify method when the process of parsing your JSON to Code has started.

@override
bool get stringify => true;

CopyWith Method

copyWith method will make your life easier with @immutable classes. Highly recommended with immutable classes.

Todo copyWith({
  int? id,
  String? description,
  bool? completed,
}) {
  return Todo(
    id: id ?? this.id,
    description: description ?? this.description,
    completed: completed ?? this.completed,
  );
}

Null Safety

Null-Safety is enabled as default like in Dart language and it will indicate that a variable may have the value null. To disable it, go to the Settings/Extensions/JSON To Dart Model.

Note: make sure your packages also support Dart null safety.

Serializing JSON Using Code Generation Libraries

If you’d like to use Code Generation Libraries from Flutter, first of all, I suggest you add dependencies to the pubspec.yaml file. It also can be done with this extension. You don’t need to worry about it  After that, you can convert your JSON to model classes. Then you need to run the flutter pub run build_runner build command to generate the missing code of the models, according to Flutter documentation. Fortunately, the extension automatically opens a new terminal session and runs that command for you, yey 

How To Use

  1. Select a valid JSON. Press Ctrl + shift + P (Linux and Mac) or Ctrl + P (Windows) and search for Convert From Selection or Convert From Selection To Code Generation Supported Classes. Provide a Base class name and location to save.
  2. Copy a valid JSON. Press Ctrl + shift + P (Linux and Mac) or Ctrl + P (Windows) and search for Convert From Clipboard or Convert From Clipboard To Code Generation Supported Classes. Provide a Base class name and location to save.
  3. Press Ctrl + shift + P (Linux and mac) or Ctrl + P (Windows) and search for Add Code Generation Libraries To pubspec.yaml and hit enter.
  4. Press Ctrl + shift + P (Linux and Mac) or Ctrl + P (Windows) and search for Build Models and hit enter.
  5. Using short cuts.
  6. Convert from the right mouse button context.

Key Bindings

Convert from Clipboard (Shift + Ctrl + Alt + V)

Convert from Selection (Shift + Ctrl + Alt + S)

Convert from file (Shift + Ctrl + Alt + B)

Convert from Clipboard to Code Generation supported classes (Shift + Ctrl + Alt + G)

Convert from Selection to Code Generation supported classes (Shift + Ctrl + Alt + H)

Converter

Known Issues

  1. Using key binding on Linux can throw error Command failed: xclip -selection clipboard -o it happens when Linux lacks clipboard package. To resolve this error run in the terminal this command to install the missing package.sudo apt-get install xclip
  2. Matches the wrong type. In my experience, some API deliverers write integer values instead of double, for example, 1 or 1.00 instead of 1.10. The problem is that this generator does a deep object scan and reads each item to detect the type of value and returns the type as found. But with lists works well, if the list only has double and integers, the list type returns as num. If you write yourself JSON objects try to give the right value type for better results. It’s all about JSON quality 

Support us

If you like this, please give us the  and share with your friends. Thank you 

License

Distributed under the MIT License. See LICENSE for more information.

Contribute to JSON to Advanced Dart Model source code on GitHub

Exit mobile version