Sha256: 329e87f6549144f46deec2d5350aafc8bf7cf451c84375ec34a2788b788d89f7

Contents?: true

Size: 1.6 KB

Versions: 33

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require_relative 'json/builder'
require_relative 'json/error_handler'
require_relative 'json/handler'
require_relative 'json/parser'

module Aws
  # @api private
  module Json
    class ParseError < StandardError
      def initialize(error)
        @error = error
        super(error.message)
      end

      attr_reader :error
    end

    class << self
      # @param [Symbol,Class] engine
      #   Must be one of the following values:
      #
      #   * :oj
      #   * :json
      #
      def engine=(engine)
        @engine = Class === engine ? engine : load_engine(engine)
      end

      # @return [Class] Returns the default engine.
      #   One of:
      #
      #   * {OjEngine}
      #   * {JsonEngine}
      #
      def engine
        set_default_engine unless @engine
        @engine
      end

      def load(json)
        @engine.load(json)
      end

      def dump(value)
        @engine.dump(value)
      end

      def set_default_engine
        [:oj, :json].each do |name|
          @engine ||= try_load_engine(name)
        end
        unless @engine
          raise 'Unable to find a compatible json library. ' \
          'Ensure that you have installed or added to your Gemfile one of ' \
          'oj or json'
        end
      end

      private

      def load_engine(name)
        require "aws-sdk-core/json/#{name}_engine"
        const_name = name[0].upcase + name[1..-1] + 'Engine'
        const_get(const_name)
      end

      def try_load_engine(name)
        load_engine(name)
      rescue LoadError
        false
      end
    end

    set_default_engine
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
aws-sdk-core-3.218.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.218.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.217.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.217.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.216.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.216.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.215.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.215.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.214.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.214.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.213.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.212.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.211.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.210.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.209.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.209.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.208.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.207.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.206.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.205.0 lib/aws-sdk-core/json.rb