Class: Doze::Entity::JSON

Inherits:
Doze::Entity show all
Defined in:
lib/doze/serialization/json.rb

Constant Summary

Constants inherited from Doze::Entity

DEFAULT_TEXT_ENCODING

Instance Attribute Summary

Attributes inherited from Doze::Entity

#binary_data_length, #encoding, #extra_content_headers, #language, #media_type, #media_type_params

Instance Method Summary collapse

Methods inherited from Doze::Entity

#binary_data, #binary_data_stream, #etag, #initialize

Constructor Details

This class inherits a constructor from Doze::Entity

Instance Method Details

#deserialize(binary_data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/doze/serialization/json.rb', line 13

def deserialize(binary_data)
  begin
    case binary_data
    when /^[\[\{]/
      ::JSON.parse(binary_data)
    else
      # A pox on the arbitrary syntactic limitation that a top-level piece of JSON must be a hash or array
      ::JSON.parse("[#{binary_data}]").first
    end
  rescue ::JSON::ParserError
    raise Doze::ClientEntityError, "Could not parse JSON"
  end
end

#serialize(ruby_data) ⇒ Object



9
10
11
# File 'lib/doze/serialization/json.rb', line 9

def serialize(ruby_data)
  ruby_data.to_json
end