Skip to content

Readme cleanup, prepare to release #128

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 4 commits into from
Apr 3, 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
55 changes: 13 additions & 42 deletions json_serializable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,18 @@ Given a library `example.dart` with an `Person` class annotated with
`@JsonSerializable()`:

```dart
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>[];
Person({this.firstName, this.lastName, this.dateOfBirth});

factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}
Expand All @@ -43,41 +31,24 @@ class Person extends Object with _$PersonSerializerMixin {
Building creates the corresponding part `example.g.dart`:

```dart
part of 'example.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
firstName: json['firstName'] as String,
lastName: json['lastName'] as String,
dateOfBirth: json['dateOfBirth'] == 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());
: DateTime.parse(json['dateOfBirth'] as String));

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;
}
Map<String, dynamic> toJson() => <String, dynamic>{
'firstName': firstName,
'lastName': lastName,
'dateOfBirth': dateOfBirth?.toIso8601String()
};
}
```

Expand Down
20 changes: 20 additions & 0 deletions json_serializable/example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: annotate_overrides, hash_and_equals
import 'package:json_annotation/json_annotation.dart';

part 'example.g.dart';

@JsonSerializable()
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);
}
29 changes: 29 additions & 0 deletions json_serializable/example/example.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'example.dart';

// **************************************************************************
// Generator: JsonSerializableGenerator
// **************************************************************************

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));

abstract class _$PersonSerializerMixin {
String get firstName;
String get lastName;
DateTime get dateOfBirth;
Map<String, dynamic> toJson() => <String, dynamic>{
'firstName': firstName,
'lastName': lastName,
'dateOfBirth': dateOfBirth?.toIso8601String()
};
}
6 changes: 1 addition & 5 deletions json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.5.0-dev
version: 0.5.0
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
Expand All @@ -22,7 +22,3 @@ dev_dependencies:
collection: ^1.14.0
dart_style: ^1.0.0
test: ^0.12.3

dependency_overrides:
json_annotation:
path: ../json_annotation
55 changes: 45 additions & 10 deletions json_serializable/test/ensure_build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,65 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// TODO(kevmoo): replace with a common utility
// https://github.com/dart-lang/build/issues/716
@Tags(const ['presubmit-only'])

import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as p;
import 'package:test/test.dart';

String _getExampleContent(String fileName) {
var lines = new File(p.join('example', fileName)).readAsLinesSync();

var lastHadContent = false;

// All lines with content, except those starting with `/`.
// Also exclude blank lines that follow other blank lines
var cleanedSource = lines.where((l) {
if (l.startsWith(r'/')) {
return false;
}

if (l.trim().isNotEmpty) {
lastHadContent = true;
return true;
}

if (lastHadContent) {
lastHadContent = false;
return true;
}

return false;
}).join('\n');

return '''
```dart
$cleanedSource
```''';
}

void main() {
String pkgRoot;
test('README example', () {
var readmeContent = new File('README.md').readAsStringSync();

var exampleContent = _getExampleContent('example.dart');
expect(readmeContent, contains(exampleContent));

var exampleGeneratedContent = _getExampleContent('example.g.dart');
expect(readmeContent, contains(exampleGeneratedContent));
});

setUpAll(() {
pkgRoot = _runProc('git', ['rev-parse', '--show-toplevel']);
// TODO(kevmoo): replace with a common utility
// https://github.com/dart-lang/build/issues/716
test('ensure local build succeeds with no changes', () {
var pkgRoot = _runProc('git', ['rev-parse', '--show-toplevel']);
var currentDir = Directory.current.resolveSymbolicLinksSync();

if (!p.equals(p.join(pkgRoot, 'json_serializable'), currentDir)) {
throw new StateError('Expected the git root ($pkgRoot) '
'to match the current directory ($currentDir).');
}
});

test('ensure local build succeeds with no changes', () {
// 1 - get a list of modified `.g.dart` files - should be empty
expect(_changedGeneratedFiles(), isEmpty);

Expand All @@ -37,7 +72,7 @@ void main() {

// 3 - get a list of modified `.g.dart` files - should still be empty
expect(_changedGeneratedFiles(), isEmpty);
});
}, tags: 'presubmit-only');
}

final _whitespace = new RegExp(r'\s');
Expand Down
1 change: 1 addition & 0 deletions json_serializable/tool/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final List<BuilderApplication> builders = [
applyToRoot(jsonPartBuilder(header: _copyrightHeader),
generateFor: const InputSet(
include: const [
'example/example.dart',
'test/test_files/json_literal.dart',
'test/test_files/json_test_example.dart',
'test/test_files/json_test_example.non_nullable.dart',
Expand Down