Skip to content

tighten up example code in readme #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions json_serializable/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[![Build Status](https://travis-ci.org/dart-lang/json_serializable.svg?branch=master)](https://travis-ci.org/dart-lang/json_serializable)

Provides [source_gen] `Generator`s to create code for JSON serialization and
deserialization.
deserialization using the [Dart Build System].

See the [example] to understand how to configure your project for the latest
released version of `json_serializable`.

## User defined and generated code
## Example

Given a library `example.dart` with an `Person` class annotated with
`@JsonSerializable()`:
Expand All @@ -16,15 +16,12 @@ import 'package:json_annotation/json_annotation.dart';

part 'example.g.dart';

@JsonSerializable()
@JsonSerializable(nullable: false)
class Person extends Object with _$PersonSerializerMixin {
final String firstName;
final String lastName;

final DateTime dateOfBirth;

Person({this.firstName, this.lastName, this.dateOfBirth});

factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}
```
Expand All @@ -37,9 +34,7 @@ part of 'example.dart';
Person _$PersonFromJson(Map<String, dynamic> json) => new Person(
firstName: json['firstName'] as String,
lastName: json['lastName'] as String,
dateOfBirth: json['dateOfBirth'] == null
? null
: DateTime.parse(json['dateOfBirth'] as String));
dateOfBirth: DateTime.parse(json['dateOfBirth'] as String));

abstract class _$PersonSerializerMixin {
String get firstName;
Expand All @@ -48,10 +43,11 @@ abstract class _$PersonSerializerMixin {
Map<String, dynamic> toJson() => <String, dynamic>{
'firstName': firstName,
'lastName': lastName,
'dateOfBirth': dateOfBirth?.toIso8601String()
'dateOfBirth': dateOfBirth.toIso8601String()
};
}
```

[example]: https://github.com/dart-lang/json_serializable/blob/master/example
[source_gen]: https://pub.dartlang.org/packages/source_gen
[Dart Build System]: https://github.com/dart-lang/build
5 changes: 1 addition & 4 deletions json_serializable/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import 'package:json_annotation/json_annotation.dart';

part 'example.g.dart';

@JsonSerializable()
@JsonSerializable(nullable: false)
class Person extends Object with _$PersonSerializerMixin {
final String firstName;
final String lastName;

final DateTime dateOfBirth;

Person({this.firstName, this.lastName, this.dateOfBirth});

factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}
6 changes: 2 additions & 4 deletions json_serializable/example/example.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ part of 'example.dart';
Person _$PersonFromJson(Map<String, dynamic> json) => new Person(
firstName: json['firstName'] as String,
lastName: json['lastName'] as String,
dateOfBirth: json['dateOfBirth'] == null
? null
: DateTime.parse(json['dateOfBirth'] as String));
dateOfBirth: DateTime.parse(json['dateOfBirth'] as String));

abstract class _$PersonSerializerMixin {
String get firstName;
Expand All @@ -24,6 +22,6 @@ abstract class _$PersonSerializerMixin {
Map<String, dynamic> toJson() => <String, dynamic>{
'firstName': firstName,
'lastName': lastName,
'dateOfBirth': dateOfBirth?.toIso8601String()
'dateOfBirth': dateOfBirth.toIso8601String()
};
}