diff options
| author | Aric Hagberg <aric.hagberg@gmail.com> | 2013-10-22 08:00:31 -0600 |
|---|---|---|
| committer | Aric Hagberg <aric.hagberg@gmail.com> | 2013-10-22 08:00:31 -0600 |
| commit | 8e95b34e66203498c166668aee5c22e7cdcd3656 (patch) | |
| tree | 0e59fb789b0d0e4d02f1949ecd9a61c4c9384e50 /networkx/readwrite/json_graph | |
| parent | eaa0c3436896ccef1db779fde4e17fd27a5ade6b (diff) | |
| download | networkx-8e95b34e66203498c166668aee5c22e7cdcd3656.tar.gz | |
JSON graph fix to handle unicode dict keys
Diffstat (limited to 'networkx/readwrite/json_graph')
| -rw-r--r-- | networkx/readwrite/json_graph/node_link.py | 5 | ||||
| -rw-r--r-- | networkx/readwrite/json_graph/tests/test_node_link.py | 14 | ||||
| -rw-r--r-- | networkx/readwrite/json_graph/tree.py | 3 |
3 files changed, 19 insertions, 3 deletions
diff --git a/networkx/readwrite/json_graph/node_link.py b/networkx/readwrite/json_graph/node_link.py index 03f150fe..8a45cbe2 100644 --- a/networkx/readwrite/json_graph/node_link.py +++ b/networkx/readwrite/json_graph/node_link.py @@ -7,6 +7,7 @@ from itertools import count,repeat import json import networkx as nx +from networkx.utils import make_str __author__ = """Aric Hagberg <hagberg@lanl.gov>""" __all__ = ['node_link_data', 'node_link_graph'] @@ -104,13 +105,13 @@ def node_link_graph(data, directed=False, multigraph=True): for d in data['nodes']: node = d.get('id',next(c)) mapping.append(node) - nodedata = dict((str(k),v) for k,v in d.items() if k!='id') + nodedata = dict((make_str(k),v) for k,v in d.items() if k!='id') graph.add_node(node, **nodedata) for d in data['links']: link_data = d.copy() source = link_data.pop('source') target = link_data.pop('target') - edgedata = dict((str(k),v) for k,v in d.items() + edgedata = dict((make_str(k),v) for k,v in d.items() if k!='source' and k!='target') graph.add_edge(mapping[source],mapping[target],**edgedata) return graph diff --git a/networkx/readwrite/json_graph/tests/test_node_link.py b/networkx/readwrite/json_graph/tests/test_node_link.py index 5430e0d7..4946a295 100644 --- a/networkx/readwrite/json_graph/tests/test_node_link.py +++ b/networkx/readwrite/json_graph/tests/test_node_link.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import json from nose.tools import assert_equal, assert_raises, assert_not_equal,assert_true import networkx as nx @@ -42,3 +43,16 @@ class TestNodeLink: H = node_link_graph(node_link_data(G)) nx.is_isomorphic(G,H) assert_equal(H[1][2]['second']['color'],'blue') + + def test_unicode_keys(self): + try: + q = unicode("qualité",'utf-8') + except NameError: + q = "qualité" + G = nx.Graph() + G.add_node(1, {q:q}) + s = node_link_data(G) + output = json.dumps(s, ensure_ascii=False) + data = json.loads(output) + H = node_link_graph(data) + assert_equal(H.node[1][q], q) diff --git a/networkx/readwrite/json_graph/tree.py b/networkx/readwrite/json_graph/tree.py index d2229e9a..b86064c7 100644 --- a/networkx/readwrite/json_graph/tree.py +++ b/networkx/readwrite/json_graph/tree.py @@ -7,6 +7,7 @@ from itertools import count,repeat import json import networkx as nx +from networkx.utils import make_str __author__ = """Aric Hagberg (hagberg@lanl.gov))""" __all__ = ['tree_data', 'tree_graph'] @@ -101,7 +102,7 @@ def tree_graph(data): grandchildren = data.get('children',[]) if grandchildren: add_children(child,grandchildren) - nodedata = dict((str(k),v) for k,v in data.items() + nodedata = dict((make_str(k),v) for k,v in data.items() if k!='id' and k!='children') graph.add_node(child,attr_dict=nodedata) root = data['id'] |
