Skip to content

Add default support for Uri conversion #218

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
Jun 4, 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
4 changes: 4 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.7

* Added support for `Uri` conversion.

## 0.5.6

* Added support for `JsonSerializable.disallowUnrecognizedKeys`.
Expand Down
9 changes: 6 additions & 3 deletions json_serializable/lib/src/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'type_helpers/enum_helper.dart';
import 'type_helpers/iterable_helper.dart';
import 'type_helpers/json_helper.dart';
import 'type_helpers/map_helper.dart';
import 'type_helpers/uri_helper.dart';
import 'type_helpers/value_helper.dart';

Iterable<TypeHelper> allHelpersImpl(JsonSerializableGenerator generator) =>
Expand All @@ -34,6 +35,7 @@ class JsonSerializableGenerator
static const _defaultHelpers = const [
const JsonHelper(),
const DateTimeHelper(),
const UriHelper()
];

final List<TypeHelper> _typeHelpers;
Expand Down Expand Up @@ -68,8 +70,8 @@ class JsonSerializableGenerator

/// Creates an instance of [JsonSerializableGenerator].
///
/// If [typeHelpers] is not provided, two built-in helpers are used:
/// [JsonHelper] and [DateTimeHelper].
/// If [typeHelpers] is not provided, three built-in helpers are used:
/// [JsonHelper], [DateTimeHelper], and [UriHelper].
const JsonSerializableGenerator({
List<TypeHelper> typeHelpers,
bool useWrappers: false,
Expand All @@ -83,7 +85,8 @@ class JsonSerializableGenerator
/// Creates an instance of [JsonSerializableGenerator].
///
/// [typeHelpers] provides a set of [TypeHelper] that will be used along with
/// the built-in helpers: [JsonHelper] and [DateTimeHelper].
/// the built-in helpers:
/// [JsonHelper], [DateTimeHelper], and [UriHelper].
factory JsonSerializableGenerator.withDefaultHelpers(
Iterable<TypeHelper> typeHelpers,
{bool useWrappers: false,
Expand Down
44 changes: 44 additions & 0 deletions json_serializable/lib/src/type_helpers/uri_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2018, 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.

import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart' show TypeChecker;
import '../type_helper.dart';
import '../utils.dart';

class UriHelper extends TypeHelper {
const UriHelper();

@override
String serialize(
DartType targetType, String expression, SerializeContext context) {
if (!_matchesType(targetType)) {
return null;
}

var buffer = new StringBuffer(expression);

if (context.nullable) {
buffer.write('?');
}

buffer.write('.toString()');

return buffer.toString();
}

@override
String deserialize(
DartType targetType, String expression, DeserializeContext context) {
if (!_matchesType(targetType)) {
return null;
}

return commonNullPrefix(
context.nullable, expression, 'Uri.parse($expression as String)');
}
}

bool _matchesType(DartType type) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe _isUri or _isUriType?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I left it this way to mirror other files. PRs welcome. 😄

const TypeChecker.fromUrl('dart:core#Uri').isExactlyType(type);
1 change: 1 addition & 0 deletions json_serializable/lib/type_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export 'src/shared_checkers.dart' show simpleJsonTypeChecker, typeArgumentsOf;
export 'src/type_helper.dart';
export 'src/type_helpers/date_time_helper.dart';
export 'src/type_helpers/json_helper.dart';
export 'src/type_helpers/uri_helper.dart';
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.5.6
version: 0.5.7-dev
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 Down
13 changes: 13 additions & 0 deletions json_serializable/test/json_serializable_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ void main() {

roundTripOrder(order);
});

test('homepage', () {
var order = new Order(Category.charmed)
..platform = Platform.undefined
..altPlatforms = {
'u': Platform.undefined,
'f': Platform.foo,
'null': null
}
..homepage = Uri.parse('https://dartlang.org');

roundTripOrder(order);
});
});

group('Item', () {
Expand Down
2 changes: 2 additions & 0 deletions json_serializable/test/test_files/json_test_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Order extends Object with _$OrderSerializerMixin {
Platform platform;
Map<String, Platform> altPlatforms;

Uri homepage;

@JsonKey(ignore: true)
String get platformValue => platform?.description;

Expand Down
8 changes: 6 additions & 2 deletions json_serializable/test/test_files/json_test_example.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ Order _$OrderFromJson(Map<String, dynamic> json) {
: new Platform.fromJson(json['platform'] as String)
..altPlatforms = (json['altPlatforms'] as Map<String, dynamic>)?.map((k,
e) =>
new MapEntry(k, e == null ? null : new Platform.fromJson(e as String)));
new MapEntry(k, e == null ? null : new Platform.fromJson(e as String)))
..homepage =
json['homepage'] == null ? null : Uri.parse(json['homepage'] as String);
}

abstract class _$OrderSerializerMixin {
Expand All @@ -67,13 +69,15 @@ abstract class _$OrderSerializerMixin {
UnmodifiableListView<Item> get items;
Platform get platform;
Map<String, Platform> get altPlatforms;
Uri get homepage;
Map<String, dynamic> toJson() => <String, dynamic>{
'count': count,
'isRushed': isRushed,
'category': category.toString().split('.').last,
'items': items,
'platform': platform,
'altPlatforms': altPlatforms
'altPlatforms': altPlatforms,
'homepage': homepage?.toString()
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Order extends Object with _$OrderSerializerMixin {
Platform platform;
Map<String, Platform> altPlatforms;

Uri homepage;

@JsonKey(ignore: true)
String get platformValue => platform?.description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Order _$OrderFromJson(Map<String, dynamic> json) {
..isRushed = json['isRushed'] as bool
..platform = new Platform.fromJson(json['platform'] as String)
..altPlatforms = (json['altPlatforms'] as Map<String, dynamic>)
.map((k, e) => new MapEntry(k, new Platform.fromJson(e as String)));
.map((k, e) => new MapEntry(k, new Platform.fromJson(e as String)))
..homepage = Uri.parse(json['homepage'] as String);
}

abstract class _$OrderSerializerMixin {
Expand All @@ -59,13 +60,15 @@ abstract class _$OrderSerializerMixin {
UnmodifiableListView<Item> get items;
Platform get platform;
Map<String, Platform> get altPlatforms;
Uri get homepage;
Map<String, dynamic> toJson() => <String, dynamic>{
'count': count,
'isRushed': isRushed,
'category': category.toString().split('.').last,
'items': items,
'platform': platform,
'altPlatforms': altPlatforms
'altPlatforms': altPlatforms,
'homepage': homepage.toString()
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Order extends Object with _$OrderSerializerMixin {
Platform platform;
Map<String, Platform> altPlatforms;

Uri homepage;

@JsonKey(ignore: true)
String get platformValue => platform?.description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ Order _$OrderFromJson(Map<String, dynamic> json) {
..isRushed = json['isRushed'] as bool
..platform = new Platform.fromJson(json['platform'] as String)
..altPlatforms = (json['altPlatforms'] as Map<String, dynamic>)
.map((k, e) => new MapEntry(k, new Platform.fromJson(e as String)));
.map((k, e) => new MapEntry(k, new Platform.fromJson(e as String)))
..homepage = Uri.parse(json['homepage'] as String);
}

abstract class _$OrderSerializerMixin {
Expand All @@ -90,6 +91,7 @@ abstract class _$OrderSerializerMixin {
UnmodifiableListView<Item> get items;
Platform get platform;
Map<String, Platform> get altPlatforms;
Uri get homepage;
Map<String, dynamic> toJson() => new _$OrderJsonMapWrapper(this);
}

Expand All @@ -104,7 +106,8 @@ class _$OrderJsonMapWrapper extends $JsonMapWrapper {
'category',
'items',
'platform',
'altPlatforms'
'altPlatforms',
'homepage'
];

@override
Expand All @@ -123,6 +126,8 @@ class _$OrderJsonMapWrapper extends $JsonMapWrapper {
return _v.platform;
case 'altPlatforms':
return _v.altPlatforms;
case 'homepage':
return _v.homepage.toString();
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Order extends Object with _$OrderSerializerMixin {
Platform platform;
Map<String, Platform> altPlatforms;

Uri homepage;

@JsonKey(ignore: true)
String get platformValue => platform?.description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ Order _$OrderFromJson(Map<String, dynamic> json) {
: new Platform.fromJson(json['platform'] as String)
..altPlatforms = (json['altPlatforms'] as Map<String, dynamic>)?.map((k,
e) =>
new MapEntry(k, e == null ? null : new Platform.fromJson(e as String)));
new MapEntry(k, e == null ? null : new Platform.fromJson(e as String)))
..homepage =
json['homepage'] == null ? null : Uri.parse(json['homepage'] as String);
}

abstract class _$OrderSerializerMixin {
Expand All @@ -98,6 +100,7 @@ abstract class _$OrderSerializerMixin {
UnmodifiableListView<Item> get items;
Platform get platform;
Map<String, Platform> get altPlatforms;
Uri get homepage;
Map<String, dynamic> toJson() => new _$OrderJsonMapWrapper(this);
}

Expand All @@ -112,7 +115,8 @@ class _$OrderJsonMapWrapper extends $JsonMapWrapper {
'category',
'items',
'platform',
'altPlatforms'
'altPlatforms',
'homepage'
];

@override
Expand All @@ -131,6 +135,8 @@ class _$OrderJsonMapWrapper extends $JsonMapWrapper {
return _v.platform;
case 'altPlatforms':
return _v.altPlatforms;
case 'homepage':
return _v.homepage?.toString();
}
}
return null;
Expand Down