-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Python bindings): Add utilty functions for converting to/from JSON
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
import .types | ||
from .types import Node, Thing | ||
|
||
# TODO: Use Entity instead of Thing | ||
# When https://github.com/stencila/schema/issues/96 is done | ||
|
||
def toDict(node: Thing) -> dict: | ||
"""Convert a Thing node to a dictionary""" | ||
node_dict = { | ||
"type": node.__class__.__name__ | ||
} | ||
node_dict.update(node.__dict__) | ||
return node_dict | ||
|
||
def fromDict(node_dict: dict) -> Thing: | ||
"""Convert a dictionary to a Thing node""" | ||
if "type" in node_dict: | ||
class_ = getattr(types, node_dict["type"]) | ||
return class_(**node_dict) | ||
else: | ||
return node_dict | ||
|
||
def toJSON(node: Node) -> str: | ||
"""Convert a node to JSON""" | ||
return json.dumps(node, default=toDict, indent=2) | ||
|
||
def fromJSON(json: str) -> Node: | ||
"""Convert JSON to a Node""" | ||
node = json.loads(json) | ||
if isinstance(node, dict): return fromDict(node) | ||
else return node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters