Skip to content

Commit 038bbd1

Browse files
committed
Refactor json converters.
1 parent c287abe commit 038bbd1

File tree

11 files changed

+205
-105
lines changed

11 files changed

+205
-105
lines changed

lib/codelessly_api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export 'src/api/nodes/nodes.dart';
1616
export 'src/api/regexes.dart';
1717
export 'src/api/typed_value.dart';
1818
export 'src/api/utils.dart';
19+
export 'src/api/converters.dart';

lib/src/api/converters.dart

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
import 'node_json_converter.dart';
4+
import 'nodes/base_node.dart';
5+
6+
/// Top level converter for serializing [DateTime] to [millisecondsSinceEpoch].
7+
class DateTimeConverter extends JsonConverter<DateTime, int?> {
8+
/// Creates a new instance of [DateTimeConverter].
9+
const DateTimeConverter();
10+
11+
@override
12+
DateTime fromJson(int? json) => deserialize(json);
13+
14+
@override
15+
int? toJson(DateTime object) => serialize(object);
16+
17+
/// Serializes [DateTime] to [int].
18+
static int? serialize(DateTime object) =>
19+
object.toUtc().millisecondsSinceEpoch;
20+
21+
/// Deserializes [int] to [DateTime].
22+
static DateTime deserialize(int? json) {
23+
return json != null
24+
? DateTime.fromMillisecondsSinceEpoch(json).toLocal()
25+
: DateTime.now();
26+
}
27+
}
28+
29+
/// Top level converter for serializing [DateTime] to [millisecondsSinceEpoch].
30+
class NullableDateTimeConverter extends JsonConverter<DateTime?, int?> {
31+
/// Creates a new instance of [NullableDateTimeConverter].
32+
const NullableDateTimeConverter();
33+
34+
@override
35+
DateTime? fromJson(int? json) => deserialize(json);
36+
37+
@override
38+
int? toJson(DateTime? object) => serialize(object);
39+
40+
/// Serializes [DateTime] to [int].
41+
static int? serialize(DateTime? object) =>
42+
object?.toUtc().millisecondsSinceEpoch;
43+
44+
/// Deserializes [int] to [DateTime].
45+
static DateTime? deserialize(int? json) {
46+
return json != null
47+
? DateTime.fromMillisecondsSinceEpoch(json).toLocal()
48+
: null;
49+
}
50+
}
51+
52+
/// Top level converter for serializing Nodes map to and from JSON.
53+
class NodesMapConverter
54+
extends JsonConverter<Map<String, BaseNode>, Map<String, dynamic>> {
55+
/// Creates a new instance of [NodesMapConverter].
56+
const NodesMapConverter();
57+
58+
@override
59+
Map<String, BaseNode> fromJson(Map<String, dynamic> json) =>
60+
deserialize(json);
61+
62+
@override
63+
Map<String, dynamic> toJson(Map<String, BaseNode> object) =>
64+
serialize(object);
65+
66+
/// Top level function to deserialize a JSON Map into a map of node ID to node.
67+
static Map<String, BaseNode> deserialize(Map<String, dynamic> value) => {
68+
for (final MapEntry(key: key, value: value) in value.entries)
69+
key: NodeJsonConverter.instance.fromJson(value)!,
70+
};
71+
72+
/// Top level function to serialize a map of node ID to node into a JSON map.
73+
static Map<String, dynamic> serialize(Map<String, BaseNode> nodes) => {
74+
for (final MapEntry(key: key, value: value) in nodes.entries)
75+
key: NodeJsonConverter.instance.toJson(value),
76+
};
77+
}
78+
79+
/// Top level converter for serializing map of DateTime.
80+
class DateTimeMapConverter
81+
extends JsonConverter<Map<String, DateTime>, Map<String, int>> {
82+
/// Creates a new instance of [DateTimeMapConverter].
83+
const DateTimeMapConverter();
84+
85+
@override
86+
Map<String, DateTime> fromJson(Map<String, int> json) => deserialize(json);
87+
88+
@override
89+
Map<String, int> toJson(Map<String, DateTime> object) => serialize(object);
90+
91+
/// Top level function for deserializing the millis from JSON as the values of
92+
/// a map to [DateTime].
93+
static Map<String, DateTime> deserialize(Map<String, dynamic> value) => {
94+
for (final MapEntry(key: key, value: value) in value.entries)
95+
key: DateTime.fromMillisecondsSinceEpoch(value).toLocal(),
96+
};
97+
98+
/// Top level function for serializing a map of with [DateTime] as the values
99+
/// to [millisecondsSinceEpoch].
100+
static Map<String, int> serialize(Map<String, DateTime> dates) => {
101+
for (final MapEntry(key: key, value: value) in dates.entries)
102+
key: value.toUtc().millisecondsSinceEpoch,
103+
};
104+
}

lib/src/api/custom_component.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import 'package:equatable/equatable.dart';
66
import 'package:json_annotation/json_annotation.dart';
77

8+
import 'converters.dart';
89
import 'mixins.dart';
910
import 'models/models.dart';
10-
import 'utils.dart';
1111

1212
part 'custom_component.g.dart';
1313

@@ -25,7 +25,7 @@ class CustomComponent with EquatableMixin, SerializableMixin {
2525
final ComponentData data;
2626

2727
/// Date and time when the component was created.
28-
@JsonKey(fromJson: jsonToDate, toJson: dateToJson)
28+
@DateTimeConverter()
2929
final DateTime createdAt;
3030

3131
/// The thumbnail URL for the component preview.

lib/src/api/custom_component.g.dart

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/models/condition.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'package:equatable/equatable.dart';
22
import 'package:json_annotation/json_annotation.dart';
33

4+
import '../converters.dart';
45
import '../mixins.dart';
5-
import '../utils.dart';
66
import 'models.dart';
77

88
part 'condition.g.dart';
@@ -334,7 +334,7 @@ sealed class BaseCondition with EquatableMixin, SerializableMixin {
334334
final String id;
335335

336336
/// last updated timestamp
337-
@JsonKey(fromJson: jsonToDate, toJson: dateToJson)
337+
@DateTimeConverter()
338338
final DateTime lastUpdated;
339339

340340
/// Creates a base condition.
@@ -538,7 +538,7 @@ class CanvasConditions with EquatableMixin {
538538
final Map<String, BaseCondition> conditions;
539539

540540
/// Last updated time of this canvas.
541-
@JsonKey(toJson: dateToJson, fromJson: jsonToDate)
541+
@DateTimeConverter()
542542
final DateTime lastUpdated;
543543

544544
/// ID of the project this canvas belongs to.

lib/src/api/models/condition.g.dart

Lines changed: 65 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/models/variables_model.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class CanvasVariables with EquatableMixin {
158158
final Map<String, CanvasVariableData> variables;
159159

160160
/// Last updated time of this canvas.
161-
@JsonKey(toJson: dateToJson, fromJson: jsonToDate)
161+
@DateTimeConverter()
162162
final DateTime lastUpdated;
163163

164164
/// ID of the project this canvas belongs to.

lib/src/api/models/variables_model.g.dart

Lines changed: 20 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/nodes/canvas_node.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import 'package:equatable/equatable.dart';
66
import 'package:json_annotation/json_annotation.dart';
77

8+
import '../converters.dart';
89
import '../mixins.dart';
910
import '../models/models.dart';
10-
import '../utils.dart';
1111
import 'nodes.dart';
1212

1313
part 'canvas_node.g.dart';
@@ -28,7 +28,7 @@ class CanvasNode extends ParentNode
2828
final bool supportsPadding = true;
2929

3030
/// Time of creation.
31-
@JsonKey(fromJson: jsonToDate, toJson: dateToJson)
31+
@DateTimeConverter()
3232
late DateTime createdTimestamp;
3333

3434
/// Holds configurable properties for the canvas.

0 commit comments

Comments
 (0)