Sha256: b402be3b6fb494c5468bd3e7eeca1ff6a4de7261d9fc716a582cf3100bb46703

Contents?: true

Size: 895 Bytes

Versions: 2

Compression:

Stored size: 895 Bytes

Contents

gem 'json'
require 'json'
require 'json/add/rails'
require 'json/add/core'

module SweatShop
  module Serializers
    class JsonSerializer < SweatShop::Serializer
      class << self
      
        def serialize(payload)
          if payload.respond_to?(:to_json)
            payload.to_json
          else
            JSON.generate(payload)
          end
        end
      
        def deserialize(payload)
          symbolize_keys(JSON.parse(payload))
        end
        
        protected 
        # another straight out of rails land - ActiveSupport::CoreExtensions::Hash::Keys
        def symbolize_keys(data)
          if data.is_a?(Hash)
            data.inject({}) do |options, (key, value)|
              options[(key.to_sym rescue key) || key] = value
              options
            end
          else
            data
          end
        end
        
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
netinlet-sweat_shop-1.1.0 lib/serializers/json_serializer.rb
netinlet-sweat_shop-1.1.1 lib/serializers/json_serializer.rb