Class: Booqable::JsonApiSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/booqable/json_api_serializer.rb

Overview

JSON API serializer with support for multiple JSON backends

Handles encoding and decoding of JSON API responses with automatic relationship population and attribute transformation. Supports multiple JSON libraries including the standard JSON gem, Yajl, and MultiJson.

See: github.com/lostisland/sawyer/blob/142c8fd9ee82bc01dd71e1929be0e4fd975fd9ed/lib/sawyer/serializer.rb

Examples:

Basic usage

serializer = Booqable::JsonApiSerializer.any_json
data = { "name" => "Order", "created_at" => Time.now }
encoded = serializer.encode(data)
decoded = serializer.decode(encoded)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, dump_method_name = nil, load_method_name = nil) ⇒ JsonApiSerializer

Initialize a new serializer

Wraps a serialization format for JSON API processing. Nested objects are prepared for serialization (such as changing Times to ISO 8601 Strings). Any serialization format that responds to #dump and #load will work.

Parameters:

  • format (Object)

    The JSON library to use (e.g., JSON, Yajl)

  • dump_method_name (Symbol, nil) (defaults to: nil)

    Method name for encoding (default: :dump)

  • load_method_name (Symbol, nil) (defaults to: nil)

    Method name for decoding (default: :load)



74
75
76
77
78
# File 'lib/booqable/json_api_serializer.rb', line 74

def initialize(format, dump_method_name = nil, load_method_name = nil)
  @format = format
  @dump = @format.method(dump_method_name || :dump)
  @load = @format.method(load_method_name || :load)
end

Class Method Details

.any_jsonBooqable::JsonApiSerializer

Get the first available JSON serializer

Tries different JSON libraries in order of preference and returns the first one that’s available.

Returns:

Raises:

  • (RuntimeError)

    If no JSON library is available



23
24
25
26
27
# File 'lib/booqable/json_api_serializer.rb', line 23

def self.any_json
  yajl || multi_json || json || begin
    raise RuntimeError, "Sawyer requires a JSON gem: yajl, multi_json, or json"
  end
end

.jsonBooqable::JsonApiSerializer?

Create a serializer using the standard JSON library

Returns:



41
42
43
44
45
# File 'lib/booqable/json_api_serializer.rb', line 41

def self.json
  require "json"
  new(JSON)
rescue LoadError
end

.message_packBooqable::JsonApiSerializer?

Create a serializer using the MessagePack library

Returns:



59
60
61
62
63
# File 'lib/booqable/json_api_serializer.rb', line 59

def self.message_pack
  require "msgpack"
  new(MessagePack, :pack, :unpack)
rescue LoadError
end

.multi_jsonBooqable::JsonApiSerializer?

Create a serializer using the MultiJson library

Returns:



50
51
52
53
54
# File 'lib/booqable/json_api_serializer.rb', line 50

def self.multi_json
  require "multi_json"
  new(MultiJson)
rescue LoadError
end

.yajlBooqable::JsonApiSerializer?

Create a serializer using the Yajl JSON library

Returns:



32
33
34
35
36
# File 'lib/booqable/json_api_serializer.rb', line 32

def self.yajl
  require "yajl"
  new(Yajl)
rescue LoadError
end

Instance Method Details

#decode(data) ⇒ Object? Also known as: load

Decode JSON data to Ruby objects

Decodes a JSON string into Ruby objects (usually a Hash or Array of Hashes) with JSON API relationship population and attribute transformation.

Parameters:

  • data (String, nil)

    JSON string to be decoded

Returns:

  • (Object, nil)

    Decoded Ruby object, or nil for empty/nil input



100
101
102
103
# File 'lib/booqable/json_api_serializer.rb', line 100

def decode(data)
  return nil if data.nil? || data.strip.empty?
  decoded = decode_object(@load.call(data))
end

#encode(data) ⇒ String Also known as: dump

Encode an object to JSON

Encodes an Object (usually a Hash or Array of Hashes) with special handling for dates, times, and nested structures.

Parameters:

  • data (Object)

    Object to be encoded

Returns:

  • (String)

    JSON-encoded string



87
88
89
# File 'lib/booqable/json_api_serializer.rb', line 87

def encode(data)
  @dump.call(encode_object(data))
end