Skip to content

Commit 070aaa4

Browse files
Added the toJson method in Pubspec (#2214)
Co-authored-by: Nate Bosch <nbosch@google.com>
1 parent e093140 commit 070aaa4

File tree

7 files changed

+370
-13
lines changed

7 files changed

+370
-13
lines changed

pkgs/pubspec_parse/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## 1.5.1-wip
1+
## 1.6.0-wip
22

3+
- Added `toJson` method to `Pubspec` to serialize the object back to a `Map`.
34
- Require Dart 3.8
45

56
## 1.5.0

pkgs/pubspec_parse/lib/src/dependency.dart

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ Map<String, Dependency> parseDeps(Map? source) =>
3939

4040
const _sourceKeys = ['sdk', 'git', 'path', 'hosted'];
4141

42-
/// Returns `null` if the data could not be parsed.
42+
/// Converts [data] into a [Dependency] object.
43+
///
44+
/// If [data] is not a valid representation of a dependency,
45+
/// returns null so that the parent logic can throw the proper error.
4346
Dependency? _fromJson(Object? data, String name) {
4447
if (data is String || data == null) {
4548
return _$HostedDependencyFromJson({'version': data});
@@ -90,11 +93,13 @@ Dependency? _fromJson(Object? data, String name) {
9093
}
9194
}
9295

93-
// Not a String or a Map – return null so parent logic can throw proper error
9496
return null;
9597
}
9698

97-
sealed class Dependency {}
99+
sealed class Dependency {
100+
/// Creates a JSON representation of the data of this object.
101+
Map<String, dynamic> toJson();
102+
}
98103

99104
@JsonSerializable()
100105
class SdkDependency extends Dependency {
@@ -114,6 +119,9 @@ class SdkDependency extends Dependency {
114119

115120
@override
116121
String toString() => 'SdkDependency: $sdk';
122+
123+
@override
124+
Map<String, dynamic> toJson() => {'sdk': sdk, 'version': version.toString()};
117125
}
118126

119127
@JsonSerializable()
@@ -149,6 +157,11 @@ class GitDependency extends Dependency {
149157

150158
@override
151159
String toString() => 'GitDependency: url@$url';
160+
161+
@override
162+
Map<String, dynamic> toJson() => {
163+
'git': {'url': url.toString(), 'ref': ?ref, 'path': ?path},
164+
};
152165
}
153166

154167
Uri? parseGitUriOrNull(String? value) =>
@@ -208,6 +221,9 @@ class PathDependency extends Dependency {
208221

209222
@override
210223
String toString() => 'PathDependency: path@$path';
224+
225+
@override
226+
Map<String, dynamic> toJson() => {'path': path};
211227
}
212228

213229
@JsonSerializable(disallowUnrecognizedKeys: true)
@@ -232,6 +248,12 @@ class HostedDependency extends Dependency {
232248

233249
@override
234250
String toString() => 'HostedDependency: $version';
251+
252+
@override
253+
Map<String, dynamic> toJson() => {
254+
'version': version.toString(),
255+
if (hosted != null) 'hosted': hosted!.toJson(),
256+
};
235257
}
236258

237259
@JsonSerializable(disallowUnrecognizedKeys: true)
@@ -275,6 +297,12 @@ class HostedDetails {
275297

276298
@override
277299
int get hashCode => Object.hash(name, url);
300+
301+
/// Creates a JSON representation of the data of this object.
302+
Map<String, dynamic> toJson() => {
303+
if (declaredName != null) 'name': declaredName,
304+
'url': url.toString(),
305+
};
278306
}
279307

280308
VersionConstraint _constraintFromString(String? input) =>

pkgs/pubspec_parse/lib/src/pubspec.dart

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import 'screenshot.dart';
1111

1212
part 'pubspec.g.dart';
1313

14-
@JsonSerializable()
14+
@JsonSerializable(createToJson: true)
1515
class Pubspec {
1616
// TODO: executables
1717

1818
final String name;
1919

20-
@JsonKey(fromJson: _versionFromString)
20+
@JsonKey(fromJson: _versionFromString, toJson: _versionToString)
2121
final Version? version;
2222

2323
final String? description;
@@ -51,7 +51,7 @@ class Pubspec {
5151
final List<String>? ignoredAdvisories;
5252

5353
/// Optional field for specifying included screenshot files.
54-
@JsonKey(fromJson: parseScreenshots)
54+
@JsonKey(fromJson: parseScreenshots, toJson: serializeScreenshots)
5555
final List<Screenshot>? screenshots;
5656

5757
/// If there is exactly 1 value in [authors], returns it.
@@ -69,16 +69,16 @@ class Pubspec {
6969
final List<String> authors;
7070
final String? documentation;
7171

72-
@JsonKey(fromJson: _environmentMap)
72+
@JsonKey(fromJson: _environmentMap, toJson: _serializeEnvironment)
7373
final Map<String, VersionConstraint?> environment;
7474

75-
@JsonKey(fromJson: parseDeps)
75+
@JsonKey(fromJson: parseDeps, toJson: _serializeDeps)
7676
final Map<String, Dependency> dependencies;
7777

78-
@JsonKey(fromJson: parseDeps)
78+
@JsonKey(fromJson: parseDeps, toJson: _serializeDeps)
7979
final Map<String, Dependency> devDependencies;
8080

81-
@JsonKey(fromJson: parseDeps)
81+
@JsonKey(fromJson: parseDeps, toJson: _serializeDeps)
8282
final Map<String, Dependency> dependencyOverrides;
8383

8484
/// Optional configuration specific to [Flutter](https://flutter.io/)
@@ -90,7 +90,7 @@ class Pubspec {
9090
final Map<String, dynamic>? flutter;
9191

9292
/// Optional field to specify executables
93-
@JsonKey(fromJson: _executablesMap)
93+
@JsonKey(fromJson: _executablesMap, toJson: _serializeExecutables)
9494
final Map<String, String?> executables;
9595

9696
/// If this package is a Pub Workspace, this field lists the sub-packages.
@@ -171,6 +171,9 @@ class Pubspec {
171171
return _$PubspecFromJson(json);
172172
}
173173

174+
/// Creates a JSON-serializable map for this instance.
175+
Map<String, dynamic> toJson() => _$PubspecToJson(this);
176+
174177
/// Parses source [yaml] into [Pubspec].
175178
///
176179
/// When [lenient] is set, top-level property-parsing or type cast errors are
@@ -247,3 +250,17 @@ Map<String, String?> _executablesMap(Map? source) =>
247250
}
248251
}) ??
249252
{};
253+
254+
Map<String, String?> _serializeEnvironment(
255+
Map<String, VersionConstraint?> map,
256+
) => map.map((key, value) => MapEntry(key, value?.toString()));
257+
258+
String? _versionToString(Version? version) => version?.toString();
259+
260+
Map<String, String?> _serializeExecutables(Map<String, String?>? map) => {
261+
...?map,
262+
};
263+
264+
Map<String, dynamic> _serializeDeps(Map<String, Dependency> input) => {
265+
for (final MapEntry(:key, :value) in input.entries) key: value.toJson(),
266+
};

pkgs/pubspec_parse/lib/src/pubspec.g.dart

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

pkgs/pubspec_parse/lib/src/screenshot.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ List<Screenshot> parseScreenshots(List? input) {
6363
}
6464
return res;
6565
}
66+
67+
/// Serializes a list of [Screenshot] objects into a JSON-serializable
68+
/// list of maps.
69+
List<Map<String, String>> serializeScreenshots(List<Screenshot>? input) => [
70+
if (input != null)
71+
for (var e in input) {'description': e.description, 'path': e.path},
72+
];

pkgs/pubspec_parse/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: pubspec_parse
2-
version: 1.5.1-wip
2+
version: 1.6.0-wip
33
description: >-
44
Simple package for parsing pubspec.yaml files with a type-safe API and rich
55
error reporting.

0 commit comments

Comments
 (0)