Skip to content

Commit 4e49b62

Browse files
authored
annotation: Update library doc comment describing the $ helper usage (#173)
* finish documenting the json_serializable annotations * more docs for json_literal
1 parent 499a7e6 commit 4e49b62

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

json_annotation/lib/json_annotation.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
/// [json_serializable](https://pub.dartlang.org/packages/json_serializable).
77
///
88
/// Also contains helper functions and classes – prefixed with `$` used by
9-
/// `json_serializable` when generating wrappers.
9+
/// `json_serializable` when the `use_wrappers` or `checked` options are
10+
/// enabled.
1011
library json_annotation;
1112

1213
export 'src/checked_helpers.dart';

json_annotation/lib/src/json_literal.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
/// JSON file.
77
///
88
/// The annotation can be applied to any member, but usually it's applied to
9-
/// getter.
9+
/// top-level getter.
10+
///
11+
/// In this example, the JSON content of `data.json` is populated into a
12+
/// top-level, final field `_$glossaryDataJsonLiteral` in the generated file.
13+
///
14+
/// ```dart
15+
/// @JsonLiteral('data.json')
16+
/// Map get glossaryData => _$glossaryDataJsonLiteral;
17+
/// ```
1018
class JsonLiteral {
1119
/// The relative path from the Dart file with the annotation to the file
1220
/// containing the source JSON.
@@ -15,6 +23,7 @@ class JsonLiteral {
1523
/// `true` if the JSON literal should be written as a constant.
1624
final bool asConst;
1725

26+
/// Creates a new [JsonLiteral] instance.
1827
const JsonLiteral(this.path, {bool asConst: false})
1928
: this.asConst = asConst ?? false;
2029
}

json_annotation/lib/src/json_serializable.dart

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,32 @@
44

55
/// An annotation used to specify a class to generate code for.
66
class JsonSerializable {
7-
// TODO(kevmoo): document these fields
7+
/// If `true` (the default), a private, static `_$ExampleFromJson` method
8+
/// is created in the generated part file.
9+
///
10+
/// Call this method from a factory constructor added to the source class:
11+
///
12+
/// ```dart
13+
/// @JsonSerializable()
14+
/// class Example {
15+
/// // ...
16+
/// factory Example.fromJson(Map<String, dynamic> json) =>
17+
/// _$ExampleFromJson(json);
18+
/// }
19+
/// ```
820
final bool createFactory;
21+
22+
/// If `true` (the default), a private `_$ClassNameMixin` class is created
23+
/// in the generated part file which contains a `toJson` method.
24+
///
25+
/// Mix in this class to the source class:
26+
///
27+
/// ```dart
28+
/// @JsonSerializable()
29+
/// class Example extends Object with _$ExampleMixin {
30+
/// // ...
31+
/// }
32+
/// ```
933
final bool createToJson;
1034

1135
/// Whether the generator should include fields with `null` values in the
@@ -22,7 +46,7 @@ class JsonSerializable {
2246
/// `null` runtime validation if it's critical.
2347
final bool nullable;
2448

25-
// TODO(kevmoo): document the constructor
49+
/// Creates a new [JsonSerializable] instance.
2650
const JsonSerializable(
2751
{bool createFactory: true,
2852
bool createToJson: true,
@@ -93,7 +117,7 @@ class JsonKey {
93117
/// Values returned by [toJson] should "round-trip" through [fromJson].
94118
final Function toJson;
95119

96-
/// Creates a new [JsonKey].
120+
/// Creates a new [JsonKey] instance.
97121
///
98122
/// Only required when the default behavior is not desired.
99123
const JsonKey(

0 commit comments

Comments
 (0)