File: mimic.rb

package info (click to toggle)
ruby-oj 3.16.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,208 kB
  • sloc: ansic: 20,028; ruby: 11,642; sh: 70; makefile: 17
file content (301 lines) | stat: -rw-r--r-- 8,255 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# frozen_string_literal: false

require 'bigdecimal'
require 'ostruct'

module Oj

  ##
  # Custom mode can be used to emulate the compat mode with some minor
  # differences. These are the options that setup the custom mode to be like
  # the compat mode.
  CUSTOM_MIMIC_JSON_OPTIONS = {
    allow_gc: true,
    allow_invalid_unicode: false,
    allow_nan: false,
    array_class: nil,
    array_nl: nil,
    auto_define: false,
    bigdecimal_as_decimal: false,
    bigdecimal_load: :auto,
    circular: false,
    class_cache: false,
    cache_keys: true,
    cache_str: 5,
    create_additions: false,
    create_id: "json_class",
    empty_string: false,
    escape_mode: :unicode_xss,
    float_precision: 0,
    hash_class: nil,
    ignore: nil,
    ignore_under: false,
    indent: 0,
    integer_range: nil,
    mode: :custom,
    nan: :raise,
    nilnil: false,
    object_nl: nil,
    omit_nil: false,
    quirks_mode: true,
    safe: false,
    second_precision: 3,
    space: nil,
    space_before: nil,
    symbol_keys: false,
    time_format: :ruby,
    trace: false,
    use_as_json: false,
    use_raw_json: false,
    use_to_hash: false,
    use_to_json: true,
  }

  # A bit hack-ish but does the trick. The JSON.dump_default_options is a Hash
  # but in mimic we use a C struct to store defaults. This class creates a view
  # onto that struct.
  class MimicDumpOption < Hash
    def initialize()
      oo = Oj.default_options
      self.store(:max_nesting, false)
      self.store(:allow_nan, true)
      self.store(:quirks_mode, oo[:quirks_mode])
      self.store(:ascii_only, (:ascii == oo[:escape_mode]))

      super
    end

    def []=(key, value)
      case key
      when :quirks_mode
        Oj.default_options = {:quirks_mode => value}
      when :ascii_only
        Oj.default_options = {:ascii_only => value}
      end
    end
  end

  # Loads mimic-ed JSON paths. Used by Oj.mimic_JSON().
  # @param mimic_paths [Array] additional paths to add to the Ruby loaded features.
  def self.mimic_loaded(mimic_paths=[])
    $LOAD_PATH.each do |d|
      next unless File.exist?(d)

      jfile = File.join(d, 'json.rb')
      $LOADED_FEATURES << jfile unless $LOADED_FEATURES.include?(jfile) if File.exist?(jfile)

      Dir.glob(File.join(d, 'json', '**', '*.rb')).each do |file|
        # allow json/add/xxx to be loaded. User can override with Oj.add_to_json(xxx).
        $LOADED_FEATURES << file unless $LOADED_FEATURES.include?(file) unless file.include?('add')
      end
    end
    mimic_paths.each { |p| $LOADED_FEATURES << p }
    $LOADED_FEATURES << 'json' unless $LOADED_FEATURES.include?('json')

    require 'oj/json'

    if Object.const_defined?('OpenStruct')
      OpenStruct.class_eval do
        # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
        unless defined?(self.as_json)
          def as_json(*)
            name = self.class.name.to_s
            raise JSON::JSONError, "Only named structs are supported!" if 0 == name.length

            { JSON.create_id => name, 't' => table }
          end
        end
        def self.json_create(h)
          new(h['t'] || h[:t])
        end
      end
    end

    BigDecimal.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          {JSON.create_id => 'BigDecimal', 'b' => _dump }
        end
      end
      def self.json_create(h)
        BigDecimal._load(h['b'])
      end
    end

    Complex.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          {JSON.create_id => 'Complex', 'r' => real, 'i' => imag }
        end
      end
      def self.json_create(h)
        Complex(h['r'], h['i'])
      end
    end

    DateTime.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          { JSON.create_id => 'DateTime',
            'y' => year,
            'm' => month,
            'd' => day,
            'H' => hour,
            'M' => min,
            'S' => sec,
            'of' => offset.to_s,
            'sg' => start }
        end
      end
      def self.json_create(h)
        # offset is a rational as a string
        as, bs = h['of'].split('/')
        a = as.to_i
        b = bs.to_i
        if 0 == b
          off = a
        else
          off = Rational(a, b)
        end
        civil(h['y'], h['m'], h['d'], h['H'], h['M'], h['S'], off, h['sg'])
      end
    end

    Date.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          { JSON.create_id => 'Date', 'y' => year, 'm' => month, 'd' => day, 'sg' => start }
        end
      end
      def self.json_create(h)
        civil(h['y'], h['m'], h['d'], h['sg'])
      end
    end

    Exception.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          {JSON.create_id => self.class.name, 'm' => message, 'b' => backtrace }
        end
      end
      def self.json_create(h)
        e = new(h['m'])
        e.set_backtrace(h['b'])
        e
      end
    end

    Range.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          {JSON.create_id => 'Range', 'a' => [first, last, exclude_end?]}
        end
      end
      def self.json_create(h)
        new(*h['a'])
      end
    end

    Rational.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          {JSON.create_id => 'Rational', 'n' => numerator, 'd' => denominator }
        end
      end
      def self.json_create(h)
        Rational(h['n'], h['d'])
      end
    end

    Regexp.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          {JSON.create_id => 'Regexp', 'o' => options, 's' => source }
        end
      end
      def self.json_create(h)
        new(h['s'], h['o'])
      end
    end

    Struct.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          name = self.class.name.to_s
          raise JSON::JSONError, "Only named structs are supported!" if 0 == name.length

          { JSON.create_id => name, 'v' => values }
        end
      end
      def self.json_create(h)
        new(*h['v'])
      end
    end

    Symbol.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          {JSON.create_id => 'Symbol', 's' => to_s }
        end
      end
      def self.json_create(h)
        h['s'].to_sym
      end
    end

    Time.class_eval do
      # Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
      unless defined?(self.as_json)
        def as_json(*)
          nsecs = [ tv_usec * 1000 ]
          nsecs << tv_nsec if respond_to?(:tv_nsec)
          nsecs = nsecs.max
          { JSON.create_id => 'Time', 's' => tv_sec, 'n' => nsecs }
        end
      end
      def self.json_create(h)
        if (usec = h.delete('u'))
          h['n'] = usec * 1000
        end
        if instance_methods.include?(:tv_nsec)
          at(h['s'], Rational(h['n'], 1000))
        else
          at(h['s'], h['n'] / 1000)
        end
      end
    end
  end # self.mimic_loaded

end # Oj

# More monkey patches.
class String
  def to_json_raw_object
    {
      JSON.create_id => self.class.name,
      'raw' => self.bytes
    }
  end
  def to_json_raw(*)
   to_json_raw_object().to_json()
  end
  def self.json_create(obj)
    s = ''
    s.encode!(Encoding::ASCII_8BIT) if s.respond_to?(:encode!)
    raw = obj['raw']
    if raw.is_a? Array
      raw.each { |v| s << v }
    end
    s
  end
end