Giter Club home page Giter Club logo

json_serializable_helper's Introduction

Pub Version codecov

By adding this package to your project, you can directly use json files as the input of the json_serializable package.

Diagram

Features

Imagine you have received the following response from an API call:

{
  "name": "John Doe",
  "age": 30,
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "country": "USA"
  },
  "job": {
    "title": "Software Engineer",
    "company": {
      "name": "TechCorp",
      "industry": "Technology"
    }
  }
}

If you want to decode and encode this simple JSON, you can use the json_serializable package. But before doing that, you need to create the corresponding person.dart class required by json_serializable:

import 'package:json_annotation/json_annotation.dart';

part 'person.g.dart';

@JsonSerializable()
class Person {
  @JsonKey(name: 'name', defaultValue: null)
  final String? name;

  @JsonKey(name: 'age', defaultValue: null)
  final int? age;

  @JsonKey(name: 'address', defaultValue: null)
  final Address? address;

  @JsonKey(name: 'job', defaultValue: null)
  final Job? job;

  Person({
    this.name,
    this.age,
    this.address,
    this.job,
  });

  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
  Map<String, dynamic> toJson() => _$PersonToJson(this);
}

@JsonSerializable()
class Address {
  @JsonKey(name: 'street', defaultValue: null)
  final String? street;

  @JsonKey(name: 'city', defaultValue: null)
  final String? city;

  @JsonKey(name: 'country', defaultValue: null)
  final String? country;

  Address({
    this.street,
    this.city,
    this.country,
  });

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

@JsonSerializable()
class Job {
  @JsonKey(name: 'title', defaultValue: null)
  final String? title;

  @JsonKey(name: 'company', defaultValue: null)
  final Company? company;

  Job({
    this.title,
    this.company,
  });

  factory Job.fromJson(Map<String, dynamic> json) => _$JobFromJson(json);
  Map<String, dynamic> toJson() => _$JobToJson(this);
}

@JsonSerializable()
class Company {
  @JsonKey(name: 'name', defaultValue: null)
  final String? name;

  @JsonKey(name: 'industry', defaultValue: null)
  final String? industry;

  Company({
    this.name,
    this.industry,
  });

  factory Company.fromJson(Map<String, dynamic> json) =>
      _$CompanyFromJson(json);
  Map<String, dynamic> toJson() => _$CompanyToJson(this);
}

And then by running the following command:

dart run build_runner build --delete-conflicting-outputs

The person.g.dart file will be generated for you and you are done!

As you can see, writing the person.dart class is a tedious and error-prone process, specially when the JSON is large and complex. This package aims to simplify this process by allowing you to use the JSON file as the input of the json_serializable. That means you only need to include the JSON file in your project, and then by running the same command above, the corresponding dart classes ( person.dart and person.g.dart) will be generated for you.

Usage

Add json_serializable and json_serializable_helper dependencies to your pubspec.yaml file:

dev_dependencies:
  json_serializable: ^6.7.1
  json_serializable_helper: ^1.0.0

Then add the build.yaml file to the root of your project. In this file you should specify the directory where your JSON file(s) will be found, for example:

global_options:
  json_serializable_helper:
    options:
      json_path: "lib/datamodels"

In the above example, your json file can be placed in the lib/datamodels directory or any subdirectory under it, for example lib/datamodels/aws/person.json.

Now run the following command to generate the dart classes:

dart run build_runner build --delete-conflicting-outputs

This will generate the person.dart and person.g.dart files next to your person.json file.

Contribution

If you find a bug or want to add a feature, please create an issue or a pull request.
Thank you!

json_serializable_helper's People

Contributors

meysammahfouzi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.