RubyTree icon indicating copy to clipboard operation
RubyTree copied to clipboard

JSON.parse not compiling to a Tree::TreeNode

Open westlakem opened this issue 9 years ago • 2 comments

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

westlakem avatar May 17 '16 20:05 westlakem

@evolve75 Any updates?

westlakem avatar Jun 06 '16 17:06 westlakem

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.

alxppp avatar Aug 27 '16 15:08 alxppp

The core behavior in Ruby seems to have changed, use create_additions option on JSON.parse

      JSON.parse(str, create_additions: true)

VinceGuidry avatar Dec 11 '23 21:12 VinceGuidry