Skip to content

Issue #188 Fix JSON Exporter for Symlink Nodes #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion anytree/exporter/dictexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ def _iter_attr_values(self, node):
for k, v in node.__dict__.items():
if k in ('_NodeMixin__children', '_NodeMixin__parent'):
continue
yield k, v
elif k == "target":
for key, val in self._iter_attr_values(v):
yield key, val
else:
yield k, v
31 changes: 31 additions & 0 deletions tests/test_dictexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from anytree import AnyNode
from anytree import Node
from anytree import NodeMixin
from anytree import SymlinkNode
from anytree.exporter import DictExporter


Expand Down Expand Up @@ -132,3 +133,33 @@ def __init__(self, foo, parent=None):
]}
]}
)


def test_dict_exporter_symlink_node():
"""Dict Exporter."""
root = Node("root")
s0 = Node("sub0", parent=root)
s0b = Node("sub0B", parent=s0)
s0a = Node("sub0A", parent=s0)
s1 = Node("sub1", parent=root, foo="bar")
s1a = SymlinkNode(target=s0, parent=s1)
s1b = Node("sub1B", parent=s1)
s1c = Node("sub1C", parent=s1)
s1ca = SymlinkNode(target=s1, parent=s1c)

exporter = DictExporter()
eq_(exporter.export(root),
{'name': 'root', 'children': [
{'name': 'sub0', 'children': [
{'name': 'sub0B'},
{'name': 'sub0A'}
]},
{'name': 'sub1', 'foo': 'bar', 'children': [
{'name': 'sub0'},
{'name': 'sub1B'},
{'name': 'sub1C', 'children': [
{'name': 'sub1', 'foo': 'bar'}
]}
]}
]}
)