Skip to content

Pass field metadata to type helpers. #70

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

Closed
wants to merge 19 commits into from
Closed
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
7 changes: 7 additions & 0 deletions json_annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.2.2

* Added a helper class – `$JsonMapWrapper` – and helper functions – `$wrapMap`,
`$wrapMapHandleNull`, `$wrapList`, and `$wrapListHandleNull` – to support
the `useWrappers` option added to `JsonSerializableGenerator` in `v0.3.0` of
`package:json_serializable`.

## 0.2.1

* `JsonSerializable` class annotation
Expand Down
54 changes: 54 additions & 0 deletions json_annotation/lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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 'dart:collection';

class JsonSerializable {
// TODO(kevmoo): document these fields
final bool createFactory;
Expand Down Expand Up @@ -68,3 +70,55 @@ class JsonKey {
/// Only required when the default behavior is not desired.
const JsonKey({this.name, this.nullable, this.includeIfNull});
}

// TODO(kevmoo): Add documentation
abstract class $JsonMapWrapper extends UnmodifiableMapBase<String, dynamic> {}

Map<String, dynamic> $wrapMap<K, V>(
Map<K, V> source, dynamic converter(V key)) =>
new _MappingMap(source, converter);

Map<String, dynamic> $wrapMapHandleNull<K, V>(
Map<K, V> source, dynamic converter(V key)) =>
source == null ? null : new _MappingMap(source, converter);

List<dynamic> $wrapList<T>(List<T> source, dynamic converter(T key)) =>
new _MappingList(source, converter);

List<dynamic> $wrapListHandleNull<T>(
List<T> source, dynamic converter(T key)) =>
source == null ? null : new _MappingList(source, converter);

typedef dynamic _Convert<S>(S value);

class _MappingList<S> extends ListBase<dynamic> {
final List<S> _source;
final _Convert<S> _converter;

_MappingList(this._source, this._converter);

@override
dynamic operator [](int index) => _converter(_source[index]);

@override
operator []=(int index, dynamic value) => throw new UnsupportedError('');

@override
int get length => _source.length;

@override
set length(int value) => throw new UnsupportedError('');
}

class _MappingMap<K, V> extends UnmodifiableMapBase<String, dynamic> {
final Map<K, V> _source;
final _Convert<V> _converter;

_MappingMap(this._source, this._converter);

@override
Iterable<String> get keys => _source.keys.map((k) => k as String);

@override
dynamic operator [](Object key) => _converter(_source[key]);
}
2 changes: 1 addition & 1 deletion json_annotation/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_annotation
version: 0.2.1-dev
version: 0.2.2-dev
description: Annotations for the json_serializable package
homepage: https://github.com/dart-lang/json_serializable
author: Dart Team <misc@dartlang.org>
Expand Down
21 changes: 21 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
## 0.3.0

* **BREAKING** `UnsupportedTypeError` added a new required constructor argument:
`reason`.

* **BREAKING** The deprecated `annotations.dart` library has been removed.
Use `package:json_annotation` instead.

* **BREAKING** The arguments to `TypeHelper` `serialize` and `deserialize` have
changed.
* `SerializeContext` and `DeserializeContext` (new classes) are now passed
instead of the `TypeHelperGenerator` typedef (which has been deleted).

* `JsonSerializableGenerator` now supports an optional `useWrappers` argument
when generates and uses wrapper classes to (hopefully) improve the speed and
memory usage of serialization – at the cost of more code.

* Make `null` field handling smarter. If a field is classified as not
`nullable`, then use this knowledge when generating serialization code – even
if `includeIfNull` is `false`.

## 0.2.5

* Throw an exception if a duplicate JSON key is detected.
Expand Down
8 changes: 0 additions & 8 deletions json_serializable/lib/annotations.dart

This file was deleted.

Loading