oj 3.10.16 → 3.10.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c6cf97a6abc7ea8698d7a6937bdd9444ed8e2def9447411fc6f2f0aaaaf1c9a
4
- data.tar.gz: d7c34c8a8de00f25718e2b20a404cc1f929dd091571a49070dee8e7edb18aa6b
3
+ metadata.gz: 6089f48d996b2561868c1d0098388cb09a484b0554f713d2ba1bb6da87c90cd7
4
+ data.tar.gz: 3068e3d291d958f5b0e89f95461b83fd4e766e27bc6a7be7bc6062c893802924
5
5
  SHA512:
6
- metadata.gz: a89b9497d951820e4d0c3e8e1bc94119acd85efec15a152513e29dd7210e1fbb41153a2205638862199b94eb50b05cc67e1b2fa373c4e0cbb834b44f210bc776
7
- data.tar.gz: d6ff8fddb8a6bc3475eb6555bdd03794d41ae3c9c553d23d1af3622e5b4c9729001429827eef32e80e8a19c3fd95984ae97eeafa0bca3768f6779d3a0987686a
6
+ metadata.gz: 16e0bb20797dbfcca9784c976931347086adb66b3658a8b6548c0941324e9517ae246777813d70a9434d31e9dd0591ec83363ae38b2bbd9d7c86c4ca79c9110c
7
+ data.tar.gz: e1b1d8232e68addab4c44ef0c9e2bfd46ecedf2a888024bacec093f67c3b44d4b99c43770b07aebfb5242532b685fe35a2b783a13d0c22fa11748b14141ab5a1
@@ -174,15 +174,20 @@ hixss_friendly_size(const uint8_t *str, size_t len) {
174
174
  return size - len * (size_t)'0' + check;
175
175
  }
176
176
 
177
- inline static size_t
177
+ inline static long
178
178
  rails_xss_friendly_size(const uint8_t *str, size_t len) {
179
- size_t size = 0;
179
+ long size = 0;
180
180
  size_t i = len;
181
+ uint8_t hi = 0;
181
182
 
182
183
  for (; 0 < i; str++, i--) {
183
184
  size += rails_xss_friendly_chars[*str];
185
+ hi |= *str & 0x80;
184
186
  }
185
- return size - len * (size_t)'0';
187
+ if (0 == hi) {
188
+ return size - len * (size_t)'0';
189
+ }
190
+ return -(size - len * (size_t)'0');
186
191
  }
187
192
 
188
193
  inline static size_t
@@ -249,7 +254,6 @@ dump_hex(uint8_t c, Out out) {
249
254
 
250
255
  static void
251
256
  raise_invalid_unicode(const char *str, int len, int pos) {
252
- char buf[len + 1];
253
257
  char c;
254
258
  char code[32];
255
259
  char *cp = code;
@@ -268,8 +272,7 @@ raise_invalid_unicode(const char *str, int len, int pos) {
268
272
  cp--;
269
273
  *cp++ = ']';
270
274
  *cp = '\0';
271
- strncpy(buf, str, len);
272
- rb_raise(oj_json_generator_error_class, "Invalid Unicode %s at %d in '%s'", code, pos, buf);
275
+ rb_raise(oj_json_generator_error_class, "Invalid Unicode %s at %d", code, pos);
273
276
  }
274
277
 
275
278
  static const char*
@@ -767,6 +770,7 @@ oj_dump_cstr(const char *str, size_t cnt, bool is_sym, bool escape1, Out out) {
767
770
  size_t size;
768
771
  char *cmap;
769
772
  const char *orig = str;
773
+ bool has_hi = false;
770
774
 
771
775
  switch (out->opts->escape_mode) {
772
776
  case NLEsc:
@@ -785,10 +789,19 @@ oj_dump_cstr(const char *str, size_t cnt, bool is_sym, bool escape1, Out out) {
785
789
  cmap = hixss_friendly_chars;
786
790
  size = hixss_friendly_size((uint8_t*)str, cnt);
787
791
  break;
788
- case RailsXEsc:
792
+ case RailsXEsc: {
793
+ long sz;
794
+
789
795
  cmap = rails_xss_friendly_chars;
790
- size = rails_xss_friendly_size((uint8_t*)str, cnt);
796
+ sz = rails_xss_friendly_size((uint8_t*)str, cnt);
797
+ if (sz < 0) {
798
+ has_hi = true;
799
+ size = (size_t)-sz;
800
+ } else {
801
+ size = (size_t)sz;
802
+ }
791
803
  break;
804
+ }
792
805
  case RailsEsc:
793
806
  cmap = rails_friendly_chars;
794
807
  size = rails_friendly_size((uint8_t*)str, cnt);
@@ -812,7 +825,7 @@ oj_dump_cstr(const char *str, size_t cnt, bool is_sym, bool escape1, Out out) {
812
825
  str++;
813
826
  is_sym = 0; // just to make sure
814
827
  }
815
- if (cnt == size) {
828
+ if (cnt == size && !has_hi) {
816
829
  if (is_sym) {
817
830
  *out->cur++ = ':';
818
831
  }
@@ -510,8 +510,6 @@ mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
510
510
  pi.options.create_ok = No;
511
511
  pi.options.allow_nan = (bang ? Yes : No);
512
512
  pi.options.nilnil = No;
513
- //pi.options.bigdec_load = FloatDec;
514
- pi.options.bigdec_load = RubyDec;
515
513
  pi.options.mode = CompatMode;
516
514
  pi.max_depth = 100;
517
515
 
@@ -561,6 +559,16 @@ mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
561
559
  pi.options.array_class = v;
562
560
  }
563
561
  }
562
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_decimal_class_sym)) {
563
+ v = rb_hash_lookup(ropts, oj_decimal_class_sym);
564
+ if (rb_cFloat == v) {
565
+ pi.options.bigdec_load = FloatDec;
566
+ } else if (oj_bigdecimal_class == v) {
567
+ pi.options.bigdec_load = BigDec;
568
+ } else if (Qnil == v) {
569
+ pi.options.bigdec_load = AutoDec;
570
+ }
571
+ }
564
572
  v = rb_hash_lookup(ropts, oj_max_nesting_sym);
565
573
  if (Qtrue == v) {
566
574
  pi.max_depth = 100;
@@ -88,6 +88,7 @@ VALUE oj_slash_string;
88
88
  VALUE oj_allow_nan_sym;
89
89
  VALUE oj_array_class_sym;
90
90
  VALUE oj_create_additions_sym;
91
+ VALUE oj_decimal_class_sym;
91
92
  VALUE oj_hash_class_sym;
92
93
  VALUE oj_indent_sym;
93
94
  VALUE oj_object_class_sym;
@@ -581,6 +582,19 @@ oj_parse_options(VALUE ropts, Options copts) {
581
582
  rb_raise(rb_eArgError, ":bigdecimal_load must be :bigdecimal, :float, or :auto.");
582
583
  }
583
584
  }
585
+ if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_decimal_class_sym)) {
586
+ v = rb_hash_lookup(ropts, oj_decimal_class_sym);
587
+ if (rb_cFloat == v) {
588
+ copts->bigdec_load = FloatDec;
589
+ } else if (oj_bigdecimal_class == v) {
590
+ copts->bigdec_load = BigDec;
591
+ } else if (Qnil == v) {
592
+ copts->bigdec_load = AutoDec;
593
+ } else {
594
+ rb_raise(rb_eArgError, ":decimal_class must be BigDecimal, Float, or nil.");
595
+ }
596
+ }
597
+
584
598
  if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, create_id_sym)) {
585
599
  v = rb_hash_lookup(ropts, create_id_sym);
586
600
  if (Qnil == v) {
@@ -1674,6 +1688,7 @@ Init_oj() {
1674
1688
  oj_array_nl_sym = ID2SYM(rb_intern("array_nl")); rb_gc_register_address(&oj_array_nl_sym);
1675
1689
  oj_ascii_only_sym = ID2SYM(rb_intern("ascii_only")); rb_gc_register_address(&oj_ascii_only_sym);
1676
1690
  oj_create_additions_sym = ID2SYM(rb_intern("create_additions"));rb_gc_register_address(&oj_create_additions_sym);
1691
+ oj_decimal_class_sym = ID2SYM(rb_intern("decimal_class")); rb_gc_register_address(&oj_decimal_class_sym);
1677
1692
  oj_hash_class_sym = ID2SYM(rb_intern("hash_class")); rb_gc_register_address(&oj_hash_class_sym);
1678
1693
  oj_indent_sym = ID2SYM(rb_intern("indent")); rb_gc_register_address(&oj_indent_sym);
1679
1694
  oj_max_nesting_sym = ID2SYM(rb_intern("max_nesting")); rb_gc_register_address(&oj_max_nesting_sym);
@@ -310,6 +310,7 @@ extern VALUE oj_array_class_sym;
310
310
  extern VALUE oj_array_nl_sym;
311
311
  extern VALUE oj_ascii_only_sym;
312
312
  extern VALUE oj_create_additions_sym;
313
+ extern VALUE oj_decimal_class_sym;
313
314
  extern VALUE oj_hash_class_sym;
314
315
  extern VALUE oj_indent_sym;
315
316
  extern VALUE oj_max_nesting_sym;
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '3.10.16'
4
+ VERSION = '3.10.17'
5
5
  end
@@ -94,7 +94,7 @@ information.
94
94
  | :array_nl | String | | | | | | x | |
95
95
  | :ascii_only | Boolean | x | x | 2 | 2 | x | x | |
96
96
  | :auto_define | Boolean | | | | | x | x | |
97
- | :bigdecimal_as_decimal | Boolean | | | | 3 | x | x | |
97
+ | :bigdecimal_as_decimal | Boolean | | | x | 3 | x | x | |
98
98
  | :bigdecimal_load | Boolean | | | | | | x | |
99
99
  | :circular | Boolean | x | x | x | x | x | x | |
100
100
  | :class_cache | Boolean | | | | | x | x | |
@@ -66,6 +66,10 @@ Determines how to load decimals.
66
66
 
67
67
  - `:auto` the most precise for the number of digits is used.
68
68
 
69
+ This can also be set with `:decimal_class` when used as a load or
70
+ parse option to match the JSON gem. In that case either `Float`,
71
+ `BigDecimal`, or `nil` can be provided.
72
+
69
73
  ### :circular [Boolean]
70
74
 
71
75
  Detect circular references while dumping. In :compat mode raise a
@@ -277,12 +277,19 @@ class CompatJuice < Minitest::Test
277
277
  # BigDecimal
278
278
  def test_bigdecimal
279
279
  # BigDecimals are dumped as strings and can not be restored to the
280
- # original value.
280
+ # original value without using an undocumented feature of the JSON gem.
281
281
  json = Oj.dump(BigDecimal('3.14159265358979323846'))
282
282
  # 2.4.0 changes the exponent to lowercase
283
283
  assert_equal('"0.314159265358979323846e1"', json.downcase)
284
284
  end
285
285
 
286
+ def test_bigdecimal_load
287
+ big = BigDecimal('3.14159265358979323846')
288
+ # :decimal_class is the undocumented feature.
289
+ json = Oj.load('3.14159265358979323846', mode: :compat, decimal_class: BigDecimal)
290
+ assert_equal(big, json)
291
+ end
292
+
286
293
  def test_infinity
287
294
  assert_raises(Oj::ParseError) { Oj.load('Infinity', :mode => :strict) }
288
295
  x = Oj.load('Infinity', :mode => :compat)
@@ -23,4 +23,13 @@ class RailsJuice < Minitest::Test
23
23
  assert_equal('0.123e3', json.downcase)
24
24
  end
25
25
 
26
+ def test_invalid_encoding
27
+ assert_raises(EncodingError) {
28
+ Oj.dump("\"\xf3j", mode: :rails)
29
+ }
30
+ assert_raises(EncodingError) {
31
+ Oj.dump("\xf3j", mode: :rails)
32
+ }
33
+ end
34
+
26
35
  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.10.16
4
+ version: 3.10.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-10 00:00:00.000000000 Z
11
+ date: 2020-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -265,7 +265,7 @@ metadata:
265
265
  homepage_uri: http://www.ohler.com/oj/
266
266
  source_code_uri: https://github.com/ohler55/oj
267
267
  wiki_uri: https://github.com/ohler55/oj/wiki
268
- post_install_message:
268
+ post_install_message:
269
269
  rdoc_options:
270
270
  - "--title"
271
271
  - Oj
@@ -285,7 +285,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
285
285
  version: '0'
286
286
  requirements: []
287
287
  rubygems_version: 3.1.2
288
- signing_key:
288
+ signing_key:
289
289
  specification_version: 4
290
290
  summary: A fast JSON parser and serializer.
291
291
  test_files: