Line data Source code
1 : import 'package:freezed_annotation/freezed_annotation.dart';
2 :
3 : import '../../types.dart';
4 :
5 : typedef ReduxActionFromJson = ReduxAction Function(JsonMap json);
6 :
7 : abstract class ReduxAction {
8 : JsonMap toJson();
9 : String get typeName;
10 : }
11 :
12 0 : final _fromJsonMap = <String, ReduxActionFromJson>{};
13 :
14 : class ReduxActionConverter implements JsonConverter<ReduxAction?, JsonMap> {
15 0 : const ReduxActionConverter();
16 :
17 0 : @override
18 : ReduxAction fromJson(JsonMap json) {
19 0 : final fromJson = _fromJsonMap[json['type']];
20 : if (fromJson == null) {
21 0 : throw Exception('No entry for \'type\' ${json['type']}');
22 : }
23 0 : return fromJson(json);
24 : }
25 :
26 0 : @override
27 : JsonMap toJson(ReduxAction? data) {
28 0 : final json = data?.toJson();
29 0 : json?['type'] = data?.typeName;
30 0 : return json ?? {};
31 : }
32 : }
|