json_annotation in Flutter

Introduction:
JSON (JavaScript Object Notation) is a popular data interchange format used for sending and receiving data between a server and a client application. In Flutter, we often need to convert JSON data to Dart objects to be able to work with them easily. Handling JSON serialization and deserialization manually can be a tedious and error-prone task. Thankfully, Flutter provides us with a convenient solution through the json_annotation package.

Jsonannotation Package:
The json
annotation package is an official Flutter package that provides a set of annotations and code generators to simplify JSON serialization and deserialization in Dart. By using this package, we can annotate our Dart classes and generate code to handle the conversion between JSON and Dart objects automatically.

Installation:
To use the json_annotation package in your Flutter project, add it as a dependency in your pubspec.yaml file:

dependencies:
  json_annotation: ^4.4.0

After adding the dependency, run flutter pub get to fetch the package.

Usage:
Let’s say we have a Dart class representing a User object:

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User {
  final String name;
  final int age;

  User(this.name, this.age);

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}

Here, we use the @JsonSerializable annotation from the json_annotation package to indicate that this class should be processed for JSON serialization and deserialization. We also define two methods fromJson and toJson to handle the conversion between JSON and Dart objects.

To generate the necessary code for serialization and deserialization, we need to run the build_runner command:

flutter pub run build_runner build

This command will generate two additional files: user.g.dart and user.g.json. The user.g.dart file contains the generated code for our User class. The user.g.json file provides additional metadata information that can be used for further customization.

Now, we can use the generated code to serialize and deserialize JSON data:

import 'dart:convert';

void main() {
  String jsonStr = '{"name": "John", "age": 30}';

  Map<String, dynamic> json = jsonDecode(jsonStr);
  User user = User.fromJson(json);

  print('User name: ${user.name}');
  print('User age: ${user.age}');

  Map<String, dynamic> userJson = user.toJson();
  String userJsonStr = jsonEncode(userJson);

  print('User JSON: $userJsonStr');
}

In this example, we deserialize the JSON string using jsonDecode method from the dart:convert package, and then use the fromJson method generated by the json_annotation package to create a User object. We can access the properties of the User object as usual. Similarly, we can serialize the User object back to JSON using the toJson method.

Conclusion:
The json_annotation package in Flutter provides a convenient way to handle JSON serialization and deserialization in Dart. By using annotations and code generation, we can minimize the manual effort required for converting JSON data to Dart objects and vice versa. This package saves time and reduces the chances of introducing errors in the serialization process.