Skip to content

json_annotation: put the wrapper helpers in their own file #153

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 12, 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
1 change: 1 addition & 0 deletions json_annotation/lib/json_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ library json_annotation;

export 'src/json_literal.dart';
export 'src/json_serializable.dart';
export 'src/wrapper_helpers.dart';
73 changes: 0 additions & 73 deletions json_annotation/lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// 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';

/// An annotation used to specify a class to generate code for.
class JsonSerializable {
// TODO(kevmoo): document these fields
Expand Down Expand Up @@ -106,74 +104,3 @@ class JsonKey {
this.fromJson,
this.toJson});
}

/// Helper classes used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
abstract class $JsonMapWrapper extends UnmodifiableMapBase<String, dynamic> {}

/// Helper function used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
Map<String, dynamic> $wrapMap<K, V>(
Map<K, V> source, dynamic converter(V key)) =>
new _MappingMap(source, converter);

/// Helper function used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
Map<String, dynamic> $wrapMapHandleNull<K, V>(
Map<K, V> source, dynamic converter(V key)) =>
source == null ? null : new _MappingMap(source, converter);

/// Helper function used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
List<dynamic> $wrapList<T>(List<T> source, dynamic converter(T key)) =>
new _MappingList(source, converter);

/// Helper function used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
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]);
}
76 changes: 76 additions & 0 deletions json_annotation/lib/src/wrapper_helpers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 'dart:collection';

/// Helper classes used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
abstract class $JsonMapWrapper extends UnmodifiableMapBase<String, dynamic> {}

/// Helper function used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
Map<String, dynamic> $wrapMap<K, V>(
Map<K, V> source, dynamic converter(V key)) =>
new _MappingMap(source, converter);

/// Helper function used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
Map<String, dynamic> $wrapMapHandleNull<K, V>(
Map<K, V> source, dynamic converter(V key)) =>
source == null ? null : new _MappingMap(source, converter);

/// Helper function used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
List<dynamic> $wrapList<T>(List<T> source, dynamic converter(T key)) =>
new _MappingList(source, converter);

/// Helper function used in generated code when
/// `JsonSerializableGenerator.useWrappers` is `true`.
///
/// Should not be used directly.
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.4
version: 0.2.5-dev
description: Annotations for the json_serializable package
homepage: https://github.com/dart-lang/json_serializable
author: Dart Team <misc@dartlang.org>
Expand Down