File: oj_engine.rb

package info (click to toggle)
ruby-aws-sdk-core 3.212.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,232 kB
  • sloc: ruby: 17,533; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,027 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

require 'oj'

module Aws
  module Json
    module OjEngine
      # @api private
      LOAD_OPTIONS = {
        mode: :compat,
        symbol_keys: false,
        empty_string: false
      }.freeze

      # @api private
      DUMP_OPTIONS = { mode: :compat }.freeze

      class << self
        def load(json)
          Oj.load(json, LOAD_OPTIONS)
        rescue *PARSE_ERRORS => e
          raise ParseError.new(e)
        end

        def dump(value)
          Oj.dump(value, DUMP_OPTIONS)
        end

        private

        # Oj before 1.4.0 does not define Oj::ParseError and instead raises
        # SyntaxError on failure
        def detect_oj_parse_errors
          require 'oj'

          if Oj.const_defined?(:ParseError)
            [Oj::ParseError, EncodingError, JSON::ParserError]
          else
            [SyntaxError]
          end
        rescue LoadError
          nil
        end
      end

      # @api private
      PARSE_ERRORS = detect_oj_parse_errors
    end
  end
end