jaguar_serializer
Format agnostic Serializer library that can be used in vm, browser and flutter for JSON, mongodb, postgresql, etc
Getting Started
Install
#pubspec.yaml
dependencies:
jaguar_serializer:
dev_dependencies:
build_runner:
jaguar_serializer_cli:
Simple serializer
Import jaguar_serializer
import 'package:jaguar_serializer/jaguar_serializer.dart';
Create your model.
/// User model
class User {
String name;
int age;
}
Declare a Serializer for your model.
@GenSerializer()
class UserJsonSerializer extends Serializer<User> with _$UserJsonSerializer {
}
Include the generated serializer functionality.
part 'user.jser.dart';
Generate Serializer
Build
Now you can build you serializer running the command
pub run build_runner build
# flutter
flutter packages pub run build_runner build
This command will generate _$UserJsonSerializer
in file 'user.jser.dart'.
Use Serializer
A Serializer will serialize and deserialize between your model and Map<String, dynamic>
, that can be used to apply
conversion to JSON, YAML, XML, etc.
import 'package:jaguar_serializer/jaguar_serializer.dart';
import 'model/user.dart';
void main() {
final userSerializer = new UserJsonSerializer();
User user = userSerializer.fromMap({
'name': 'John',
'age': 25
});
print(userSerializer.toMap(user));
}
Serializer repository
You can also use a JSON repository or implement one.
import 'package:jaguar_serializer/jaguar_serializer.dart';
import 'model/user.dart';
void main() {
final jsonRepository = new JsonRepo()..add(new UserSerializer());
User user = jsonRepository.from<User>('{"name":"John","age": 25}');
print(jsonRepository.serialize(user));
}