JSON.parse not compiling to a Tree::TreeNode
Title says it all, JSON.parse is returning the default hash instead of tree
json file
{
"name": "ROOT",
"content": null,
"json_class": "Tree::TreeNode",
"children": [
{
"name": "NON-ROOT",
"content": null,
"json_class": "Tree::TreeNode",
"children": [
{
"name": "NON-ROOT-2",
"content": null,
"json_class": "Tree::TreeNode"
}
]
}
]
}
r = JSON.parse(File.read('config/routes.json'))
=> {"name"=>"ROOT", "content"=>nil, "json_class"=>"Tree::TreeNode", "children"=>[{"name"=>"NON-ROOT", "content"=>nil, "json_class"=>"Tree::TreeNode", "children"=>[{"name"=>"NON-ROOT-2", "content"=>nil, "json_class"=>"Tree::TreeNode"}]}]}
r.class
=> Hash
rubytree version 0.9.7 JSON version 1.8.3 on JRUBY 9.0.5.0
@evolve75 Any updates?
I am using this as a workaround:
def load_tree(json_hash)
node = Tree::TreeNode.new(json_hash['name'], json_hash['content'])
json_hash['children'].each { |h| node << load_tree(h) } unless json_hash['children'].nil?
node
end
I guess the performance is terrible but it's working well enough for me with a tree < 10,000 leafs.
The core behavior in Ruby seems to have changed, use create_additions option on JSON.parse
JSON.parse(str, create_additions: true)