Class: Booqable::JsonApiSerializer
- Inherits:
-
Object
- Object
- Booqable::JsonApiSerializer
- 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
Class Method Summary collapse
-
.any_json ⇒ Booqable::JsonApiSerializer
Get the first available JSON serializer.
-
.json ⇒ Booqable::JsonApiSerializer?
Create a serializer using the standard JSON library.
-
.message_pack ⇒ Booqable::JsonApiSerializer?
Create a serializer using the MessagePack library.
-
.multi_json ⇒ Booqable::JsonApiSerializer?
Create a serializer using the MultiJson library.
-
.yajl ⇒ Booqable::JsonApiSerializer?
Create a serializer using the Yajl JSON library.
Instance Method Summary collapse
-
#decode(data) ⇒ Object?
(also: #load)
Decode JSON data to Ruby objects.
-
#encode(data) ⇒ String
(also: #dump)
Encode an object to JSON.
-
#initialize(format, dump_method_name = nil, load_method_name = nil) ⇒ JsonApiSerializer
constructor
Initialize a new serializer.
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.
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_json ⇒ Booqable::JsonApiSerializer
Get the first available JSON serializer
Tries different JSON libraries in order of preference and returns the first one that’s 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 |
.json ⇒ Booqable::JsonApiSerializer?
Create a serializer using the standard JSON library
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_pack ⇒ Booqable::JsonApiSerializer?
Create a serializer using the MessagePack library
59 60 61 62 63 |
# File 'lib/booqable/json_api_serializer.rb', line 59 def self. require "msgpack" new(MessagePack, :pack, :unpack) rescue LoadError end |
.multi_json ⇒ Booqable::JsonApiSerializer?
Create a serializer using the MultiJson library
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 |
.yajl ⇒ Booqable::JsonApiSerializer?
Create a serializer using the Yajl JSON library
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.
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.
87 88 89 |
# File 'lib/booqable/json_api_serializer.rb', line 87 def encode(data) @dump.call(encode_object(data)) end |