Module: Served::Serializers::Json

Defined in:
lib/served/serializers/json.rb

Overview

The default serializer assumes that the default Rails API response is used for both data and errors.

Class Method Summary collapse

Class Method Details

.dump(resource, attributes) ⇒ Object



22
23
24
25
# File 'lib/served/serializers/json.rb', line 22

def self.dump(resource, attributes)
  a = Hash[attributes.collect { |k, v| v.blank? ? nil : [k, v] }.compact]
  { resource.resource_name.singularize => a }.to_json
end

.exception(data) ⇒ Object



27
28
29
30
31
# File 'lib/served/serializers/json.rb', line 27

def self.exception(data)
  JSON.parse(data)
rescue StandardError
  {}
end

.load(resource, data) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/served/serializers/json.rb', line 7

def self.load(resource, data)
  parsed = JSON.parse(data)
  # assume we need to return the entire response if it isn't
  # namespaced by keys. TODO: remove after 1.0, this is strictly
  # for backwards compatibility
  resource_name = resource.resource_name
  if resource_name.is_a? Array
    warn '[DEPRECATION] passing an array for resource name will no longer ' \
         'be supported in Served 1.0, please ensure a single string is ' \
         'returned instead'
    resource_name = resource_name.last # backwards compatibility
  end
  parsed[resource_name.singularize] || parsed
end