Skip to content

Commit b73f214

Browse files
committed
Add documentation for GitHubJson
1 parent 3befab8 commit b73f214

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

lib/src/json.dart

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
import 'dart:convert';
22

33
import 'package:github/src/common/util/utils.dart';
4-
import 'package:github/src/util.dart';
54

5+
/// Internal class for Json encoding
6+
/// that should be used instead of `dart:convert`.
7+
///
8+
/// It contains methods that ensures that converted Json
9+
/// will work with the GitHub API.
610
class GitHubJson {
7-
GitHubJson._();
11+
const GitHubJson._();
812

13+
/// Called only if an object is of a non primitive type.
14+
///
15+
/// If [object] is a [DateTime], it converts it to a String whose format is compatible with the API.
16+
/// Else, it uses the default behavior of [JsonEncoder] which is to call `toJson` method onto [object].
17+
///
18+
/// If [object] is not a [DateTime] and don't have a `toJson` method, an exception will be thrown
19+
/// but handled by [JsonEncoder].
20+
/// Do not catch it.
921
static dynamic _toEncodable(dynamic object) {
1022
if (object is DateTime) {
1123
return dateToGitHubIso8601(object);
1224
}
25+
// `toJson` could return a [Map] or a [List],
26+
// so we have to delete null values in them.
1327
return _checkObject(object.toJson());
1428
}
1529

30+
/// Encodes [object] to a Json String compatible with the GitHub API.
31+
/// It should be used instead of `jsonEncode`.
32+
///
33+
/// Equivalent to `jsonEncode` except that
34+
/// it converts [DateTime] to a proper String for the GitHub API,
35+
/// and it also deletes keys associated with null values in maps before converting them.
36+
///
37+
/// The obtained String can be decoded using `jsonDecode`.
1638
static String encode(Object object, {String indent}) {
1739
final encoder = JsonEncoder.withIndent(indent, _toEncodable);
1840
return encoder.convert(_checkObject(object));
1941
}
2042

43+
/// Deletes keys associated with null values
44+
/// in every map contained in [object].
2145
static dynamic _checkObject(dynamic object) {
2246
if (object is Map) {
2347
return createNonNullMap(object, recursive: true);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy