Viewing version 0.3.2. Open the latest package page.
json_serializable
0.3.2Generates utilities to aid in serializing to/from JSON.
Readme
Provides source_gen Generators to create code for JSON serialization and
deserialization.
See the example package to understand how to configure your project.
User defined and generated code
Given a library example.dart with an Person class annotated with
@JsonSerializable():
library json_serializable.example;
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
final String firstName;
@JsonKey(includeIfNull: false)
final String middleName;
final String lastName;
@JsonKey(name: 'date-of-birth', nullable: false)
final DateTime dateOfBirth;
@JsonKey(name: 'last-order')
final DateTime lastOrder;
@JsonKey(nullable: false)
List<Order> orders;
Person(this.firstName, this.lastName, this.dateOfBirth,
{this.middleName, this.lastOrder, List<Order> orders})
: this.orders = orders ?? <Order>[];
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}
Building creates the corresponding part example.g.dart:
Person _$PersonFromJson(Map<String, dynamic> json) => new Person(
json['firstName'] as String,
json['lastName'] as String,
DateTime.parse(json['date-of-birth'] as String),
middleName: json['middleName'] as String,
lastOrder: json['last-order'] == null
? null
: DateTime.parse(json['last-order'] as String),
orders: (json['orders'] as List)
.map((e) => new Order.fromJson(e as Map<String, dynamic>))
.toList());
abstract class _$PersonSerializerMixin {
String get firstName;
String get middleName;
String get lastName;
DateTime get dateOfBirth;
DateTime get lastOrder;
List<Order> get orders;
Map<String, dynamic> toJson() {
var $map = <String, dynamic>{};
void $writeNotNull(String key, dynamic value) {
if (value != null) {
$map[key] = value;
}
}
$map['firstName'] = firstName;
$writeNotNull('middleName', middleName);
$map['lastName'] = lastName;
$map['date-of-birth'] = dateOfBirth.toIso8601String();
$map['last-order'] = lastOrder?.toIso8601String();
$map['orders'] = orders;
return $map;
}
}
Changelog
0.3.2
- The
require_library_directiveoption now defaults tofalse. The option will be removed entirely in0.4.0.
0.3.1+2
- Support the latest version of the
analyzerpackage.
0.3.1+1
- Expanded
package:buildsupport to allow version0.12.0.
0.3.1
-
Add a
build.yamlso the builder can be consumed by users ofbuild_runnerversion 0.7.0. -
Now requires a Dart
2.0.0-devrelease.
0.3.0
-
NEW top-level library
json_serializable.dart.-
Replaces now deprecated
generators.dartto accessJsonSerializableGeneratorandJsonLiteralGenerator. -
Adds the
jsonPartBuilderfunction to make it easy to create aPartBuilder, without creating an explicit dependency onsource_gen.
-
-
BREAKING
UnsupportedTypeErroradded a new required constructor argument:reason. -
BREAKING The deprecated
annotations.dartlibrary has been removed. Usepackage:json_annotationinstead. -
BREAKING The arguments to
TypeHelperserializeanddeserializehave changed.SerializeContextandDeserializeContext(new classes) are now passed instead of theTypeHelperGeneratortypedef (which has been deleted).
-
JsonSerializableGeneratornow supports an optionaluseWrappersargument when generates and uses wrapper classes to (hopefully) improve the speed and memory usage of serialization – at the cost of more code.NOTE:
useWrappersis not guaranteed to improve the performance of serialization. Benchmarking is recommended. -
Make
nullfield handling smarter. If a field is classified as notnullable, then use this knowledge when generating serialization code – even ifincludeIfNullisfalse.
0.2.5
-
Throw an exception if a duplicate JSON key is detected.
-
Support the
nullablefield on theJsonSerializableclass annotation.
0.2.4+1
- Throw a more helpful error when a constructor is missing.
0.2.4
-
Moved the annotations in
annotations.darttopackage:json_annotations.- Allows package authors to release code that has the corresponding annotations without requiring package users to inherit all of the transitive dependencies.
-
Deprecated
annotations.dart.
0.2.3
- Write out
toJsonmethods more efficiently when the first fields written are not intercepted by the null-checking method.
0.2.2+1
-
Simplify the serialization of
Mapinstances when no conversion is required forvalues. -
Handle
intliterals in JSON being assigned todoublefields.
0.2.2
- Enable support for
enumvalues. - Added
asConsttoJsonLiteral. - Improved the handling of Dart-specific characters in JSON strings.
0.2.1
- Upgrade to
package:source_genv0.7.0
0.2.0+1
- When serializing classes that implement their own
fromJsonconstructor, honor their constructor parameter type.
0.2.0
-
BREAKING Types are now segmented into their own libraries.
-
package:json_serializable/generators.dartcontainsGeneratorimplementations. -
package:json_serializable/annotations.dartcontains annotations. This library should be imported with your target classes. -
package:json_serializable/type_helpers.dartcontainsTypeHelperclasses and related helpers which allow custom generation for specific types.
-
-
BREAKING Generation fails for types that are not a JSON primitive or that do not explicitly supports JSON serialization.
-
BREAKING
TypeHelper:-
Removed
canmethods. Returnnullfrom(de)serializeif the provided type is not supported. -
Added
(de)serializeNestedarguments to(de)serializemethods allowing generic types. This is how support forIterable,List, andMapis implemented.
-
-
BREAKING
JsonKey.jsonNamewas renamed tonameand is now a named parameter. -
Added support for optional, non-nullable fields.
-
Added support for excluding
nullvalues when generating JSON. -
Eliminated all implicit casts in generated code. These would end up being runtime checks in most cases.
-
Provide a helpful error when generation fails due to undefined types.
0.1.0+1
- Fix homepage in
pubspec.yaml.
0.1.0
-
Split off from source_gen.
-
Add
/* unsafe */comments to generated output likely to be unsafe. -
Support (de)serializing values in
Map. -
Fix ordering of fields when they are initialized via constructor.
-
Don't use static members when calculating fields to (de)serialize.
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'json_serializable:0.3.2'pubspec.yaml
name: json_serializable
version: 0.3.2
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
environment:
sdk: '>=2.0.0-dev <2.0.0'
dependencies:
analyzer: '>=0.29.10 <0.32.0'
build: '>=0.9.0 <0.13.0'
build_config: ^0.2.1
cli_util: ^0.1.0
# Use a tight version constraint to ensure that a constraint on
# `json_annotation`. Properly constrains all features it provides.
json_annotation: '>=0.2.2 <0.2.3'
path: ^1.3.2
source_gen: ^0.7.5
dev_dependencies:
build_runner: ^0.7.0
build_test: ">=0.9.0 <0.11.0"
collection: ^1.14.0
dart_style: ^1.0.0
test: ^0.12.3
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.1.0 | Cached · Upstream SHA-256 matched | Download | ea38aff2…357ce4 |
| 0.1.0+1 | Cached · Upstream SHA-256 matched | Download | 54608c1f…fae3e4 |
| 0.2.0 | Cached · Upstream SHA-256 matched | Download | 228de10d…0dc07d |
| 0.2.0+1 | Cached · Upstream SHA-256 matched | Download | bc4de154…cd2c76 |
| 0.2.1 | Cached · Upstream SHA-256 matched | Download | 925b3d51…19cab5 |
| 0.2.2 | Cached · Upstream SHA-256 matched | Download | 4bde8d44…7c6625 |
| 0.2.2+1 | Cached · Upstream SHA-256 matched | Download | 06b55410…03b5cf |
| 0.2.3 | Cached · Upstream SHA-256 matched | Download | 98f510ad…eac912 |
| 0.2.3+1 | Cached · Upstream SHA-256 matched | Download | e6363ffc…0f3f04 |
| 0.2.4 | Cached · Upstream SHA-256 matched | Download | 332c5d27…4db89f |
| 0.2.4+1 | Cached · Upstream SHA-256 matched | Download | 9ee6223b…9cd202 |
| 0.2.5 | Cached · Upstream SHA-256 matched | Download | 25d0c6df…5da947 |
| 0.3.0 | Cached · Upstream SHA-256 matched | Download | 8970e927…c017f6 |
| 0.3.1 | Cached · Upstream SHA-256 matched | Download | 4d856bb2…1086ee |
| 0.3.1+1 | Cached · Upstream SHA-256 matched | Download | ceaa1075…d43605 |
| 0.3.1+2 | Cached · Upstream SHA-256 matched | Download | 7576a95d…9b2e9d |
| 0.3.2 | Cached · Upstream SHA-256 matched | Download | 8fc35438…08ba16 |
| 0.4.0 | Cached · Upstream SHA-256 matched | Download | 52fca6a7…bbb0d5 |
| 0.5.0 | Cached · Upstream SHA-256 matched | Download | ceb97da7…f705f8 |
| 0.5.1 | Cached · Upstream SHA-256 matched | Download | 555278a0…31b474 |
| 0.5.2 | Cached · Upstream SHA-256 matched | Download | 4a477c42…dea9df |
| 0.5.3 | Cached · Upstream SHA-256 matched | Download | 453fab42…39f3f8 |
| 0.5.4 | Cached · Upstream SHA-256 matched | Download | 2b743d72…be94c7 |
| 0.5.4+1 | Cached · Upstream SHA-256 matched | Download | 8e4f2cfa…4fc7de |
| 0.5.5 | Cached · Upstream SHA-256 matched | Download | 825ff2da…d2676c |
| 0.5.6 | Cached · Upstream SHA-256 matched | Download | 11092952…b28665 |
| 0.5.7 | Cached · Upstream SHA-256 matched | Download | afd52873…21feff |
| 0.5.8 | Cached · Upstream SHA-256 matched | Download | 56d661f5…19ac8b |
| 0.5.8+1 | Cached · Upstream SHA-256 matched | Download | 6c80bfb5…d03209 |
| 1.0.0 | Cached · Upstream SHA-256 matched | Download | ca0c6ffc…e59fc5 |
| 1.0.1 | Cached · Upstream SHA-256 matched | Download | c1479f04…3d14ee |
| 1.1.0 | Cached · Upstream SHA-256 matched | Download | 46d41e4f…df92f9 |
| 1.2.0 | Cached · Upstream SHA-256 matched | Download | 4075e577…1b11b7 |
| 1.2.1 | Cached · Upstream SHA-256 matched | Download | 8629d4cb…9de3dc |
| 1.3.0 | Cached · Upstream SHA-256 matched | Download | 0236f7f0…a15438 |
| 1.4.0 | Cached · Upstream SHA-256 matched | Download | 394035ff…d1641c |
| 1.5.0 | Cached · Upstream SHA-256 matched | Download | 086663d3…fae00b |
| 1.5.1 | Cached · Upstream SHA-256 matched | Download | 977f3916…91f75b |
| 2.0.0 | Cached · Upstream SHA-256 matched | Download | 0b0a7239…f467f4 |
| 2.0.1 | Cached · Upstream SHA-256 matched | Download | e314d1da…a97c4f |
| 2.0.2 | Cached · Upstream SHA-256 matched | Download | 31fc6262…16290c |
| 2.0.3 | Cached · Upstream SHA-256 matched | Download | 12a5cfd6…199157 |
| 2.1.0 | Cached · Upstream SHA-256 matched | Download | 330b4534…674a33 |
| 2.1.1 | Cached · Upstream SHA-256 matched | Download | 9fb477f1…8ff6b9 |
| 2.1.2 | Cached · Upstream SHA-256 matched | Download | 744218d0…9167e3 |
| 2.2.0 | Cached · Upstream SHA-256 matched | Download | 35995d69…dcfe7b |
| 2.2.1 | Cached · Upstream SHA-256 matched | Download | 2dbfe302…23d088 |
| 2.2.2 | Cached · Upstream SHA-256 matched | Download | 33166f5f…53ca39 |
| 2.3.0 | Cached · Upstream SHA-256 matched | Download | c132f057…8d9ab2 |
| 3.0.0 | Cached · Upstream SHA-256 matched | Download | 48d5294c…082144 |
| 3.1.0 | Cached · Upstream SHA-256 matched | Download | 200067f1…872db6 |
| 3.2.0 | Cached · Upstream SHA-256 matched | Download | 3c157eae…aba16d |
| 3.2.1 | Cached · Upstream SHA-256 matched | Download | 4c198769…eb5690 |
| 3.2.2 | Cached · Upstream SHA-256 matched | Download | 358bed36…505f0c |
| 3.2.3 | Cached · Upstream SHA-256 matched | Download | 4ea7527e…908178 |
| 3.2.4 | Cached · Upstream SHA-256 matched | Download | 8717e5f5…f655fd |
| 3.2.5 | Cached · Upstream SHA-256 matched | Download | 62aa4c18…96ec5c |
| 3.3.0 | Cached · Upstream SHA-256 matched | Download | f30cdc18…5eee4d |
| 3.4.0 | Cached · Upstream SHA-256 matched | Download | b198894a…a89ac9 |
| 3.4.1 | Cached · Upstream SHA-256 matched | Download | 47222df0…cecf87 |
| 3.5.0 | Cached · Upstream SHA-256 matched | Download | 8000c8f8…cc15f7 |
| 3.5.1 | Cached · Upstream SHA-256 matched | Download | 698f328e…e4bab5 |
| 3.5.2 | Cached · Upstream SHA-256 matched | Download | 0f9e7274…71bf71 |
| 4.0.0-nullsafety.0 | Cached · Upstream SHA-256 matched | Download | 65086760…4d8130 |
| 4.0.0 | Cached · Upstream SHA-256 matched | Download | bf5cd59a…50e4e3 |
| 4.0.1 | Cached · Upstream SHA-256 matched | Download | f9f62b88…b2677e |
| 4.0.2 | Cached · Upstream SHA-256 matched | Download | c29fcdae…9ab2bc |
| 4.0.3 | Cached · Upstream SHA-256 matched | Download | c17fe901…049696 |
| 4.1.0 | Cached · Upstream SHA-256 matched | Download | 04a71e61…0ec003 |
| 4.1.1 | Cached · Upstream SHA-256 matched | Download | 194d4ffc…746aa0 |
| 4.1.2 | Cached · Upstream SHA-256 matched | Download | e58e35c2…2b5dba |
| 4.1.3 | Cached · Upstream SHA-256 matched | Download | bd4c3eec…990338 |
| 4.1.4 | Cached · Upstream SHA-256 matched | Download | 86d3edf6…3bb323 |
| 5.0.0 | Cached · Upstream SHA-256 matched | Download | e55b5148…5ed5cd |
| 5.0.1 | Cached · Upstream SHA-256 matched | Download | e760d5cf…bf636c |
| 5.0.2 | Cached · Upstream SHA-256 matched | Download | 4af86580…04cc1c |
| 6.0.0 | Cached · Upstream SHA-256 matched | Download | 5a0f38bb…21beb8 |
| 6.0.1 | Cached · Upstream SHA-256 matched | Download | 45234e44…a4b23c |
| 6.1.0 | Cached · Upstream SHA-256 matched | Download | 64965139…038587 |
| 6.1.1 | Cached · Upstream SHA-256 matched | Download | 9baed298…ead1cb |
| 6.1.2 | Cached · Upstream SHA-256 matched | Download | c7119ff7…7c1a31 |
| 6.1.3 | Cached · Upstream SHA-256 matched | Download | 85f3e219…d57cfc |
| 6.1.4 | Cached · Upstream SHA-256 matched | Download | dc6f9551…1ee8ca |
| 6.1.5 | Cached · Upstream SHA-256 matched | Download | a53cf24d…a08610 |
| 6.1.6 | Cached · Upstream SHA-256 matched | Download | 3feab8c3…b10f39 |
| 6.2.0 | Cached · Upstream SHA-256 matched | Download | 4fc3fb22…aee75d |
| 6.3.0 | Cached · Upstream SHA-256 matched | Download | 40e713f3…a90f13 |
| 6.3.1 | Cached · Upstream SHA-256 matched | Download | 0cec7060…ee8238 |
| 6.3.2 | Cached · Upstream SHA-256 matched | Download | fd1bcfbf…116932 |
| 6.4.0 | Cached · Upstream SHA-256 matched | Download | 276b80dd…a54202 |
| 6.4.1 | Cached · Upstream SHA-256 matched | Download | 581006a3…7cfe4b |
| 6.5.0 | Cached · Upstream SHA-256 matched | Download | 45205106…757fe9 |
| 6.5.1 | Cached · Upstream SHA-256 matched | Download | 2296021b…f04bb4 |
| 6.5.2 | Cached · Upstream SHA-256 matched | Download | f8aec50d…c97f74 |
| 6.5.3 | Cached · Upstream SHA-256 matched | Download | 87ce738d…c2bfb0 |
| 6.5.4 | Cached · Upstream SHA-256 matched | Download | f3c2c18a…53ef5d |
| 6.6.0 | Cached · Upstream SHA-256 matched | Download | 040088d9…700a75 |
| 6.6.1 | Cached · Upstream SHA-256 matched | Download | dadc08bd…8d187a |
| 6.6.2 | Cached · Upstream SHA-256 matched | Download | 43793352…c04dc7 |
| 6.7.0 | Cached · Upstream SHA-256 matched | Download | 61a60716…2325c4 |
| 6.7.1 | Cached · Upstream SHA-256 matched | Download | aa1f5a89…b0b969 |
| 6.8.0 | Cached · Upstream SHA-256 matched | Download | ea1432d1…a1fd4b |
| 6.9.0 | Cached · Upstream SHA-256 matched | Download | c2fcb392…050d7c |
| 6.9.1 | Cached · Upstream SHA-256 matched | Download | e7855a40…7154b4 |
| 6.9.2 | Cached · Upstream SHA-256 matched | Download | 8f52361c…05d571 |
| 6.9.3 | Cached · Upstream SHA-256 matched | Download | b0a98230…dd59c9 |
| 6.9.4 | Cached · Upstream SHA-256 matched | Download | 81f04dee…c9e58a |
| 6.9.5 | Cached · Upstream SHA-256 matched | Download | c50ef5fc…feea8c |
| 6.10.0-dev | Cached · Upstream SHA-256 matched | Download | ab72b330…666393 |
| 6.10.0 | Cached · Upstream SHA-256 matched | Download | ce2cf974…55be27 |
| 6.11.0 | Cached · Upstream SHA-256 matched | Download | 3f2913b7…b80cbd |
| 6.11.1 | Cached · Upstream SHA-256 matched | Download | 33a04066…3aa6fe |
| 6.11.2 | Cached · Upstream SHA-256 matched | Download | c5b2ee75…fdffa3 |
| 6.11.3 | Cached · Upstream SHA-256 matched | Download | 6b253f78…e0b5b1 |
| 6.11.4 | Cached · Upstream SHA-256 matched | Download | 5b89c1e3…f8104e |
| 6.12.0 | Cached · Upstream SHA-256 matched | Download | 93fba3ad…25760d |
| 6.13.0 | Cached · Upstream SHA-256 matched | Download | 44729f5c…16a6c0 |
| 6.13.1 | Cached · Upstream SHA-256 matched | Download | fbcf404b…a78925 |
| 6.13.2 | Cached · Upstream SHA-256 matched | Download | 2c15e78e…7f20db |
| 6.14.0 | Cached · Upstream SHA-256 matched | Download | ffcd10cd…97e501 |
| 6.14.1 | Cached · Upstream SHA-256 matched | Download | e45aefa0…63113d |
| 6.15.0 | Cached · Upstream SHA-256 matched | Download | eb8d06bc…b541f8 |
| 6.15.1 | Cached · Upstream SHA-256 matched | Download | 3b177bc1…fd0f33 |
| 6.15.2 | Cached · Upstream SHA-256 matched | Download | cc782276…1c66f1 |
Dependencies
analyzer "\u003e=0.29.10 \u003c0.32.0"
build "\u003e=0.9.0 \u003c0.13.0"
build_config "^0.2.1"
cli_util "^0.1.0"
json_annotation "\u003e=0.2.2 \u003c0.2.3"
path "^1.3.2"
source_gen "^0.7.5"