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

add conversion of non-string keys for maps #25

Merged
merged 1 commit into from
Oct 9, 2021
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
21 changes: 18 additions & 3 deletions json_serializable_e2e_test/lib/src/model/ilist_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class IListWrapper {

IListWrapper(this.iList);

factory IListWrapper.fromJson(Map<String, dynamic> json) => _$IListWrapperFromJson(json);
factory IListWrapper.fromJson(Map<String, dynamic> json) =>
_$IListWrapperFromJson(json);

Map<String, dynamic> toJson() => _$IListWrapperToJson(this);
}
Expand All @@ -20,18 +21,32 @@ class IMapWrapper {

IMapWrapper(this.iMap);

factory IMapWrapper.fromJson(Map<String, dynamic> json) => _$IMapWrapperFromJson(json);
factory IMapWrapper.fromJson(Map<String, dynamic> json) =>
_$IMapWrapperFromJson(json);

Map<String, dynamic> toJson() => _$IMapWrapperToJson(this);
}

@JsonSerializable()
class IMapWrapper2 {
final IMap<int, String> iMap;

IMapWrapper2(this.iMap);

factory IMapWrapper2.fromJson(Map<String, dynamic> json) =>
_$IMapWrapper2FromJson(json);

Map<String, dynamic> toJson() => _$IMapWrapper2ToJson(this);
}

@JsonSerializable()
class ISetWrapper {
final ISet<String> iSet;

ISetWrapper(this.iSet);

factory ISetWrapper.fromJson(Map<String, dynamic> json) => _$ISetWrapperFromJson(json);
factory ISetWrapper.fromJson(Map<String, dynamic> json) =>
_$ISetWrapperFromJson(json);

Map<String, dynamic> toJson() => _$ISetWrapperToJson(this);
}
39 changes: 23 additions & 16 deletions json_serializable_e2e_test/lib/src/model/ilist_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions json_serializable_e2e_test/test/ilist_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:json_serializable_e2e_test/src/model/ilist_model.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -54,6 +56,13 @@ void main() {
'c': 'd',
}
});
expect(
jsonEncode(IMapWrapper2({
1: 'b',
2: 'd',
}.lock)
.toJson()),
'''{"iMap":{"1":"b","2":"d"}}''');
});

test('can deserialize IMap', () {
Expand All @@ -68,6 +77,14 @@ void main() {
'a': 'b',
'c': 'd',
}));
expect(
IMapWrapper2.fromJson(jsonDecode(
'''{"iMap":{"1":"b","2":"d"}}'''
)).iMap,
IMap({
1: 'b',
2: 'd',
}));
});
});
}
56 changes: 53 additions & 3 deletions lib/src/imap/imap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,13 @@ abstract class IMap<K, V> // ignore: must_be_immutable
K Function(Object?) fromJsonK,
V Function(Object?) fromJsonV,
) =>
json.map<K, V>((key, value) => MapEntry(fromJsonK(key), fromJsonV(value))).lock;
json.map<K, V>((key, value) => MapEntry(fromJsonK(_safeKeyFromJson<K>(key)), fromJsonV(value))).lock;

/// Converts to JSon. Json serialization support for json_serializable with @JsonSerializable.
Object toJson(Object? Function(K) toJsonK, Object? Function(V) toJsonV) =>
unlock.map((key, value) => MapEntry(toJsonK(key), toJsonV(value)));
Object toJson<NewK extends Object?>(NewK Function(K) toJsonK, Object? Function(V) toJsonV) =>
unlock.map((key, value) => MapEntry(_safeKeyToJson(toJsonK(key)), toJsonV(value)));


/// See also: [ImmutableCollection], [ImmutableCollection.lockConfig],
/// [ImmutableCollection.isConfigLocked],[flushFactor], [defaultConfig]
static void resetAllConfigurations() {
Expand Down Expand Up @@ -1382,3 +1383,52 @@ class InternalsForTestingPurposesIMap {
}

// /////////////////////////////////////////////////////////////////////////////

Object _safeKeyToJson<NewK extends Object?>(NewK key) {
if (key == null){
return '$key';
}
if (key is String) {
return key;
}
if (key is num || key is bool || key is DateTime || key is BigInt || key is Uri){
return '$key';
}
throw Exception('IMap key $key of type ${key.runtimeType} not serializable to/from json');
}


NewK _safeKeyFromJson<NewK extends Object?>(String key) {
if (key == 'null'){
return null as NewK;
}
if (_dummyBool is NewK){
return (key == 'true') as NewK;
}
if (_dummyDouble is NewK){
return double.parse(key) as NewK;
}
if (_dummyInt is NewK){
return int.parse(key) as NewK;
}
if (_dummyBigInt is NewK){
return BigInt.parse(key) as NewK;
}
if (_dummyDate is NewK){
return DateTime.parse(key) as NewK;
}
if (_dummyUri is NewK){
return Uri.parse(key) as NewK;
}
if (_dummyString is NewK){
return key as NewK;
}
return key as NewK;
}
const _dummyInt = 1;
const _dummyDouble = 1.0;
const _dummyString = '';
const _dummyBool = true;
final _dummyUri = Uri.parse('http://www.google.com');
final _dummyDate = DateTime.now();
final _dummyBigInt = BigInt.from(1);