Skip to content

ishkawa/custom_json_serializable_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

custom_json_serializable_example

An example project that generates fromJson/toJson methods for custom types.

The root package of this repository is a code generator based on json_serializable. As an example of customization, this package also supports serialize(deserialize) DateTime to(from) UNIX milliseconds.

For example, suppose we have a class below:

@JsonSerializable()
class Item {
  String name;
  DateTime createdAt;

  Item(this.name, this.createdAt);

  factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
  Map<String, dynamic> toJson() => _$ItemToJson(this);
}

An instance of this class Item('Apple', DateTime(2020, 3, 8)) can be serialized into following JSON:

{
    "name": "Apple",
    "createdAt": 1583593200000
}

In the example package, you can re-generate fromJson/toJson methods by pub run build_runner build.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages