Skip to content

Commit e632181

Browse files
committed
fix: revert example that was being used for testing purposes
1 parent 7e54bad commit e632181

File tree

3 files changed

+44
-190
lines changed

3 files changed

+44
-190
lines changed

json_serializable/README.md

Lines changed: 22 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -31,52 +31,22 @@ import 'package:json_annotation/json_annotation.dart';
3131
part 'example.g.dart';
3232
3333
@JsonSerializable()
34-
class A {
35-
final StatusCode? statusCode;
36-
final StatusCode2 statusCode2;
37-
final StatusCode3 statusCode3;
34+
class Person {
35+
/// The generated code assumes these values exist in JSON.
36+
final String firstName, lastName;
3837
39-
A(this.statusCode, this.statusCode2, this.statusCode3);
38+
/// The generated code below handles if the corresponding JSON value doesn't
39+
/// exist or is empty.
40+
final DateTime? dateOfBirth;
4041
41-
factory A.fromJson(Map<String, dynamic> json) => _$AFromJson(json);
42+
Person({required this.firstName, required this.lastName, this.dateOfBirth});
4243
43-
Map<String, dynamic> toJson() => _$AToJson(this);
44-
}
45-
46-
enum StatusCode {
47-
@JsonValue(200, aliases: [201, 202])
48-
success,
49-
@JsonValue(301)
50-
movedPermanently,
51-
@JsonValue(302)
52-
found,
53-
@JsonValue(500)
54-
internalServerError,
55-
}
56-
57-
@JsonEnum(valueField: 'code')
58-
enum StatusCode2 {
59-
success(200),
60-
movedPermanently(301),
61-
found(302),
62-
internalServerError(500);
44+
/// Connect the generated [_$PersonFromJson] function to the `fromJson`
45+
/// factory.
46+
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
6347
64-
const StatusCode2(this.code);
65-
66-
final int code;
67-
}
68-
69-
@JsonEnum(valueField: 'code')
70-
enum StatusCode3 {
71-
success(200),
72-
movedPermanently(301),
73-
@JsonValue(1000)
74-
found(302),
75-
internalServerError(500);
76-
77-
const StatusCode3(this.code);
78-
79-
final int code;
48+
/// Connect the generated [_$PersonToJson] function to the `toJson` method.
49+
Map<String, dynamic> toJson() => _$PersonToJson(this);
8050
}
8151
```
8252

@@ -85,62 +55,19 @@ Building creates the corresponding part `example.g.dart`:
8555
```dart
8656
part of 'example.dart';
8757
88-
A _$AFromJson(Map<String, dynamic> json) => A(
89-
$enumDecodeNullableWithDecodeMap(
90-
_$StatusCodeEnumDecodeMap, json['statusCode']),
91-
$enumDecodeWithDecodeMap(_$StatusCode2EnumDecodeMap, json['statusCode2']),
92-
$enumDecodeWithDecodeMap(_$StatusCode3EnumDecodeMap, json['statusCode3']),
58+
Person _$PersonFromJson(Map<String, dynamic> json) => Person(
59+
firstName: json['firstName'] as String,
60+
lastName: json['lastName'] as String,
61+
dateOfBirth: json['dateOfBirth'] == null
62+
? null
63+
: DateTime.parse(json['dateOfBirth'] as String),
9364
);
9465
95-
Map<String, dynamic> _$AToJson(A instance) => <String, dynamic>{
96-
'statusCode': _$StatusCodeEnumMap[instance.statusCode],
97-
'statusCode2': _$StatusCode2EnumMap[instance.statusCode2]!,
98-
'statusCode3': _$StatusCode3EnumMap[instance.statusCode3]!,
66+
Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
67+
'firstName': instance.firstName,
68+
'lastName': instance.lastName,
69+
'dateOfBirth': instance.dateOfBirth?.toIso8601String(),
9970
};
100-
101-
const _$StatusCodeEnumMap = {
102-
StatusCode.success: 200,
103-
StatusCode.movedPermanently: 301,
104-
StatusCode.found: 302,
105-
StatusCode.internalServerError: 500,
106-
};
107-
108-
const _$StatusCodeEnumDecodeMap = {
109-
200: StatusCode.success,
110-
201: StatusCode.success,
111-
202: StatusCode.success,
112-
301: StatusCode.movedPermanently,
113-
302: StatusCode.found,
114-
500: StatusCode.internalServerError,
115-
};
116-
117-
const _$StatusCode2EnumMap = {
118-
StatusCode2.success: 200,
119-
StatusCode2.movedPermanently: 301,
120-
StatusCode2.found: 302,
121-
StatusCode2.internalServerError: 500,
122-
};
123-
124-
const _$StatusCode2EnumDecodeMap = {
125-
200: StatusCode2.success,
126-
301: StatusCode2.movedPermanently,
127-
302: StatusCode2.found,
128-
500: StatusCode2.internalServerError,
129-
};
130-
131-
const _$StatusCode3EnumMap = {
132-
StatusCode3.success: 200,
133-
StatusCode3.movedPermanently: 301,
134-
StatusCode3.found: 1000,
135-
StatusCode3.internalServerError: 500,
136-
};
137-
138-
const _$StatusCode3EnumDecodeMap = {
139-
200: StatusCode3.success,
140-
301: StatusCode3.movedPermanently,
141-
1000: StatusCode3.found,
142-
500: StatusCode3.internalServerError,
143-
};
14471
```
14572

14673
# Running the code generator

json_serializable/example/example.dart

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,20 @@ import 'package:json_annotation/json_annotation.dart';
77
part 'example.g.dart';
88

99
@JsonSerializable()
10-
class A {
11-
final StatusCode? statusCode;
12-
final StatusCode2 statusCode2;
13-
final StatusCode3 statusCode3;
10+
class Person {
11+
/// The generated code assumes these values exist in JSON.
12+
final String firstName, lastName;
1413

15-
A(this.statusCode, this.statusCode2, this.statusCode3);
14+
/// The generated code below handles if the corresponding JSON value doesn't
15+
/// exist or is empty.
16+
final DateTime? dateOfBirth;
1617

17-
factory A.fromJson(Map<String, dynamic> json) => _$AFromJson(json);
18+
Person({required this.firstName, required this.lastName, this.dateOfBirth});
1819

19-
Map<String, dynamic> toJson() => _$AToJson(this);
20-
}
21-
22-
enum StatusCode {
23-
@JsonValue(200, aliases: [201, 202])
24-
success,
25-
@JsonValue(301)
26-
movedPermanently,
27-
@JsonValue(302)
28-
found,
29-
@JsonValue(500)
30-
internalServerError,
31-
}
32-
33-
@JsonEnum(valueField: 'code')
34-
enum StatusCode2 {
35-
success(200),
36-
movedPermanently(301),
37-
found(302),
38-
internalServerError(500);
39-
40-
const StatusCode2(this.code);
41-
42-
final int code;
43-
}
44-
45-
@JsonEnum(valueField: 'code')
46-
enum StatusCode3 {
47-
success(200),
48-
movedPermanently(301),
49-
@JsonValue(1000)
50-
found(302),
51-
internalServerError(500);
52-
53-
const StatusCode3(this.code);
20+
/// Connect the generated [_$PersonFromJson] function to the `fromJson`
21+
/// factory.
22+
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
5423

55-
final int code;
24+
/// Connect the generated [_$PersonToJson] function to the `toJson` method.
25+
Map<String, dynamic> toJson() => _$PersonToJson(this);
5626
}

json_serializable/example/example.g.dart

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

0 commit comments

Comments
 (0)