Module: SerialBox::Serializers::JSON

Extended by:
ActiveSupport::Concern
Defined in:
lib/serializers/json.rb

Overview

JSON adapter for SerialBox. Defines the following methods when included in a class:

  • ‘#as_json`

  • ‘#to_json`

  • ‘.json_create`

This gives the class total ability to work with ‘JSON.parse` and `#to_json`.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

JSON_CREATE_ID =
JSON.respond_to?(:create_id) ? JSON.create_id : 'json_class'

Instance Method Summary collapse

Instance Method Details

#as_jsonHash, Array

Converts this object into primitives suitable for JSON serialization.

Returns:

  • (Hash, Array)

    A JSON-serializable object.



29
30
31
32
33
34
35
# File 'lib/serializers/json.rb', line 29

def as_json(*)
  hsh = {SerialBox::Serializers::JSON::JSON_CREATE_ID => self.class.name}
  self.class._serialbox_serializer.serialization_operations.each do |operation|
    operation.apply self, hsh
  end
  return hsh
end

#to_json(*args) ⇒ String

Serializes this object as JSON.

Returns:

  • (String)

    The JSON representation of this object.



41
# File 'lib/serializers/json.rb', line 41

def to_json(*args) as_json.to_json(*args) end