oj 3.12.2 → 3.12.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89a87064f6668864cec4ab0c43892b4950c8ef7a9f7ea3da22f459af5bcd0083
4
- data.tar.gz: bc078e8b28d710a2194e2943343db687ec15d35d2556efff2f9e46936f3bb0b0
3
+ metadata.gz: 125ec67a260b09db65d47add3487d46c55ee805ebaa721d3bf841ef27fe3ade7
4
+ data.tar.gz: a1cd38c40217e8c5ffd24f8759ff00f0d6770dcab56a736bd0c4ba52d5f3a03a
5
5
  SHA512:
6
- metadata.gz: 95cb66c13e871bf85a351348b0c458c53fe2cd4057886396a62b763386fafe5d99c97143151808eb70dd526b8f13740cd43e266805c6b3454eac1be6940897db
7
- data.tar.gz: '09e24c7ec1daf2c10850b071eb4060aed265db662a92a78439eee1746b22437396f3358f82d982e040874c0916af3c38def244e03416e4971c2c5649823ff7c2'
6
+ metadata.gz: 63496098643062caf40131db78e9ef1ccd75f9fd56f32e49538ccb6e005a42dcc86198d202562edc0b149b7ee649492376584d21554423846ac078efec700899
7
+ data.tar.gz: 9d619857d1f9f217d5e7c27c8a999013516680de8e28bc835ee2e267e60eee5dacde1a69e2de694d99d8caf21eb7e5c0c5a51ab9407d03b509423af894913985
data/ext/oj/mimic_json.c CHANGED
@@ -464,19 +464,19 @@ oj_mimic_pretty_generate(int argc, VALUE *argv, VALUE self) {
464
464
  } else {
465
465
  h = argv[1];
466
466
  }
467
- if (Qfalse == rb_funcall(h, oj_has_key_id, 1, oj_indent_sym)) {
467
+ if (!oj_hash_has_key(h, oj_indent_sym)) {
468
468
  rb_hash_aset(h, oj_indent_sym, rb_str_new2(" "));
469
469
  }
470
- if (Qfalse == rb_funcall(h, oj_has_key_id, 1, oj_space_before_sym)) {
470
+ if (!oj_hash_has_key(h, oj_space_before_sym)) {
471
471
  rb_hash_aset(h, oj_space_before_sym, rb_str_new2(""));
472
472
  }
473
- if (Qfalse == rb_funcall(h, oj_has_key_id, 1, oj_space_sym)) {
473
+ if (!oj_hash_has_key(h, oj_space_sym)) {
474
474
  rb_hash_aset(h, oj_space_sym, rb_str_new2(" "));
475
475
  }
476
- if (Qfalse == rb_funcall(h, oj_has_key_id, 1, oj_object_nl_sym)) {
476
+ if (!oj_hash_has_key(h, oj_object_nl_sym)) {
477
477
  rb_hash_aset(h, oj_object_nl_sym, rb_str_new2("\n"));
478
478
  }
479
- if (Qfalse == rb_funcall(h, oj_has_key_id, 1, oj_array_nl_sym)) {
479
+ if (!oj_hash_has_key(h, oj_array_nl_sym)) {
480
480
  rb_hash_aset(h, oj_array_nl_sym, rb_str_new2("\n"));
481
481
  }
482
482
  if (Qundef == state_class) {
@@ -548,7 +548,7 @@ static VALUE mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
548
548
  pi.options.allow_nan = (Qtrue == v) ? Yes : No;
549
549
  }
550
550
 
551
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_hash_class_sym)) {
551
+ if (oj_hash_has_key(ropts, oj_hash_class_sym)) {
552
552
  if (Qnil == (v = rb_hash_lookup(ropts, oj_hash_class_sym))) {
553
553
  pi.options.hash_class = Qnil;
554
554
  } else {
@@ -556,7 +556,7 @@ static VALUE mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
556
556
  pi.options.hash_class = v;
557
557
  }
558
558
  }
559
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_object_class_sym)) {
559
+ if (oj_hash_has_key(ropts, oj_object_class_sym)) {
560
560
  if (Qnil == (v = rb_hash_lookup(ropts, oj_object_class_sym))) {
561
561
  pi.options.hash_class = Qnil;
562
562
  } else {
@@ -564,7 +564,7 @@ static VALUE mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
564
564
  pi.options.hash_class = v;
565
565
  }
566
566
  }
567
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_array_class_sym)) {
567
+ if (oj_hash_has_key(ropts, oj_array_class_sym)) {
568
568
  if (Qnil == (v = rb_hash_lookup(ropts, oj_array_class_sym))) {
569
569
  pi.options.array_class = Qnil;
570
570
  } else {
@@ -572,7 +572,7 @@ static VALUE mimic_parse_core(int argc, VALUE *argv, VALUE self, bool bang) {
572
572
  pi.options.array_class = v;
573
573
  }
574
574
  }
575
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_decimal_class_sym)) {
575
+ if (oj_hash_has_key(ropts, oj_decimal_class_sym)) {
576
576
  pi.options.compat_bigdec = (oj_bigdecimal_class ==
577
577
  rb_hash_lookup(ropts, oj_decimal_class_sym));
578
578
  }
data/ext/oj/oj.c CHANGED
@@ -40,7 +40,6 @@ ID oj_error_id;
40
40
  ID oj_file_id;
41
41
  ID oj_fileno_id;
42
42
  ID oj_ftype_id;
43
- ID oj_has_key_id;
44
43
  ID oj_hash_end_id;
45
44
  ID oj_hash_key_id;
46
45
  ID oj_hash_set_id;
@@ -582,6 +581,14 @@ static VALUE set_def_opts(VALUE self, VALUE opts) {
582
581
  return Qnil;
583
582
  }
584
583
 
584
+ bool oj_hash_has_key(VALUE hash, VALUE key)
585
+ {
586
+ if (Qundef == rb_hash_lookup2(hash, key, Qundef)) {
587
+ return false;
588
+ }
589
+ return true;
590
+ }
591
+
585
592
  void oj_parse_options(VALUE ropts, Options copts) {
586
593
  struct _yesNoOpt ynos[] = {{circular_sym, &copts->circular},
587
594
  {auto_define_sym, &copts->auto_define},
@@ -612,7 +619,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
612
619
  if (T_HASH != rb_type(ropts)) {
613
620
  return;
614
621
  }
615
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_indent_sym)) {
622
+ if (oj_hash_has_key(ropts, oj_indent_sym)) {
616
623
  v = rb_hash_lookup(ropts, oj_indent_sym);
617
624
  switch (rb_type(v)) {
618
625
  case T_NIL:
@@ -773,7 +780,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
773
780
  if (Qnil != (v = rb_hash_lookup(ropts, compat_bigdecimal_sym))) {
774
781
  copts->compat_bigdec = (Qtrue == v);
775
782
  }
776
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_decimal_class_sym)) {
783
+ if (oj_hash_has_key(ropts, oj_decimal_class_sym)) {
777
784
  v = rb_hash_lookup(ropts, oj_decimal_class_sym);
778
785
  if (rb_cFloat == v) {
779
786
  copts->compat_bigdec = false;
@@ -783,7 +790,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
783
790
  rb_raise(rb_eArgError, ":decimal_class must be BigDecimal or Float.");
784
791
  }
785
792
  }
786
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, create_id_sym)) {
793
+ if (oj_hash_has_key(ropts, create_id_sym)) {
787
794
  v = rb_hash_lookup(ropts, create_id_sym);
788
795
  if (Qnil == v) {
789
796
  if (oj_json_class != oj_default_options.create_id && NULL != copts->create_id) {
@@ -805,7 +812,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
805
812
  }
806
813
  }
807
814
  for (o = ynos; 0 != o->attr; o++) {
808
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, o->sym)) {
815
+ if (oj_hash_has_key(ropts, o->sym)) {
809
816
  v = rb_hash_lookup(ropts, o->sym);
810
817
  if (Qnil == v) {
811
818
  *o->attr = NotSet;
@@ -820,7 +827,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
820
827
  }
821
828
  }
822
829
  }
823
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_space_sym)) {
830
+ if (oj_hash_has_key(ropts, oj_space_sym)) {
824
831
  if (Qnil == (v = rb_hash_lookup(ropts, oj_space_sym))) {
825
832
  copts->dump_opts.after_size = 0;
826
833
  *copts->dump_opts.after_sep = '\0';
@@ -835,7 +842,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
835
842
  copts->dump_opts.after_size = (uint8_t)len;
836
843
  }
837
844
  }
838
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_space_before_sym)) {
845
+ if (oj_hash_has_key(ropts, oj_space_before_sym)) {
839
846
  if (Qnil == (v = rb_hash_lookup(ropts, oj_space_before_sym))) {
840
847
  copts->dump_opts.before_size = 0;
841
848
  *copts->dump_opts.before_sep = '\0';
@@ -850,7 +857,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
850
857
  copts->dump_opts.before_size = (uint8_t)len;
851
858
  }
852
859
  }
853
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_object_nl_sym)) {
860
+ if (oj_hash_has_key(ropts, oj_object_nl_sym)) {
854
861
  if (Qnil == (v = rb_hash_lookup(ropts, oj_object_nl_sym))) {
855
862
  copts->dump_opts.hash_size = 0;
856
863
  *copts->dump_opts.hash_nl = '\0';
@@ -865,7 +872,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
865
872
  copts->dump_opts.hash_size = (uint8_t)len;
866
873
  }
867
874
  }
868
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_array_nl_sym)) {
875
+ if (oj_hash_has_key(ropts, oj_array_nl_sym)) {
869
876
  if (Qnil == (v = rb_hash_lookup(ropts, oj_array_nl_sym))) {
870
877
  copts->dump_opts.array_size = 0;
871
878
  *copts->dump_opts.array_nl = '\0';
@@ -914,7 +921,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
914
921
  } else if (Qfalse == v) {
915
922
  copts->escape_mode = JSONEsc;
916
923
  }
917
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_hash_class_sym)) {
924
+ if (oj_hash_has_key(ropts, oj_hash_class_sym)) {
918
925
  if (Qnil == (v = rb_hash_lookup(ropts, oj_hash_class_sym))) {
919
926
  copts->hash_class = Qnil;
920
927
  } else {
@@ -922,7 +929,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
922
929
  copts->hash_class = v;
923
930
  }
924
931
  }
925
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_object_class_sym)) {
932
+ if (oj_hash_has_key(ropts, oj_object_class_sym)) {
926
933
  if (Qnil == (v = rb_hash_lookup(ropts, oj_object_class_sym))) {
927
934
  copts->hash_class = Qnil;
928
935
  } else {
@@ -930,7 +937,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
930
937
  copts->hash_class = v;
931
938
  }
932
939
  }
933
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, oj_array_class_sym)) {
940
+ if (oj_hash_has_key(ropts, oj_array_class_sym)) {
934
941
  if (Qnil == (v = rb_hash_lookup(ropts, oj_array_class_sym))) {
935
942
  copts->array_class = Qnil;
936
943
  } else {
@@ -939,7 +946,7 @@ void oj_parse_options(VALUE ropts, Options copts) {
939
946
  }
940
947
  }
941
948
  oj_parse_opt_match_string(&copts->str_rx, ropts);
942
- if (Qtrue == rb_funcall(ropts, oj_has_key_id, 1, ignore_sym)) {
949
+ if (oj_hash_has_key(ropts, ignore_sym)) {
943
950
  xfree(copts->ignore);
944
951
  copts->ignore = NULL;
945
952
  if (Qnil != (v = rb_hash_lookup(ropts, ignore_sym))) {
@@ -1237,6 +1244,38 @@ static VALUE safe_load(VALUE self, VALUE doc) {
1237
1244
  * - *io* [_IO__|_String_] IO Object to read from
1238
1245
  */
1239
1246
 
1247
+ struct dump_arg {
1248
+ struct _out *out;
1249
+ struct _options *copts;
1250
+ int argc;
1251
+ VALUE *argv;
1252
+ };
1253
+
1254
+ static VALUE dump_body(VALUE a)
1255
+ {
1256
+ volatile struct dump_arg *arg = (void *)a;
1257
+ VALUE rstr;
1258
+
1259
+ oj_dump_obj_to_json_using_params(*arg->argv, arg->copts, arg->out, arg->argc - 1, arg->argv + 1);
1260
+ if (0 == arg->out->buf) {
1261
+ rb_raise(rb_eNoMemError, "Not enough memory.");
1262
+ }
1263
+ rstr = rb_str_new2(arg->out->buf);
1264
+ rstr = oj_encode(rstr);
1265
+
1266
+ return rstr;
1267
+ }
1268
+
1269
+ static VALUE dump_ensure(VALUE a)
1270
+ {
1271
+ volatile struct dump_arg *arg = (void *)a;
1272
+
1273
+ if (arg->out->allocated) {
1274
+ xfree(arg->out->buf);
1275
+ }
1276
+ return Qnil;
1277
+ }
1278
+
1240
1279
  /* Document-method: dump
1241
1280
  * call-seq: dump(obj, options={})
1242
1281
  *
@@ -1246,9 +1285,9 @@ static VALUE safe_load(VALUE self, VALUE doc) {
1246
1285
  */
1247
1286
  static VALUE dump(int argc, VALUE *argv, VALUE self) {
1248
1287
  char buf[4096];
1288
+ struct dump_arg arg;
1249
1289
  struct _out out;
1250
1290
  struct _options copts = oj_default_options;
1251
- VALUE rstr;
1252
1291
 
1253
1292
  if (1 > argc) {
1254
1293
  rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
@@ -1262,21 +1301,18 @@ static VALUE dump(int argc, VALUE *argv, VALUE self) {
1262
1301
  if (CompatMode == copts.mode && copts.escape_mode != ASCIIEsc) {
1263
1302
  copts.escape_mode = JSONEsc;
1264
1303
  }
1265
- out.buf = buf;
1266
- out.end = buf + sizeof(buf) - 10;
1267
- out.allocated = false;
1268
- out.omit_nil = copts.dump_opts.omit_nil;
1269
- out.caller = CALLER_DUMP;
1270
- oj_dump_obj_to_json_using_params(*argv, &copts, &out, argc - 1, argv + 1);
1271
- if (0 == out.buf) {
1272
- rb_raise(rb_eNoMemError, "Not enough memory.");
1273
- }
1274
- rstr = rb_str_new2(out.buf);
1275
- rstr = oj_encode(rstr);
1276
- if (out.allocated) {
1277
- xfree(out.buf);
1278
- }
1279
- return rstr;
1304
+ arg.out = &out;
1305
+ arg.copts = &copts;
1306
+ arg.argc = argc;
1307
+ arg.argv = argv;
1308
+
1309
+ arg.out->buf = buf;
1310
+ arg.out->end = buf + sizeof(buf) - 10;
1311
+ arg.out->allocated = false;
1312
+ arg.out->omit_nil = copts.dump_opts.omit_nil;
1313
+ arg.out->caller = CALLER_DUMP;
1314
+
1315
+ return rb_ensure(dump_body, (VALUE)&arg, dump_ensure, (VALUE)&arg);
1280
1316
  }
1281
1317
 
1282
1318
  /* Document-method: to_json
@@ -1785,7 +1821,6 @@ void Init_oj() {
1785
1821
  oj_file_id = rb_intern("file?");
1786
1822
  oj_fileno_id = rb_intern("fileno");
1787
1823
  oj_ftype_id = rb_intern("ftype");
1788
- oj_has_key_id = rb_intern("has_key?");
1789
1824
  oj_hash_end_id = rb_intern("hash_end");
1790
1825
  oj_hash_key_id = rb_intern("hash_key");
1791
1826
  oj_hash_set_id = rb_intern("hash_set");
data/ext/oj/oj.h CHANGED
@@ -245,6 +245,7 @@ extern VALUE oj_compat_parse_cstr(int argc, VALUE *argv, char *json, size_t len)
245
245
  extern VALUE oj_object_parse_cstr(int argc, VALUE *argv, char *json, size_t len);
246
246
  extern VALUE oj_custom_parse_cstr(int argc, VALUE *argv, char *json, size_t len);
247
247
 
248
+ extern bool oj_hash_has_key(VALUE hash, VALUE key);
248
249
  extern void oj_parse_options(VALUE ropts, Options copts);
249
250
 
250
251
  extern void oj_dump_obj_to_json(VALUE obj, Options copts, Out out);
@@ -327,7 +328,6 @@ extern ID oj_exclude_end_id;
327
328
  extern ID oj_file_id;
328
329
  extern ID oj_fileno_id;
329
330
  extern ID oj_ftype_id;
330
- extern ID oj_has_key_id;
331
331
  extern ID oj_hash_end_id;
332
332
  extern ID oj_hash_key_id;
333
333
  extern ID oj_hash_set_id;
data/lib/oj/mimic.rb CHANGED
@@ -24,7 +24,7 @@ module Oj
24
24
  circular: false,
25
25
  class_cache: false,
26
26
  cache_keys: true,
27
- cache_str: false,
27
+ cache_str: 5,
28
28
  create_additions: false,
29
29
  create_id: "json_class",
30
30
  empty_string: false,
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.12.2'
4
+ VERSION = '3.12.3'
5
5
  end
data/pages/Modes.md CHANGED
@@ -97,6 +97,8 @@ information.
97
97
  | :bigdecimal_as_decimal | Boolean | | | | 3 | x | x | |
98
98
  | :bigdecimal_load | Boolean | | | | | | x | |
99
99
  | :compat_bigdecimal | Boolean | | | x | | | x | |
100
+ | :cache_keys | Boolean | x | x | x | x | | x | |
101
+ | :cache_strings | Fixnum | x | x | x | x | | x | |
100
102
  | :circular | Boolean | x | x | x | x | x | x | |
101
103
  | :class_cache | Boolean | | | | | x | x | |
102
104
  | :create_additions | Boolean | | | x | x | | x | |
data/pages/Options.md CHANGED
@@ -70,13 +70,17 @@ This can also be set with `:decimal_class` when used as a load or
70
70
  parse option to match the JSON gem. In that case either `Float`,
71
71
  `BigDecimal`, or `nil` can be provided.
72
72
 
73
- ### :compat_bigdecimal [Boolean]
73
+ ### :cache_keys [Boolean]
74
74
 
75
- Determines how to load decimals when in `:compat` mode.
75
+ If true Hash keys are cached or interned. There are trade-offs with
76
+ caching keys. Large caches will use more memory and in extreme cases
77
+ (like over a million) the cache may be slower than not using
78
+ it. Repeated parsing of similar JSON docs is where cache_keys shines.
76
79
 
77
- - `true` convert all decimal numbers to BigDecimal.
80
+ ### :cache_strings [Int]
78
81
 
79
- - `false` convert all decimal numbers to Float.
82
+ Shorter strings can be cached for better performance. A limit,
83
+ cache_strings, defines the upper limit on what strings are cached.
80
84
 
81
85
  ### :circular [Boolean]
82
86
 
@@ -90,6 +94,14 @@ recreate the looped references on load.
90
94
  Cache classes for faster parsing. This option should not be used if
91
95
  dynamically modifying classes or reloading classes then don't use this.
92
96
 
97
+ ### :compat_bigdecimal [Boolean]
98
+
99
+ Determines how to load decimals when in `:compat` mode.
100
+
101
+ - `true` convert all decimal numbers to BigDecimal.
102
+
103
+ - `false` convert all decimal numbers to Float.
104
+
93
105
  ### :create_additions
94
106
 
95
107
  A flag indicating that the :create_id key, when encountered during parsing,
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.12.2
4
+ version: 3.12.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-25 00:00:00.000000000 Z
11
+ date: 2021-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler