Sha256: 7942b5f045678c5ed46b0dccd3f3c159979a06749c96bcab61812f91f850f9a1

Contents?: true

Size: 1.31 KB

Versions: 15

Compression:

Stored size: 1.31 KB

Contents

require 'json'
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
      def load(json)
        ENGINE.load(json, *ENGINE_LOAD_OPTIONS)
      rescue *ENGINE_ERRORS => e
        raise ParseError, e
      end

      def load_file(path)
        load(File.open(path, 'r', encoding: 'UTF-8', &:read))
      end

      def dump(value)
        ENGINE.dump(value, *ENGINE_DUMP_OPTIONS)
      end

      private

      def oj_engine
        require 'oj'
        [
          Oj,
          [{ mode: :compat, symbol_keys: false }],
          [{ mode: :compat }],
          oj_parse_error
        ]
      rescue LoadError
        false
      end

      def json_engine
        [JSON, [], [], [JSON::ParserError]]
      end

      def oj_parse_error
        if Oj.const_defined?('ParseError')
          [Oj::ParseError, EncodingError, JSON::ParserError]
        else
          [SyntaxError]
        end
      end
    end

    # @api private
    ENGINE, ENGINE_LOAD_OPTIONS, ENGINE_DUMP_OPTIONS, ENGINE_ERRORS =
      oj_engine || json_engine
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
aws-sdk-core-3.100.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.99.2 lib/aws-sdk-core/json.rb
aws-sdk-core-3.99.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.99.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.98.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.97.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.97.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.96.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.96.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.95.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.94.1 lib/aws-sdk-core/json.rb
aws-sdk-core-3.94.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.93.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.92.0 lib/aws-sdk-core/json.rb
aws-sdk-core-3.91.1 lib/aws-sdk-core/json.rb