@@ -11,13 +11,13 @@ import 'screenshot.dart';
1111
1212part 'pubspec.g.dart' ;
1313
14- @JsonSerializable ()
14+ @JsonSerializable (createToJson : true )
1515class 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+ };
0 commit comments