dartx_serialization 1.0.0
dartx_serialization: ^1.0.0 copied to clipboard
A Json serialization package that use reflection to serialize object to json string and deserialize json string to class object..
A Json serialization package use reflection to serialize object to json string and deserialize json string to class object.
Features #
- easy to use.
- support enum values.
- support datetime object.
- support serialize any value and deserialize to any class value using
@ValueConvertorannotation.
Getting started #
add dependencies to pubspec.yaml file
dependencies:
dartx_serialization: ^1.0.0
Usage #
To make a class serializable annotate it with @Serializable annotation.
import 'package:dartx_serialization/dartx_serialization.dart';
@Serializable()
class Person{}
Get instance of DartXJson class using static field instance
import 'package:dartx_serialization/dartx_serialization.dart';
final jsonSerialization = DartXJson.instance;
To serialize to json string use toJson() method which accept value with following types: list, map, object.
import 'package:dartx_serialization/dartx_serialization.dart';
final jsonSerialization = DartXJson.instance;
final person = Person(name: 'name', age: 24);
final personJsonStr = jsonSerialization.toJson(person);
print(personJsonStr);
To deserialize to class object use fromJson<T>(source) method which accept string value represent json string and has generic type T to determine the class which we need to create object from it.
import 'package:dartx_serialization/dartx_serialization.dart';
final jsonString = '{"name":"person name", "age":24}';
final jsonSerialization = DartXJson.instance;
final person = jsonSerialization.fromJson<Person>(jsonString);
Additional information #
Custom json key #
By default the json key of any class field is the name of that field, to set custom json key you can use @JsonKey() annotation.
class Person{
@JsonKey('p_name')
final String name;
}
Ignore some fields #
To ignore some class fields form serialization and deserialization process use @Ignore() annotation.
Note: you must initialized
ignoredfields on declaration or in constructor using default value otherwise theNoMatchConstructorErrorwill be occurred.
class Person{
final String name;
final int age;
@Ignore()
final bool registered;
Person(this.name, this.age, [this.registered = false]);
}
Serialize (and Deserialize) unsupported types #
Supported types in this package by default is: primitive types, list, map, object that is serializable, enum, datetime.
To use other type that not included in above list use @ValueConvertor<T1, T2> annotation, this annotation accept tow parameters (convertor and deconvertor).
The convertor is a function that accept T2 value and return T1 value.
The deconvertor is a function that accept T1 value and return T2 value.
class Person{
final String name;
final int age;
@ValueConvertor<int, Duration>(convertor: _convert, deconvertor: _deconvert)
final Duration birthday;
...
static int _convert(Duration d) => d.inMilliseconds;
static Duration _deconvert(int millis) => Duration(milliseconds: millis);
}
deserialize list type #
By default the json array element is deserialized to List<dynamic> type. if your class has field of list of type other than dynamic you must specify the type of list explicitly using @ListType<T> annotation, otherwise the ParameterValueNotAccepted error will be thrown.
@Serializable()
class Person{
...
@ListType<double>()
List<double> grades;
}
Limitation #
The reflection on dart is only supported by Dart VM, mean that the package is only work on platforms that has Dart VM.