Skip to content

Commit 2dfd33b

Browse files
updated readme to include toJson methods
1 parent 9c472f8 commit 2dfd33b

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,41 @@ void main() {
1717
}
1818
```
1919

20-
You can find more about the `PubspecLock` object in the documentation [here](https://pub.dev/documentation/pubspec_lock_parse/latest/)
20+
You can find more about the `PubspecLock` object in the documentation [here](https://pub.dev/documentation/pubspec_lock_parse/latest/)
21+
22+
### toJson() methods
23+
24+
`pubspec_lock_parse` includes `.toJson()` methods on each object to allow for easy re-generation/mutation of pubspec files
25+
26+
This can be combined with [yaml_writer](https://github.com/gmpassos/yaml_writer) to re-generate yaml output
27+
28+
```dart
29+
void main() {
30+
final pubspec = PubspecLock(
31+
packages: {
32+
'somePackage': Package(
33+
dependency: 'transitive',
34+
description: PathPackageDescription(
35+
path: '../somePackage',
36+
relative: true,
37+
)
38+
)
39+
},
40+
sdks: {
41+
'dart': VersionConstraint.parse(">=2.17.0 <3.0.0"),
42+
}
43+
);
44+
45+
final jsonPubspec = pubspec.toJson(); // outputs Map<String, dynamic>
46+
final yamlPubspec = YamlWriter().write(jsonPubspec); // outputs String
47+
}
48+
```
49+
50+
`toJson` can also be run on any individual object
51+
```dart
52+
// outputs: { path: '../somePackage', relative: true }
53+
print(PathPackageDescription(
54+
path: '../somePackage',
55+
relative: true,
56+
));
57+
```

0 commit comments

Comments
 (0)