-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathjson_test_common.dart
100 lines (74 loc) · 2.14 KB
/
json_test_common.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:collection';
import 'package:json_annotation/json_annotation.dart';
@JsonEnum(fieldRename: FieldRename.kebab, caseInsensitive: true)
enum Category {
top,
bottom,
strange,
charmed,
up,
down,
// NOTE: this should override the kebab bits below!
@JsonValue('not_discovered_yet')
notDiscoveredYet
}
enum Colors { red, green, yellow, blue }
enum Direction { up, down, left, right }
enum StatusCode {
@JsonValue(200)
success,
@JsonValue(404)
notFound,
// Intentionally using a non-int value to validate heterogeneous
// type-inference.
@JsonValue('500')
weird,
unknown,
}
Duration? durationFromInt(int? ms) =>
ms == null ? null : Duration(milliseconds: ms);
int? durationToInt(Duration? duration) => duration?.inMilliseconds;
DateTime? dateTimeFromEpochUs(int? us) =>
us == null ? null : DateTime.fromMicrosecondsSinceEpoch(us);
int? dateTimeToEpochUs(DateTime? dateTime) => dateTime?.microsecondsSinceEpoch;
class Platform {
final String description;
static const Platform foo = Platform._('foo');
static const Platform undefined = Platform._('undefined');
const Platform._(this.description);
factory Platform.fromJson(String value) {
switch (value) {
case 'foo':
return foo;
case 'undefined':
return undefined;
default:
throw ArgumentError.value(value, 'value', 'Not a supported value.');
}
}
String toJson() => description;
}
abstract class ItemCore {
final int? price;
ItemCore(this.price);
}
class MyList<T> extends ListBase<T> {
final List<T> _data;
MyList(Iterable<T> source) : _data = source.toList();
factory MyList.fromJson(List<T> items) => MyList(items);
@override
int get length => _data.length;
@override
set length(int value) {
_data.length = value;
}
@override
T operator [](int index) => _data[index];
@override
void operator []=(int index, T value) {
_data[index] = value;
}
}