module Tree::Utils::JSONConverter

Provides utility methods to convert a {Tree::TreeNode} to and from JSON.

Public Class Methods

included(base) click to toggle source
# File lib/tree/utils/json_converter.rb, line 44
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

as_json(options = {}) click to toggle source

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.

@see to_json @see stackoverflow.com/a/6880638/273808

# File lib/tree/utils/json_converter.rb, line 62
def as_json(options = {})

  json_hash = {
    "name"         => name,
    "content"      => content,
    JSON.create_id => self.class.name
  }

  if has_children?
    json_hash["children"] = children
  end

  return json_hash

end
to_json(*a) click to toggle source

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 Tree::Utils::JSONConverter::ClassMethods#json_create @see as_json @see flori.github.com/json

# File lib/tree/utils/json_converter.rb, line 90
def to_json(*a)
  as_json.to_json(*a)
end