module Tree::Utils::JSONConverter
Provides utility methods to convert a {Tree::TreeNode} to and from JSON.
Public Class Methods
Source
# File lib/tree/utils/json_converter.rb, line 47 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
Source
# File lib/tree/utils/json_converter.rb, line 68 def as_json(_options = {}) json_hash = { name: name, content: content, JSON.create_id => self.class.name } json_hash['children'] = children if children? json_hash end
Creates a JSON ready Hash for the to_json
method.
@author Eric Cline (github.com/escline) @since 0.8.3
@return A hash based representation of the JSON
Rails uses JSON in ActiveSupport, and all Rails JSON encoding goes through as_json
.
@param [Object] _options
@see to_json
@see stackoverflow.com/a/6880638/273808 noinspection RubyUnusedLocalVariable
Source
# File lib/tree/utils/json_converter.rb, line 92 def to_json(*args) as_json.to_json(*args) end
Creates a JSON representation of this node including all it’s children. This requires the JSON gem to be available, or else the operation fails with a warning message. Uses the Hash output of as_json
method.
@author Dirk Breuer (github.com/railsbros-dirk) @since 0.7.0
@return The JSON representation of this subtree.
@see ClassMethods#json_create
@see as_json
@see flori.github.com/json