/
json_serializable_example.dart
42 lines (32 loc) · 1.13 KB
/
json_serializable_example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// ignore_for_file: annotate_overrides
import 'package:example/encoder_example.dart';
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:theme_tailor_annotation/theme_tailor_annotation.dart';
part 'json_serializable_example.tailor.dart';
part 'json_serializable_example.g.dart';
class JsonColorConverter implements JsonConverter<Color, int> {
const JsonColorConverter();
@override
Color fromJson(int json) => Color(json);
@override
int toJson(Color color) => color.value;
}
@tailorMixin
@JsonSerializable(explicitToJson: true)
@CustomColorEncoder()
@JsonColorConverter()
class SerializableTheme extends ThemeExtension<SerializableTheme>
with _$SerializableThemeTailorMixin {
SerializableTheme({
required this.fooNumber,
this.barColor = Colors.black,
});
factory SerializableTheme.fromJson(Map<String, dynamic> json) =>
_$SerializableThemeFromJson(json);
@JsonKey(name: 'foo_number', defaultValue: 10)
final int fooNumber;
@JsonKey(name: 'bar_color')
final Color barColor;
Map<String, dynamic> toJson() => _$SerializableThemeToJson(this);
}