Format agnostic Serializer library that can be used in vm, browser and flutter for JSON, mongodb, postgresql, etc
#pubspec.yaml
dependencies:
jaguar_serializer:
dev_dependencies:
build_runner:
jaguar_serializer_cli:
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';
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'.
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));
}
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));
}
DateTimeMillisecondsProcessor
with MillisecondsProcessor
MillisecondsProcessor
utc bugDateProcessor
, use DateTimeProcessor
insteadDateTimeProcessor.utc()
constructorDateTimeUtcProcessor()
date_format
dependencypass
annotationpassProcessor
PassProcessor
Map<dynamic, dynamic>
Set
are not serializableintroduce field format to automatically convert field Example:
class Model {
String myField;
}
@GenSerializer(fieldFormat: FieldFormat.snakeCase)
class ModelSerializer extends Serializer ... {}
final map = { 'my_field': 'foo' };
final model = serializer.fromMap(map);
print(model.myField); // print 'foo'
we now support final field deserialization
createModel()
in Serializer
class is now deprecated, it will be remove in the final release of 1.0.0
*** Breaking Changes ***
Map<String, dynamic>
can be encodetoMap(null)
return nullfromMap(null)
return nullfromMap({})
return objectbuild.yaml
and build_runner
processors
field in GenSerializer
use processor
field in Property
insteadGenSerializer.processor
fields, FieldProcessor go into GenSerializer.fields
inside Property
String
, int
, double
, bool
, if a field is non nullable, jaguar will use the default value declared in you object constructorBreaking Change
EnDecode
, DecodeOnly
, EncodeOnly
does not use optional parameter in constructor anymore,
example: EnDecode({this.alias, bool isNullable, FieldProcessor<T, dynamic> processor, T defaultsTo})
Breaking Changes
@GenSerializer(
fields: const {
'field': const EncodeOnly('f')
},
ignore: const [
'ignoreField'
],
processors: const {
'dates': const DateTimeProcessor(),
},
serializers: const [
FieldSerializer,
])
to
, from
, serialize
, deserialize
methods now handle null
values correctlyserialize
and deserialize
methods now handle null
values correctlySerializerRepo
handles when type
is dynamic
from
and to
methods to SerializerRepo
Map
, List<List>
, etcaddAll
method to SerializerRepo
to add multiple serializersRawData
custom field processorRawData
Processorbuild
package to >=0.9.3 <0.10.0
serializers.yaml
is now optional, Jaguar Serializer will take all dart files under lib
, bin
, example
and test
folders.
Be careful on big project, watch
can be slow or take a lot of resources.build
(>=0.7.0 <0.8.0)serializer init
command, generate serializer.yaml
New
Repository
system (Json, Yaml)bool typeKey (default:true)
if you want to include the typeString modelName
value that will be include as a type.First release.
Contains:
example/example.dart
import 'package:jaguar_serializer/jaguar_serializer.dart';
part 'example.jser.dart';
class Player {
String name;
String email;
int score;
List<Address> address;
Player({this.name, this.email, this.score, this.address});
String toString() => 'Player($name, $email, $score, $address)';
}
class Address {
String street;
String city;
Address({this.street, this.city});
String toString() => 'Address($street, $city)';
}
@GenSerializer()
class PlayerSerializer extends Serializer<Player> with _$PlayerSerializer {}
@GenSerializer()
class AddressSerializer extends Serializer<Address> with _$AddressSerializer {}
void main() {
// user basic serializer
PlayerSerializer plSer = new PlayerSerializer();
Player player = new Player(
name: 'John',
email: 'john@noemail.com',
score: 1000,
address: [(new Address(street: 'Skögsangavägen', city: 'Stockholm'))]);
final Map map = plSer.toMap(player);
print(map);
Player decoded = plSer.fromMap(map);
print(decoded);
}
Add this to your package's pubspec.yaml file:
dependencies:
jaguar_serializer: ^2.2.8
You can install packages from the command line:
with pub:
$ pub get
with Flutter:
$ flutter packages get
Alternatively, your editor might support pub get
or flutter packages get
.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:jaguar_serializer/jaguar_serializer.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
2.2.8 | Jan 28, 2019 |
|
|
2.2.6 | Dec 27, 2018 |
|
|
2.2.5 | Dec 2, 2018 |
|
|
2.2.4 | Sep 19, 2018 |
|
|
2.2.2 | Sep 10, 2018 |
|
|
2.2.0 | Aug 7, 2018 |
|
|
2.1.14 | Jul 5, 2018 |
|
|
2.1.12 | Jul 5, 2018 |
|
|
2.1.10 | Jun 29, 2018 |
|
|
2.1.9 | Jun 29, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
90
|
Health:
Code health derived from static analysis.
[more]
|
94
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
93
|
We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, web, other
No platform restriction found in primary library
package:jaguar_serializer/jaguar_serializer.dart
.
Fix lib/src/annotations/annotations.dart
. (-3.45 points)
Analysis of lib/src/annotations/annotations.dart
reported 7 hints, including:
line 46 col 19: Use =
to separate a named parameter from its default value.
line 47 col 18: Use =
to separate a named parameter from its default value.
line 48 col 23: Use =
to separate a named parameter from its default value.
line 49 col 28: Use =
to separate a named parameter from its default value.
line 50 col 26: Use =
to separate a named parameter from its default value.
Fix lib/src/serializer/field_processor.dart
. (-2.48 points)
Analysis of lib/src/serializer/field_processor.dart
reported 5 hints:
line 144 col 48: Use =
to separate a named parameter from its default value.
line 156 col 48: Use =
to separate a named parameter from its default value.
line 168 col 48: Use =
to separate a named parameter from its default value.
line 182 col 51: Use =
to separate a named parameter from its default value.
line 247 col 45: 'DateTimeMillisecondsProcessor' is deprecated and shouldn't be used.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0 <3.0.0 | ||
Dev dependencies | |||
build_runner | ^1.0.0 | ||
jaguar_serializer_cli |