Skip to content

Commit ca65189

Browse files
ianardeefharper
authored andcommitted
🐛 fix for dumping JSON in CLI
1 parent 92cbbad commit ca65189

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* :bug: fix for locale constructor
77
* :bug: fix custom document in CLI
88
* :label: declare type info to mypy
9+
* :bug: fix for dumping JSON in CLI
910

1011
### Changes
1112
* :label: set stricter pylint and mypy settings

mindee/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any, Dict
55

66
from mindee import Client
7+
from mindee.documents.base import serialize_for_json
78

89
DOCUMENTS: Dict[str, Dict[str, Any]] = {
910
"invoice": {
@@ -91,7 +92,8 @@ def call_endpoint(args: Namespace):
9192
if args.output_type == "raw":
9293
print(json.dumps(parsed_data.http_response, indent=2))
9394
elif args.output_type == "parsed":
94-
print(json.dumps(getattr(parsed_data, doc_type), indent=2, default=vars))
95+
doc = getattr(parsed_data, doc_type)
96+
print(json.dumps(doc, indent=2, default=serialize_for_json))
9597
else:
9698
print(getattr(parsed_data, doc_type))
9799

mindee/documents/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
import datetime
12
from typing import List, Optional, Type
23

34
# from mindee.inputs import InputDocument
45
from mindee.http import Endpoint
56

67

8+
def serialize_for_json(obj):
9+
"""
10+
Custom serializer for Document objects.
11+
12+
Use as the `default` argument of the `json.dump` functions.
13+
"""
14+
if isinstance(obj, datetime.date):
15+
return obj.__str__()
16+
return vars(obj)
17+
18+
719
class Document:
820
type: str
921
checklist: dict = {}

0 commit comments

Comments
 (0)