Sha256: 27f4321e79539d5f636fb8bd8bceb87b329c2846967c778e56504678b22186e1

Contents?: true

Size: 1.5 KB

Versions: 26

Compression:

Stored size: 1.5 KB

Contents

begin
  require 'multi_json'
rescue LoadError
  require 'json'
end

module Hanami
  module Utils
    # JSON wrapper
    #
    # If you use MultiJson gem this wrapper will use it.
    # Otherwise - JSON std lib.
    #
    # @since 0.8.0
    module Json
      # MultiJson adapter
      #
      # @since 0.9.1
      # @api private
      class MultiJsonAdapter
        # @since 0.9.1
        # @api private
        def parse(payload)
          MultiJson.load(payload)
        end

        # @since 0.9.1
        # @api private
        def generate(object)
          MultiJson.dump(object)
        end
      end

      # rubocop:disable Style/ClassVars
      if defined?(MultiJson)
        @@engine    = MultiJsonAdapter.new
        ParserError = MultiJson::ParseError
      else
        @@engine    = ::JSON
        ParserError = ::JSON::ParserError
      end
      # rubocop:enable Style/ClassVars

      # Parse the given JSON paylod
      #
      # @param payload [String] a JSON payload
      #
      # @return [Object] the result of the loading process
      #
      # @raise [Hanami::Utils::Json::ParserError] if the paylod is invalid
      #
      # @since 0.9.1
      def self.parse(payload)
        @@engine.parse(payload)
      end

      # Generate a JSON document from the given object
      #
      # @param object [Object] any object
      #
      # @return [String] the result of the dumping process
      #
      # @since 0.9.1
      def self.generate(object)
        @@engine.generate(object)
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
hanami-utils-1.3.5 lib/hanami/utils/json.rb
hanami-utils-1.3.4 lib/hanami/utils/json.rb
hanami-utils-1.3.3 lib/hanami/utils/json.rb
hanami-utils-1.3.2 lib/hanami/utils/json.rb
hanami-utils-1.3.1 lib/hanami/utils/json.rb
hanami-utils-1.3.0 lib/hanami/utils/json.rb
hanami-utils-1.3.0.beta1 lib/hanami/utils/json.rb
hanami-utils-1.2.0 lib/hanami/utils/json.rb
hanami-utils-1.2.0.rc2 lib/hanami/utils/json.rb
hanami-utils-1.2.0.rc1 lib/hanami/utils/json.rb
hanami-utils-1.2.0.beta2 lib/hanami/utils/json.rb
hanami-utils-1.2.0.beta1 lib/hanami/utils/json.rb
hanami-utils-1.1.2 lib/hanami/utils/json.rb
hanami-utils-1.1.1 lib/hanami/utils/json.rb
hanami-utils-1.1.0 lib/hanami/utils/json.rb
hanami-utils-1.1.0.rc1 lib/hanami/utils/json.rb
hanami-utils-1.1.0.beta3 lib/hanami/utils/json.rb
hanami-utils-1.1.0.beta2 lib/hanami/utils/json.rb
hanami-utils-1.0.4 lib/hanami/utils/json.rb
hanami-utils-1.0.3 lib/hanami/utils/json.rb