Easily convert common Dart data types into a json compatible ones.
Getting Started
This package provides a unified way to work with Dart data type serialization.
All converters implement the JsonConverter interface which has two methods:
- toJson, which serializes the value.
- fromJson, which deserializes the value.
Each converter in this package has a nullable and non-nullable variation.
A value passed to the non-nullable converter should be also non-nullable.
The name of the nullable converter starts with optional. It may return a
nullable value.
For a complete example, see Example tab.
Installation
This is a standalone helper package which does not have any dependencies.
In general, put the reference to this package under dependencies in your
pubspec.yaml.
dependencies:
json_converters:
Usage
To use this package, import it in your code:
import 'package:json_converters_lite/json_converters_lite.dart';
And use any of the currently available converters, currently featuring:
- BoolConverter, which converts a
boolto anint. - DateTimeConverter, which converts a
DateTimeto theISO8601string. - DurationConverter, which gets a total amount of seconds from
Duration. - EnumConverter, which serializes
Enumvalue as string from it's name. - IterableConverter, which takes the other
converteras an argument and serializes each value in theIterablewith that converter.
Also, you can create your own converters and use them, by implementing the
JsonConverter interface.
Contributing
You can contibute by designing your own converters and proposing them to be added to this package. Also, any bug reports are appreciated.
FAQ
Why create a package for such basic features?
Because, these features are frequently used in different projects. Thus, it is very useful and convenient to have a consistent code base between projects.