Skip to content

Commit 5e92586

Browse files
committed
move to using canonicaljson for bundle output
1 parent 5c4f28d commit 5e92586

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ structure based on the current specification and would allow for building a cust
4545
user interface for generating `bundle.json` files.
4646

4747
```python
48-
import json
4948
from cnab import Bundle, InvocationImage
5049

5150
bundle = Bundle(
@@ -60,7 +59,7 @@ bundle = Bundle(
6059
],
6160
)
6261

63-
print(json.dumps(bundle.to_dict(), indent=4))
62+
print(bundle.to_json())
6463
```
6564

6665
## Running CNABs

cnab/test_types.py

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ def test_convert_bundle_to_dict(self, bundle):
5151
def test_bundle_description_blank(self, bundle):
5252
assert not bundle.description
5353

54+
def test_convert_bundle_to_json(self, bundle):
55+
assert isinstance(bundle.to_json(), str)
56+
57+
def test_convert_bundle_to_pretty_json(self, bundle):
58+
assert isinstance(bundle.to_json(pretty=True), str)
59+
5460

5561
def test_read_bundle():
5662
with open("fixtures/helloworld/bundle.json") as f:

cnab/types.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import canonicaljson # type: ignore
2+
13
from dataclasses import dataclass, field
24
from typing import Optional, Any, List, Union, Dict, TypeVar, Callable, Type, cast
35

@@ -416,3 +418,10 @@ def to_dict(self) -> dict:
416418
result["schemaVersion"] = from_str(self.schema_version)
417419
result["version"] = from_str(self.version)
418420
return clean(result)
421+
422+
def to_json(self, pretty: bool = False) -> str:
423+
if pretty:
424+
func = canonicaljson.encode_pretty_printed_json
425+
else:
426+
func = canonicaljson.encode_canonical_json
427+
return func(self.to_dict()).decode()

poetry.lock

+33-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ keywords = ["cnab"]
1111
[tool.poetry.dependencies]
1212
python = "^3.7"
1313
docker = { version = "^3.6", optional = true }
14+
canonicaljson = "^1.1"
1415

1516
[tool.poetry.extras]
1617
docker = ["docker"]

0 commit comments

Comments
 (0)