Module: Tree::Utils::JSONConverter::ClassMethods

Defined in:
lib/tree/utils/json_converter.rb

Overview

ClassMethods for the Tree::Utils::JSONConverter module. Will become class methods in the include target.

Converting to/from JSON collapse

Instance Method Details

#json_create(json_hash) ⇒ Tree::TreeNode

Helper method to create a Tree::TreeNode instance from the JSON hash representation. Note that this method should NOT be called directly. Instead, to convert the JSON hash back to a tree, do:

tree = JSON.parse(the_json_hash, create_additions: true)

This operation requires the JSON gem to be available, or else the operation fails with a warning message.

Note the create_additions: true option, which is required for successfully parsing the string or hash.

Parameters:

  • json_hash (Hash)

    The JSON hash to convert from.

Returns:

See Also:

Author:

Since:

  • 0.7.0



120
121
122
123
124
125
126
127
128
# File 'lib/tree/utils/json_converter.rb', line 120

def json_create(json_hash)
  node = new(json_hash['name'], json_hash['content'])

  json_hash['children']&.each do |child|
    node << child
  end

  node
end