Skip to content
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

Unjsonify Function #48

Closed
eileenjiang opened this issue Aug 17, 2015 · 3 comments
Closed

Unjsonify Function #48

eileenjiang opened this issue Aug 17, 2015 · 3 comments

Comments

@eileenjiang
Copy link

Hey guys, I noticed that treelib has a to_json function, but not an unjsonify function that inputs a JSON string and builds the original tree as the output. I decided to go ahead and write an unjsonify function, which I thought might be useful:

import json                                                                                                              
from treelib import Node, Tree
from collections import Hashable

# takes in a jsonified tree and returns the correct tree structure
def unjsonify(jsonTree):
    def giveKey(d):
        return d.keys()[0]
    def unjsonifyHelper(node, subtree):
        for childStruct in subtree[node]['children']: # childStruct is immutable element if base case, else dict
            if type(childStruct) == list: childStruct = tuple(childStruct) # tuples in original tree
            if isinstance(childStruct, Hashable): # base case
                newTree.create_node(childStruct, childStruct, parent=node)
            else:
                childNode = giveKey(childStruct)
                newTree.create_node(childNode, childNode, parent=node)
                unjsonifyHelper(childNode, childStruct)
    jsonTree = json.loads(jsonTree)
    root = giveKey(jsonTree)
    newTree = Tree()
    newTree.create_node(root, root)
    unjsonifyHelper(root, jsonTree)
    return newTree

# simple test
tree = Tree()
tree.create_node('home', 'home')
tree.create_node('phone', 'phone', parent='home')
tree.create_node('laptop', 'laptop', parent='home')
tree.create_node('screen', 'screen', parent='laptop')
tree.create_node(19, 19, parent='home')
tree.create_node((1,2), (1,2), parent='screen')
j = tree.to_json()
unjsonify(j).show()   

There's some stuff to note here about the code:

  1. The to_json function as it's written only preserves keys and not values in the tree. Would you guys be interested in a custom to_json function that saves both keys and values (which json.dumps currently doesn't do right now)?
  2. I'm not 100% sure why, but json.dumps converts tuples into lists. Because treelib doesn't allow mutable elements to be keys, I've decided to turn lists back into tuples before I determine if an element is hashable or not (and would be a leaf if it's hashable), since there couldn't have been any lists as keys in the original tree anyway.

Please let me know what you guys think!

@caesar0301
Copy link
Owner

We are collecting extra features based on the Tree object. A module located in treelib.plugins is added to hold these features. You can add your unjsonify() function there.

What do you mean about value in the json tree? At present, both node id and data are preserved in the json string. But treelib currently complains about the tuple key in your example, as the to_json function needs the key to be a string. We are glad if you can enhance it.

@eileenjiang
Copy link
Author

Just FYI, I just forked and created a pull request with the unjsonify code in my original post.

Hmm. I'm not actually quite sure what you mean. It seems to me that when I parse through the JSON string, I only see the keys for the JSON string. Could you provide a small example of a JSON string that contains both the key and value for each node?

Also, why do the keys added to the tree need to be a string? I've been using integers, tuples, and essentially any immutable value as keys, and the tree has always built successfully.

Sorry for the late response!

@leonardbinet
Copy link
Collaborator

This issue is included in this one: #85 (json/yaml serialization/deserialization)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants