emerald 1.1.1 emerald: ^1.1.1 copied to clipboard
Simple json serializer/deserializer for Dart (only JIT, not Flutter).
- Serialize object
- Deserialize object
- Work with collections
- Set JSON constructor
- Change name of JSON fields
Serialize object #
class User {
final String name;
final int age;
User(this.name, this.age);
}
void main() {
// Creates instance of your object
var object = User('Alex', 21);
// Serializes instance of User to json string
var json = Emerald.serialize(object);
print(json);
}
Deserialize object #
class User {
final String name;
final int age;
User(this.name, this.age);
@override
String toString() {
return 'User with name: $name, age: $age.';
}
}
void main() {
var json = '{"name": "Alex", "age": 21}';
// Deserializes json string to instance of class
var object = Emerald.deserialize<User>(json);
print(object);
}
Work with collections #
class Point {
final double x;
final double y;
Point(this.x, this.y);
}
void main() {
var list = [Point(0.1, 0.25), Point(0.7, -0.1)];
// Serializes list to json string
var json = Emerald.serialize(list);
print(json);
}
Set JSON constructor #
class User {
final String name;
final int age;
final List<User> friends;
User(this.name, this.age, [this.friends = const []]);
// Use json constructor annotation for set this constructor as constructor for Emerald
@jsonConstructor
User.json({required this.name, required this.age, required this.friends});
bool isEqual(User other) {
if (name != other.name) {
return false;
}
if (age != other.age) {
return false;
}
if (friends.length != other.friends.length) {
return false;
}
for (var i = 0; i < friends.length; i++) {
if (!friends[i].isEqual(other.friends[i])) {
return false;
}
}
return true;
}
}
void main() {
// Creates instance of your object
var object = User('Alex', 21, [User('Bill', 22)]);
// Serializes it to json string
var json = Emerald.serialize(object);
print(json);
// Deserializes json string to instance of Gamer class
var deserialized = Emerald.deserialize<User>(json);
// Checks is initial object and deserialized object equal
var isEqual = object.isEqual(deserialized);
print('Is initial object and deserialized object equal: $isEqual.');
}
Change name of JSON fields #
class User {
// Use json property annotation for set custom json name for this field
@JsonProperty(name: 'custom_name')
final String name;
// Use json property annotation for set custom json name for this field
@JsonProperty(name: 'custom_age')
final int age;
// Use json parameter annotation for set custom json name for parameters
User(@JsonParameter(name: 'custom_name') this.name,
@JsonParameter(name: 'custom_age') this.age);
bool isEqual(User other) {
return name == other.name && age == other.age;
}
}
void main() {
// Creates instance of your object
var object = User('Alex', 21);
// Serializes it to json string
var json = Emerald.serialize(object);
print(json);
// Deserializes json string to instance of class
var deserialized = Emerald.deserialize<User>(json);
// Checks is initial object and deserialized object equal
var isEqual = object.isEqual(deserialized);
print(isEqual);
}