module Served::Serializers::Json

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

Public Class Methods

dump(resource, attributes) click to toggle source
# 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) click to toggle source
# File lib/served/serializers/json.rb, line 27
def self.exception(data)
  JSON.parse(data)
rescue StandardError
  {}
end
load(resource, data) click to toggle source
# 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