Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
156 additions
and
10 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
"""Importer.""" | ||
|
||
from .dictimporter import DictImporter # noqa | ||
from .jsonimporter import JsonImporter # noqa |
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
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,65 @@ | ||
import json | ||
|
||
from .dictimporter import DictImporter | ||
|
||
|
||
class JsonImporter(object): | ||
|
||
def __init__(self, dictimporter=None, **kwargs): | ||
""" | ||
Import Tree from JSON. | ||
The JSON is read and converted to a dictionary via `dictimporter`. | ||
Keyword Arguments: | ||
dictimporter: Dictionary Importer used (see :any:`DictImporter`). | ||
kwargs: All other arguments are passed to | ||
:any:`json.load`/:any:`json.loads`. | ||
See documentation for reference. | ||
>>> from anytree.importer import JsonImporter | ||
>>> from anytree import RenderTree | ||
>>> importer = JsonImporter() | ||
>>> data = ''' | ||
... { | ||
... "a": "root", | ||
... "children": [ | ||
... { | ||
... "a": "sub0", | ||
... "children": [ | ||
... { | ||
... "a": "sub0A", | ||
... "b": "foo" | ||
... }, | ||
... { | ||
... "a": "sub0B" | ||
... } | ||
... ] | ||
... }, | ||
... { | ||
... "a": "sub1" | ||
... } | ||
... ] | ||
... }''' | ||
>>> root = importer.import_(data) | ||
>>> print(RenderTree(root)) | ||
AnyNode(a='root') | ||
├── AnyNode(a='sub0') | ||
│ ├── AnyNode(a='sub0A', b='foo') | ||
│ └── AnyNode(a='sub0B') | ||
└── AnyNode(a='sub1') | ||
""" | ||
self.dictimporter = dictimporter | ||
self.kwargs = kwargs | ||
|
||
def import_(self, data): | ||
"""Read JSON from `data`.""" | ||
dictimporter = self.dictimporter or DictImporter() | ||
data = json.loads(data, **self.kwargs) | ||
return dictimporter.import_(data) | ||
|
||
def read(self, filehandle): | ||
"""Read JSON from `filehandle`.""" | ||
dictimporter = self.dictimporter or DictImporter() | ||
data = json.load(filehandle, **self.kwargs) | ||
return dictimporter.import_(data) |
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
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,7 @@ | ||
JSON Importer | ||
============= | ||
|
||
.. automodule:: anytree.importer.jsonimporter | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
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
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
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,73 @@ | ||
import filecmp | ||
|
||
from tempfile import NamedTemporaryFile | ||
|
||
from nose.tools import eq_ | ||
|
||
from anytree import AnyNode | ||
from anytree.importer import JsonImporter | ||
from anytree.exporter import DictExporter | ||
from anytree.importer import DictImporter | ||
|
||
|
||
def test_json_importer(): | ||
"""Json Importer.""" | ||
refdata = { | ||
'id': 'root', 'children': [ | ||
{'id': 'sub0', 'children': [ | ||
{'id': 'sub0B'}, | ||
{'id': 'sub0A'} | ||
]}, | ||
{'id': 'sub1', 'children': [ | ||
{'id': 'sub1A'}, | ||
{'id': 'sub1B'}, | ||
{'id': 'sub1C', 'children': [ | ||
{'id': 'sub1Ca'} | ||
]} | ||
]} | ||
]} | ||
lines = [ | ||
'{', | ||
' "children": [', | ||
' {', | ||
' "children": [', | ||
' {', | ||
' "id": "sub0B"', | ||
' },', | ||
' {', | ||
' "id": "sub0A"', | ||
' }', | ||
' ],', | ||
' "id": "sub0"', | ||
' },', | ||
' {', | ||
' "children": [', | ||
' {', | ||
' "id": "sub1A"', | ||
' },', | ||
' {', | ||
' "id": "sub1B"', | ||
' },', | ||
' {', | ||
' "children": [', | ||
' {', | ||
' "id": "sub1Ca"', | ||
' }', | ||
' ],', | ||
' "id": "sub1C"', | ||
' }', | ||
' ],', | ||
' "id": "sub1"', | ||
' }', | ||
' ],', | ||
' "id": "root"', | ||
'}' | ||
] | ||
|
||
imported = DictExporter().export(JsonImporter().import_("\n".join(lines))) | ||
eq_(refdata, imported) | ||
with NamedTemporaryFile(mode="w+") as ref: | ||
ref.write("\n".join(lines)) | ||
ref.seek(0) | ||
imported = DictExporter().export(JsonImporter().read(ref)) | ||
eq_(refdata, imported) |