Skip to content

Commit 16fb464

Browse files
committed
Consolidate 2 json.dart files into 1. make new GithubJson part of common.
1 parent 8166d06 commit 16fb464

18 files changed

+65
-71
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 6.2.0
2+
- Bug fix: Fix setRepositorySubscription to be a PUT instead of a POST https://github.com/SpinlockLabs/github.dart/commit/5b5d7656ce9ce1cb06e15651da06e7e192bc19e1
3+
- Bug fix: Repository clone URLs were null. DEPRECATED `Repository.cloneUrls` use `cloneUrl`,`gitUrl`,`sshUrl`, or `svnUrl` instead.
4+
- Bug fix: Use a shared json encoder util to remove nulls from maps and lists, encode all dates for github. https://github.com/SpinlockLabs/github.dart/pull/182
5+
16
## 6.1.3
27
- Add missing fields for Notification https://github.com/SpinlockLabs/github.dart/pull/210
38
- Can now create draft PRs https://github.com/SpinlockLabs/github.dart/pull/212

lib/src/common/activity_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:convert';
33
import 'package:github/src/common.dart';
44
import 'package:github/src/common/model/users.dart';
55
import 'package:github/src/common/util/pagination.dart';
6-
import 'package:github/src/json.dart';
76
import 'package:github/src/util.dart';
87
import 'package:http/http.dart' as http;
98

lib/src/common/gists_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22
import 'dart:convert';
33
import 'package:github/src/common.dart';
44
import 'package:github/src/common/util/pagination.dart';
5-
import 'package:github/src/json.dart';
65

76
/// The [GistsService] handles communication with gist
87
/// methods of the GitHub API.

lib/src/common/git_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22
import 'dart:convert';
33
import 'package:github/src/common.dart';
44
import 'package:github/src/common/util/pagination.dart';
5-
import 'package:github/src/json.dart';
65

76
/// The [GitService] handles communication with git related methods of the
87
/// GitHub API.

lib/src/common/issues_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'dart:convert';
44
import 'package:github/src/common.dart';
55
import 'package:github/src/common/model/users.dart';
66
import 'package:github/src/common/util/pagination.dart';
7-
import 'package:github/src/json.dart';
87

98
/// The [IssuesService] handles communication with issues related methods of the
109
/// GitHub API.

lib/src/common/misc_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'dart:async';
22
import 'package:github/src/common.dart';
3-
import 'package:github/src/json.dart';
43

54
/// The [MiscService] handles communication with misc related methods of the
65
/// GitHub API.

lib/src/common/model/repos.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class Repository {
190190
}
191191

192192
/// Repository Clone Urls
193-
@Deprecated("These URLs are available on the Repository class")
193+
@Deprecated('These URLs are available on the Repository class')
194194
@JsonSerializable()
195195
class CloneUrls {
196196
/// Git Protocol

lib/src/common/orgs_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22
import 'dart:convert';
33
import 'package:github/src/common.dart';
44
import 'package:github/src/common/util/pagination.dart';
5-
import 'package:github/src/json.dart';
65
import 'package:github/src/util.dart';
76
import 'package:http/http.dart' as http;
87

lib/src/common/pulls_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22
import 'dart:convert';
33
import 'package:github/src/common.dart';
44
import 'package:github/src/common/util/pagination.dart';
5-
import 'package:github/src/json.dart';
65
import 'package:github/src/util.dart';
76

87
/// The [PullRequestsService] handles communication with pull request

lib/src/common/repos_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'package:github/src/common.dart';
44
import 'package:github/src/common/model/repos_releases.dart';
55
import 'package:github/src/common/model/users.dart';
66
import 'package:github/src/common/util/pagination.dart';
7-
import 'package:github/src/json.dart';
87
import 'package:github/src/util.dart';
98
import 'package:http/http.dart' as http;
109
import 'package:meta/meta.dart';

lib/src/common/users_service.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22
import 'package:github/src/common.dart';
33
import 'package:github/src/common/model/users.dart';
44
import 'package:github/src/common/util/pagination.dart';
5-
import 'package:github/src/json.dart';
65
import 'package:github/src/util.dart';
76
import 'package:http/http.dart' as http;
87

lib/src/common/util/json.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,61 @@
1+
import 'dart:convert';
2+
3+
import 'package:github/src/common/util/utils.dart';
4+
5+
16
/// Creates a Model Object from the JSON [input]
27
typedef JSONConverter<S, T> = T Function(S input);
8+
9+
10+
/// Internal class for Json encoding
11+
/// that should be used instead of `dart:convert`.
12+
///
13+
/// It contains methods that ensures that converted Json
14+
/// will work with the GitHub API.
15+
class GitHubJson {
16+
const GitHubJson._();
17+
18+
/// Called only if an object is of a non primitive type.
19+
///
20+
/// If [object] is a [DateTime], it converts it to a String whose format is compatible with the API.
21+
/// Else, it uses the default behavior of [JsonEncoder] which is to call `toJson` method onto [object].
22+
///
23+
/// If [object] is not a [DateTime] and don't have a `toJson` method, an exception will be thrown
24+
/// but handled by [JsonEncoder].
25+
/// Do not catch it.
26+
static dynamic _toEncodable(dynamic object) {
27+
if (object is DateTime) {
28+
return dateToGitHubIso8601(object);
29+
}
30+
// `toJson` could return a [Map] or a [List],
31+
// so we have to delete null values in them.
32+
return _checkObject(object.toJson());
33+
}
34+
35+
/// Encodes [object] to a Json String compatible with the GitHub API.
36+
/// It should be used instead of `jsonEncode`.
37+
///
38+
/// Equivalent to `jsonEncode` except that
39+
/// it converts [DateTime] to a proper String for the GitHub API,
40+
/// and it also deletes keys associated with null values in maps before converting them.
41+
///
42+
/// The obtained String can be decoded using `jsonDecode`.
43+
static String encode(Object object, {String indent}) {
44+
final encoder = JsonEncoder.withIndent(indent, _toEncodable);
45+
return encoder.convert(_checkObject(object));
46+
}
47+
48+
/// Deletes keys associated with null values
49+
/// in every map contained in [object].
50+
static dynamic _checkObject(dynamic object) {
51+
if (object is Map) {
52+
return Map.fromEntries(object.entries
53+
.where((e) => e.value != null)
54+
.map((e) => MapEntry(e.key, _checkObject(e.value))));
55+
}
56+
if (object is List) {
57+
return object.map(_checkObject).toList();
58+
}
59+
return object;
60+
}
61+
}

lib/src/common/util/oauth2.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:async';
22
import 'dart:convert';
33
import 'package:github/src/common.dart';
4-
import 'package:github/src/json.dart';
54
import 'package:github/src/util.dart';
65
import 'package:http/http.dart' as http;
76

lib/src/json.dart

Lines changed: 0 additions & 56 deletions
This file was deleted.

lib/src/server/hooks.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:async';
22
import 'dart:convert';
33
import 'dart:io';
4-
import 'package:github/src/json.dart';
54
import 'package:json_annotation/json_annotation.dart';
65
import '../common.dart';
76

test/data_object_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'dart:convert';
22

3-
import 'package:github/src/json.dart';
43
import 'package:test/test.dart';
54
import 'package:github/github.dart';
65

test/experiment/error_handling.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:io';
22

33
import 'package:github/github.dart';
4-
import 'package:github/src/json.dart';
54

65
import '../helper/http.dart';
76

test/git_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:async';
22
import 'dart:convert';
33

44
import 'package:github/src/common.dart';
5-
import 'package:github/src/json.dart';
65
import 'package:github/src/util.dart';
76
import 'package:http/http.dart' as http;
87
import 'package:mockito/mockito.dart';

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