Skip to content
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

json_serializable: fix readme spelling, DRY up some type logic #1212

Merged
merged 1 commit into from Sep 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions json_serializable/CHANGELOG.md
@@ -1,3 +1,5 @@
## 6.4.2-dev

## 6.4.1

- Fixed a bug when an `@JsonSerializable` class uses a mixin with fields.
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/README.md
Expand Up @@ -167,7 +167,7 @@ For [`Map`], the key value must be one of
If you want to use types that are not supported out-of-the-box or if you want to
customize the encoding/decoding of any type, you have a few options.

1. If you own/cotrol the desired type, add a `fromJson` constructor and/or a
1. If you own/control the desired type, add a `fromJson` constructor and/or a
`toJson()` function to the type. Note: while you can use `json_serializable`
for these types, you don't have to! The generator code only looks for these
methods. It doesn't care how they were created.
Expand Down
7 changes: 4 additions & 3 deletions json_serializable/lib/src/type_helpers/map_helper.dart
Expand Up @@ -167,20 +167,21 @@ void _checkSafeKeyType(String expression, DartType keyArg) {
throw UnsupportedTypeError(
keyArg,
expression,
'Map keys must be one of: ${_allowedTypeNames.join(', ')}.',
'Map keys must be one of: ${allowedMapKeyTypes.join(', ')}.',
);
}

/// The names of types that can be used as [Map] keys.
///
/// Used in [_checkSafeKeyType] to provide a helpful error with unsupported
/// types.
Iterable<String> get _allowedTypeNames => const [
List<String> get allowedMapKeyTypes => [
'Object',
'dynamic',
'enum',
'String',
].followedBy(_instances.map((i) => i.coreTypeName));
..._instances.map((i) => i.coreTypeName)
];

extension on DartType {
bool get isSimpleJsonTypeNotDouble =>
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/lib/type_helper.dart
Expand Up @@ -14,7 +14,7 @@ export 'src/type_helpers/enum_helper.dart';
export 'src/type_helpers/iterable_helper.dart';
export 'src/type_helpers/json_converter_helper.dart';
export 'src/type_helpers/json_helper.dart';
export 'src/type_helpers/map_helper.dart';
export 'src/type_helpers/map_helper.dart' show MapHelper;
export 'src/type_helpers/uri_helper.dart';
export 'src/type_helpers/value_helper.dart';
export 'src/unsupported_type_error.dart';
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
@@ -1,5 +1,5 @@
name: json_serializable
version: 6.4.1
version: 6.4.2-dev
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/tool/readme/readme_template.md
Expand Up @@ -103,7 +103,7 @@ For `core:Map`, the key value must be one of
If you want to use types that are not supported out-of-the-box or if you want to
customize the encoding/decoding of any type, you have a few options.

1. If you own/cotrol the desired type, add a `fromJson` constructor and/or a
1. If you own/control the desired type, add a `fromJson` constructor and/or a
`toJson()` function to the type. Note: while you can use `json_serializable`
for these types, you don't have to! The generator code only looks for these
methods. It doesn't care how they were created.
Expand Down
14 changes: 4 additions & 10 deletions json_serializable/tool/test_type_builder.dart
Expand Up @@ -7,6 +7,7 @@ import 'dart:async';
import 'package:build/build.dart';
import 'package:collection/collection.dart';
import 'package:dart_style/dart_style.dart';
import 'package:json_serializable/src/type_helpers/map_helper.dart';

import 'shared.dart';
import 'test_type_data.dart';
Expand Down Expand Up @@ -96,16 +97,9 @@ final _typesToTest = {
..._collectionTypes,
};

const mapKeyTypes = {
'BigInt',
'DateTime',
'dynamic',
customEnumType,
'int',
'Object',
'String',
'Uri',
};
Iterable<String> get mapKeyTypes =>
allowedMapKeyTypes.map((e) => e == 'enum' ? customEnumType : e).toList()
..sort(compareAsciiLowerCase);

final _iterableGenericArgs = ([
..._trivialTypesToTest.keys,
Expand Down