Module: Tree::Utils::JSONConverter
- Included in:
- TreeNode
- Defined in:
- lib/tree/utils/json_converter.rb
Overview
Defined Under Namespace
Modules: ClassMethods
Converting to/from JSON collapse
-
#as_json(_options = {}) ⇒ Object
Creates a JSON ready Hash for the #to_json method.
-
#to_json(*args) ⇒ Object
Creates a JSON representation of this node including all it’s children.
Instance Method Details
#as_json(_options = {}) ⇒ Object
Creates a JSON ready Hash for the #to_json method.
Rails uses JSON in ActiveSupport, and all Rails JSON encoding goes through as_json
.
noinspection RubyUnusedLocalVariable
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tree/utils/json_converter.rb', line 68 def as_json( = {}) json_hash = { name: name, content: content, JSON.create_id => self.class.name } json_hash['children'] = children if children? json_hash end |
#to_json(*args) ⇒ Object
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.
92 93 94 |
# File 'lib/tree/utils/json_converter.rb', line 92 def to_json(*args) as_json.to_json(*args) end |