oj 3.13.15 → 3.13.18

Sign up to get free protection for your applications and to get access to all the features.
data/ext/oj/wab.c CHANGED
@@ -266,7 +266,7 @@ static DumpFunc wab_funcs[] = {
266
266
  void oj_dump_wab_val(VALUE obj, int depth, Out out) {
267
267
  int type = rb_type(obj);
268
268
 
269
- if (Yes == out->opts->trace) {
269
+ if (RB_UNLIKELY(Yes == out->opts->trace)) {
270
270
  oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceIn);
271
271
  }
272
272
  if (MAX_DEPTH < depth) {
@@ -277,7 +277,7 @@ void oj_dump_wab_val(VALUE obj, int depth, Out out) {
277
277
 
278
278
  if (NULL != f) {
279
279
  f(obj, depth, out, false);
280
- if (Yes == out->opts->trace) {
280
+ if (RB_UNLIKELY(Yes == out->opts->trace)) {
281
281
  oj_trace("dump", obj, __FILE__, __LINE__, depth, TraceOut);
282
282
  }
283
283
  return;
@@ -312,13 +312,13 @@ static VALUE calc_hash_key(ParseInfo pi, Val parent) {
312
312
  }
313
313
 
314
314
  static void hash_end(ParseInfo pi) {
315
- if (Yes == pi->options.trace) {
315
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
316
316
  oj_trace_parse_hash_end(pi, __FILE__, __LINE__);
317
317
  }
318
318
  }
319
319
 
320
320
  static void array_end(ParseInfo pi) {
321
- if (Yes == pi->options.trace) {
321
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
322
322
  oj_trace_parse_array_end(pi, __FILE__, __LINE__);
323
323
  }
324
324
  }
@@ -328,7 +328,7 @@ static VALUE noop_hash_key(ParseInfo pi, const char *key, size_t klen) {
328
328
  }
329
329
 
330
330
  static void add_value(ParseInfo pi, VALUE val) {
331
- if (Yes == pi->options.trace) {
331
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
332
332
  oj_trace_parse_call("add_value", pi, __FILE__, __LINE__, val);
333
333
  }
334
334
  pi->stack.head->val = val;
@@ -478,7 +478,7 @@ static VALUE cstr_to_rstr(ParseInfo pi, const char *str, size_t len) {
478
478
 
479
479
  static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
480
480
  pi->stack.head->val = cstr_to_rstr(pi, str, len);
481
- if (Yes == pi->options.trace) {
481
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
482
482
  oj_trace_parse_call("add_string", pi, __FILE__, __LINE__, pi->stack.head->val);
483
483
  }
484
484
  }
@@ -488,13 +488,13 @@ static void add_num(ParseInfo pi, NumInfo ni) {
488
488
  oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
489
489
  }
490
490
  pi->stack.head->val = oj_num_as_value(ni);
491
- if (Yes == pi->options.trace) {
491
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
492
492
  oj_trace_parse_call("add_number", pi, __FILE__, __LINE__, pi->stack.head->val);
493
493
  }
494
494
  }
495
495
 
496
496
  static VALUE start_hash(ParseInfo pi) {
497
- if (Yes == pi->options.trace) {
497
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
498
498
  oj_trace_parse_in("start_hash", pi, __FILE__, __LINE__);
499
499
  }
500
500
  if (Qnil != pi->options.hash_class) {
@@ -507,7 +507,7 @@ static void hash_set_cstr(ParseInfo pi, Val parent, const char *str, size_t len,
507
507
  volatile VALUE rval = cstr_to_rstr(pi, str, len);
508
508
 
509
509
  rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), rval);
510
- if (Yes == pi->options.trace) {
510
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
511
511
  oj_trace_parse_call("set_string", pi, __FILE__, __LINE__, rval);
512
512
  }
513
513
  }
@@ -520,20 +520,20 @@ static void hash_set_num(ParseInfo pi, Val parent, NumInfo ni) {
520
520
  }
521
521
  rval = oj_num_as_value(ni);
522
522
  rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), rval);
523
- if (Yes == pi->options.trace) {
523
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
524
524
  oj_trace_parse_call("set_number", pi, __FILE__, __LINE__, rval);
525
525
  }
526
526
  }
527
527
 
528
528
  static void hash_set_value(ParseInfo pi, Val parent, VALUE value) {
529
529
  rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), value);
530
- if (Yes == pi->options.trace) {
530
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
531
531
  oj_trace_parse_call("set_value", pi, __FILE__, __LINE__, value);
532
532
  }
533
533
  }
534
534
 
535
535
  static VALUE start_array(ParseInfo pi) {
536
- if (Yes == pi->options.trace) {
536
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
537
537
  oj_trace_parse_in("start_array", pi, __FILE__, __LINE__);
538
538
  }
539
539
  return rb_ary_new();
@@ -543,7 +543,7 @@ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const c
543
543
  volatile VALUE rval = cstr_to_rstr(pi, str, len);
544
544
 
545
545
  rb_ary_push(stack_peek(&pi->stack)->val, rval);
546
- if (Yes == pi->options.trace) {
546
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
547
547
  oj_trace_parse_call("set_value", pi, __FILE__, __LINE__, rval);
548
548
  }
549
549
  }
@@ -556,14 +556,14 @@ static void array_append_num(ParseInfo pi, NumInfo ni) {
556
556
  }
557
557
  rval = oj_num_as_value(ni);
558
558
  rb_ary_push(stack_peek(&pi->stack)->val, rval);
559
- if (Yes == pi->options.trace) {
559
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
560
560
  oj_trace_parse_call("append_number", pi, __FILE__, __LINE__, rval);
561
561
  }
562
562
  }
563
563
 
564
564
  static void array_append_value(ParseInfo pi, VALUE value) {
565
565
  rb_ary_push(stack_peek(&pi->stack)->val, value);
566
- if (Yes == pi->options.trace) {
566
+ if (RB_UNLIKELY(Yes == pi->options.trace)) {
567
567
  oj_trace_parse_call("append_value", pi, __FILE__, __LINE__, value);
568
568
  }
569
569
  }
data/lib/oj/saj.rb CHANGED
@@ -1,11 +1,17 @@
1
1
  module Oj
2
- # A SAX style parse handler for JSON hence the acronym SAJ for Simple API for
3
- # JSON. The Oj::Saj handler class should be subclassed and then used with the
4
- # Oj::Saj key_parse() method. The Saj methods will then be called as the file
5
- # is parsed.
2
+ # A SAX style parse handler for JSON hence the acronym SAJ for Simple API
3
+ # for JSON. The Oj::Saj handler class can be subclassed and then used with
4
+ # the Oj::Saj key_parse() method or with the more resent
5
+ # Oj::Parser.new(:saj). The Saj methods will then be called as the file is
6
+ # parsed.
7
+ #
8
+ # With Oj::Parser.new(:saj) each method can also include a line and column
9
+ # argument so hash_start(key) could also be hash_start(key, line,
10
+ # column). The error() method is no used with Oj::Parser.new(:saj) so it
11
+ # will never be called.
6
12
  #
7
13
  # @example
8
- #
14
+ #
9
15
  # require 'oj'
10
16
  #
11
17
  # class MySaj < ::Oj::Saj
@@ -23,6 +29,14 @@ module Oj
23
29
  # Oj.saj_parse(cnt, f)
24
30
  # end
25
31
  #
32
+ # or
33
+ #
34
+ # p = Oj::Parser.new(:saj)
35
+ # p.handler = MySaj.new()
36
+ # File.open('any.json', 'r') do |f|
37
+ # p.parse(f.read)
38
+ # end
39
+ #
26
40
  # To make the desired methods active while parsing the desired method should
27
41
  # be made public in the subclasses. If the methods remain private they will
28
42
  # not be called during parsing.
@@ -61,6 +75,6 @@ module Oj
61
75
 
62
76
  def error(message, line, column)
63
77
  end
64
-
78
+
65
79
  end # Saj
66
80
  end # Oj
data/lib/oj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '3.13.15'
4
+ VERSION = '3.13.18'
5
5
  end
data/test/bar.rb CHANGED
@@ -6,11 +6,6 @@ $: << File.join(File.dirname(__FILE__), "../ext")
6
6
 
7
7
  require 'oj'
8
8
 
9
- puts Oj.dump({
10
- "float_test" => 0.25,
11
- "nan_test" => Float::NAN,
12
- "inf_test" => Float::INFINITY,
13
- "minus_inf_test" => -Float::INFINITY,
14
- "min_test" => Float::MIN,
15
- "max_test" => Float::MAX,
16
- }, mode: :object) # => {"float_test":0.25,"nan_test":3.3e14159265358979323846
9
+ p = Oj::Parser.validate
10
+ # p = Oj::Parser.new(:debug)
11
+ p.parse(%|{|)
data/test/test_compat.rb CHANGED
@@ -504,6 +504,15 @@ class CompatJuice < Minitest::Test
504
504
  assert_equal("ぴーたー", Oj.load(json)['a'])
505
505
  end
506
506
 
507
+ def test_parse_large_escaped_string
508
+ invalid_json = %|{"a":\"aaaa\\nbbbb\\rcccc\\tddd\\feee\\bf\/\\\\\\u3074\\u30fc\\u305f\\u30fc }|
509
+ error = assert_raises() { Oj.load(invalid_json) }
510
+ assert(error.message.include?('quoted string not terminated'))
511
+
512
+ json = "\"aaaa\\nbbbb\\rcccc\\tddd\\feee\\bf\/\\\\\\u3074\\u30fc\\u305f\\u30fc \""
513
+ assert_equal("aaaa\nbbbb\rcccc\tddd\feee\bf/\\ぴーたー ", Oj.load(json))
514
+ end
515
+
507
516
  def dump_and_load(obj, trace=false)
508
517
  json = Oj.dump(obj)
509
518
  puts json if trace
@@ -5,7 +5,7 @@ $: << File.dirname(__FILE__)
5
5
 
6
6
  require 'helper'
7
7
 
8
- $json = %{{
8
+ $json = %|{
9
9
  "array": [
10
10
  {
11
11
  "num" : 3,
@@ -18,7 +18,7 @@ $json = %{{
18
18
  }
19
19
  ],
20
20
  "boolean" : true
21
- }}
21
+ }|
22
22
 
23
23
  class AllSaj < Oj::Saj
24
24
  attr_accessor :calls
@@ -53,6 +53,35 @@ class AllSaj < Oj::Saj
53
53
 
54
54
  end # AllSaj
55
55
 
56
+ class LocSaj
57
+ attr_accessor :calls
58
+
59
+ def initialize()
60
+ @calls = []
61
+ end
62
+
63
+ def hash_start(key, line, column)
64
+ @calls << [:hash_start, key, line, column]
65
+ end
66
+
67
+ def hash_end(key, line, column)
68
+ @calls << [:hash_end, key, line, column]
69
+ end
70
+
71
+ def array_start(key, line, column)
72
+ @calls << [:array_start, key, line, column]
73
+ end
74
+
75
+ def array_end(key, line, column)
76
+ @calls << [:array_end, key, line, column]
77
+ end
78
+
79
+ def add_value(value, key, line, column)
80
+ @calls << [:add_value, value, key, line, column]
81
+ end
82
+
83
+ end # LocSaj
84
+
56
85
  class SajTest < Minitest::Test
57
86
 
58
87
  def test_nil
@@ -242,4 +271,28 @@ class SajTest < Minitest::Test
242
271
  ], handler.calls)
243
272
  end
244
273
 
274
+ def test_loc
275
+ handler = LocSaj.new()
276
+ Oj::Parser.saj.handler = handler
277
+ Oj::Parser.saj.parse($json)
278
+ assert_equal([[:hash_start, nil, 1, 1],
279
+ [:array_start, 'array', 2, 12],
280
+ [:hash_start, nil, 3, 5],
281
+ [:add_value, 3, 'num', 4, 18],
282
+ [:add_value, 'message', 'string', 5, 25],
283
+ [:hash_start, 'hash', 6, 17],
284
+ [:hash_start, 'h2', 7, 17],
285
+ [:array_start, 'a', 8, 17],
286
+ [:add_value, 1, nil, 8, 20],
287
+ [:add_value, 2, nil, 8, 23],
288
+ [:add_value, 3, nil, 8, 26],
289
+ [:array_end, 'a', 8, 27],
290
+ [:hash_end, 'h2', 9, 9],
291
+ [:hash_end, 'hash', 10, 7],
292
+ [:hash_end, nil, 11, 5],
293
+ [:array_end, 'array', 12, 3],
294
+ [:add_value, true, 'boolean', 13, 18],
295
+ [:hash_end, nil, 14, 1]], handler.calls)
296
+ end
297
+
245
298
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.13.15
4
+ version: 3.13.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-03 00:00:00.000000000 Z
11
+ date: 2022-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler