module Tree::Utils::JSONConverter::ClassMethods
ClassMethods for the {JSONConverter} module. Will become class methods in the include target.
Public Instance Methods
Source
# File lib/tree/utils/json_converter.rb, line 137 def from_json(source) tree = JSON.parse(source, on_load: method(:json_on_load).to_proc) return tree if tree.is_a?(self) raise ArgumentError, "JSON document does not describe a #{self} tree" end
Parses a JSON document and returns the tree it describes.
tree = Tree::TreeNode.from_json(json_string)
The nodes are rebuilt from the hashes tagged with the json_class key, see {#json_on_load} for the rules. The document root must describe a node of this class, or of one of its subclasses.
This works with both the json gem 2.x and 3.x. With the json gem 2.x the legacy +JSON.parse(json_string, create_additions: true)+ keeps working as well, but the json gem 3.x removed that option.
@param [String] source The JSON document to parse.
@return [Tree::TreeNode] The root node of the parsed tree.
@raise [ArgumentError] This exception is raised if the document does
not describe a node of this class.
@raise [JSON::ParserError] This exception is raised if the document
is not valid JSON.
@see json_on_load @see JSONConverter#to_json
Source
# File lib/tree/utils/json_converter.rb, line 211 def json_create(json_hash) node = new(json_hash['name'], json_hash['content']) json_hash['children']&.each do |child| node << child end node end
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 a JSON document back to a tree, do:
tree = Tree::TreeNode.from_json(the_json_string)
With the json gem 2.x this method also serves as the hook of the create_additions mechanism, so the legacy +JSON.parse(the_json_string, create_additions: true)+ keeps working there. The json gem 3.x removed that mechanism.
@author Dirk Breuer (github.com/railsbros-dirk) @since 0.7.0
@param [Hash] json_hash The JSON hash to convert from.
@return [Tree::TreeNode] The created tree.
@see from_json @see json_on_load @see JSONConverter#to_json
Source
# File lib/tree/utils/json_converter.rb, line 181 def json_node_class(class_name) return nil unless class_name.is_a?(String) node_class = Object.const_get(class_name) node_class if node_class.is_a?(Class) && node_class <= self rescue NameError nil end
Resolves the class named by a json_class tag, limited to this class and its subclasses.
@param [String, nil] class_name The class name found in the tag.
@return [Class, nil] The node class, or nil when the tag is
missing, unknown or names a foreign class.
Source
# File lib/tree/utils/json_converter.rb, line 167 def json_on_load(object) return object unless object.is_a?(Hash) node_class = json_node_class(object[JSON_CLASS_KEY]) node_class ? node_class.json_create(object) : object end
Rebuilds a node from its parsed JSON hash. This is the on_load callback for the parser of the json gem, used by {#from_json} and available for custom parser setups (the parser expects a Proc):
on_load = Tree::TreeNode.method(:json_on_load).to_proc tree = JSON::Coder.new(on_load: on_load).load(json_string)
The parser invokes the callback for every parsed value, innermost first, so the children of a node hash are already nodes by the time the hash of their parent arrives here. Only hashes whose json_class names this class or one of its subclasses are turned into nodes, any other value is returned untouched. This keeps the deserialization scoped to tree nodes, unlike the global create_additions mechanism of the json gem 2.x which instantiated any class named in a document.
@param [Object] object A value produced by the JSON parser.
@return [Tree::TreeNode, Object] The node for a tagged hash, or the
unchanged value otherwise.
@see from_json @see json_create