Skip to content

ishkawa/custom_json_serializable_example

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
lib
 
 
 
 
 
 
 
 
 
 

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