oj 3.17.3 → 3.17.8

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -0
  3. data/ext/oj/cache.c +72 -39
  4. data/ext/oj/cache.h +5 -0
  5. data/ext/oj/circarray.c +6 -2
  6. data/ext/oj/code.c +38 -28
  7. data/ext/oj/code.h +2 -0
  8. data/ext/oj/compat.c +63 -31
  9. data/ext/oj/custom.c +43 -8
  10. data/ext/oj/dump.c +120 -41
  11. data/ext/oj/dump.h +10 -0
  12. data/ext/oj/dump_compat.c +19 -15
  13. data/ext/oj/dump_object.c +38 -12
  14. data/ext/oj/dump_strict.c +2 -1
  15. data/ext/oj/extconf.rb +7 -0
  16. data/ext/oj/fast.c +132 -72
  17. data/ext/oj/intern.c +80 -77
  18. data/ext/oj/mimic_json.c +5 -29
  19. data/ext/oj/object.c +47 -17
  20. data/ext/oj/odd.c +6 -2
  21. data/ext/oj/oj.c +393 -44
  22. data/ext/oj/oj.h +10 -3
  23. data/ext/oj/parse.c +75 -7
  24. data/ext/oj/parse.h +23 -2
  25. data/ext/oj/parser.c +219 -71
  26. data/ext/oj/parser.h +1 -1
  27. data/ext/oj/rails.c +188 -45
  28. data/ext/oj/reader.c +22 -6
  29. data/ext/oj/reader.h +1 -0
  30. data/ext/oj/resolve.c +16 -6
  31. data/ext/oj/resolve.h +9 -0
  32. data/ext/oj/rxclass.c +16 -0
  33. data/ext/oj/rxclass.h +1 -0
  34. data/ext/oj/safe.c +62 -48
  35. data/ext/oj/safe.h +14 -2
  36. data/ext/oj/saj.c +60 -17
  37. data/ext/oj/saj2.c +44 -28
  38. data/ext/oj/simd.h +5 -3
  39. data/ext/oj/sparse.c +23 -6
  40. data/ext/oj/stream_writer.c +19 -1
  41. data/ext/oj/strict.c +42 -4
  42. data/ext/oj/string_writer.c +40 -10
  43. data/ext/oj/usual.c +17 -5
  44. data/ext/oj/util.c +27 -0
  45. data/ext/oj/util.h +2 -1
  46. data/ext/oj/val_stack.c +11 -22
  47. data/ext/oj/val_stack.h +70 -30
  48. data/ext/oj/wab.c +17 -26
  49. data/lib/oj/generic_object.rb +82 -0
  50. data/lib/oj/json.rb +1 -71
  51. data/lib/oj/mimic.rb +0 -1
  52. data/lib/oj/version.rb +1 -1
  53. data/pages/Compatibility.md +1 -1
  54. data/pages/Custom.md +2 -3
  55. data/pages/InstallOptions.md +8 -6
  56. data/pages/Modes.md +1 -1
  57. data/pages/Options.md +5 -6
  58. data/pages/Parser.md +2 -2
  59. data/pages/Rails.md +6 -0
  60. data/pages/Security.md +21 -7
  61. metadata +17 -16
data/ext/oj/custom.c CHANGED
@@ -492,6 +492,7 @@ static VALUE dump_common(VALUE obj, int depth, Out out) {
492
492
  rs = rb_funcall2(obj, oj_to_json_id, out->argc, out->argv);
493
493
  }
494
494
  TRACE(out->opts->trace, "to_json", obj, depth + 1, TraceRubyOut);
495
+ StringValue(rs);
495
496
  s = RSTRING_PTR(rs);
496
497
  len = RSTRING_LEN(rs);
497
498
 
@@ -565,7 +566,7 @@ static int dump_attr_cb(ID key, VALUE value, VALUE ov) {
565
566
  } else if (Yes == out->opts->ignore_under && '@' == *attr && '_' == attr[1]) {
566
567
  return ST_CONTINUE;
567
568
  }
568
- if (0 == strcmp("bt", attr) || 0 == strcmp("mesg", attr)) {
569
+ if ('@' != *attr && (0 == strcmp("bt", attr) || 0 == strcmp("mesg", attr) || 0 == strcmp("bt_locations", attr))) {
569
570
  return ST_CONTINUE;
570
571
  }
571
572
  assure_size(out, size);
@@ -640,7 +641,9 @@ static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out)
640
641
  if (rb_obj_is_kind_of(obj, rb_eException)) {
641
642
  volatile VALUE rv;
642
643
 
643
- if (',' != *(out->cur - 1)) {
644
+ // An exception that was never raised has no instance variables, so
645
+ // without the class name nothing has been written since the '{'.
646
+ if (',' != *(out->cur - 1) && '{' != *(out->cur - 1)) {
644
647
  *out->cur++ = ',';
645
648
  }
646
649
  // message
@@ -705,6 +708,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
705
708
  cnt--;
706
709
  for (i = 0; i <= cnt; i++) {
707
710
  if (out->opts->dump_opts.use) {
711
+ assure_size(out, size);
708
712
  if (0 < out->opts->dump_opts.array_size) {
709
713
  APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
710
714
  }
@@ -722,7 +726,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
722
726
  *out->cur++ = ',';
723
727
  }
724
728
  }
725
- size = depth * out->indent + 1;
729
+ size = indent_len(out, depth, out->opts->dump_opts.array_size) + 1;
726
730
  assure_size(out, size);
727
731
  if (out->opts->dump_opts.use) {
728
732
  if (0 < out->opts->dump_opts.array_size) {
@@ -814,6 +818,18 @@ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
814
818
  oj_dump_custom_val(v, d3, out, true);
815
819
  *out->cur++ = ',';
816
820
  }
821
+ if (2 == cnt && rb_obj_is_kind_of(obj, rb_cRange)) {
822
+ VALUE beg;
823
+ VALUE end;
824
+ int excl;
825
+
826
+ rb_range_values(obj, &beg, &end, &excl);
827
+ assure_size(out, size + 7);
828
+ fill_indent(out, d3);
829
+ APPEND_CHARS(out->cur, "\"excl\":", 7);
830
+ oj_dump_custom_val(excl ? Qtrue : Qfalse, d3, out, true);
831
+ *out->cur++ = ',';
832
+ }
817
833
  out->cur--;
818
834
  *out->cur++ = '}';
819
835
  *out->cur = '\0';
@@ -896,12 +912,15 @@ void oj_dump_custom_val(VALUE obj, int depth, Out out, bool as_ok) {
896
912
 
897
913
  static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
898
914
  const char *key = kval->key;
899
- int klen = kval->klen;
915
+ size_t klen = kval->klen;
900
916
  Val parent = stack_peek(&pi->stack);
901
917
 
902
918
  if (Qundef == kval->key_val && Yes == pi->options.create_ok && NULL != pi->options.create_id &&
903
- *pi->options.create_id == *key && (int)pi->options.create_id_len == klen &&
919
+ *pi->options.create_id == *key && (size_t)pi->options.create_id_len == klen &&
904
920
  0 == strncmp(pi->options.create_id, key, klen)) {
921
+ if (oj_is_backtrace_name(str, len)) {
922
+ return;
923
+ }
905
924
  parent->clas = oj_name2class(pi, str, len, false, rb_eArgError);
906
925
  if (2 == klen && '^' == *key && 'o' == key[1]) {
907
926
  if (Qundef != parent->clas) {
@@ -1012,7 +1031,7 @@ static void array_append_num(ParseInfo pi, NumInfo ni) {
1012
1031
  Val parent = stack_peek(&pi->stack);
1013
1032
  volatile VALUE rval = oj_num_as_value(ni);
1014
1033
 
1015
- rb_ary_push(parent->val, rval);
1034
+ oj_compat_array_append(parent->val, rval);
1016
1035
  TRACE_PARSE_CALL(pi->options.trace, "append_number", pi, rval);
1017
1036
  }
1018
1037
 
@@ -1023,16 +1042,32 @@ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const c
1023
1042
  VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, len);
1024
1043
 
1025
1044
  if (Qnil != clas) {
1026
- rb_ary_push(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));
1045
+ oj_compat_array_append(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));
1027
1046
  return;
1028
1047
  }
1029
1048
  }
1030
- rb_ary_push(stack_peek(&pi->stack)->val, rstr);
1049
+ oj_compat_array_append(stack_peek(&pi->stack)->val, rstr);
1031
1050
  TRACE_PARSE_CALL(pi->options.trace, "append_string", pi, rstr);
1032
1051
  }
1033
1052
 
1053
+ // Compat mode defers Hash creation to its end_hash (bulk insert from the
1054
+ // buffered pairs) but the custom callbacks below fill the Hash as they go,
1055
+ // so hashes must exist from the opening brace here.
1056
+ static VALUE start_hash(ParseInfo pi) {
1057
+ volatile VALUE h;
1058
+
1059
+ if (Qnil != pi->options.hash_class) {
1060
+ h = rb_class_new_instance(0, NULL, pi->options.hash_class);
1061
+ } else {
1062
+ h = rb_hash_new();
1063
+ }
1064
+ TRACE_PARSE_IN(pi->options.trace, "start_hash", pi);
1065
+ return h;
1066
+ }
1067
+
1034
1068
  void oj_set_custom_callbacks(ParseInfo pi) {
1035
1069
  oj_set_compat_callbacks(pi);
1070
+ pi->start_hash = start_hash;
1036
1071
  pi->hash_set_cstr = hash_set_cstr;
1037
1072
  pi->end_hash = end_hash;
1038
1073
  pi->hash_set_num = hash_set_num;
data/ext/oj/dump.c CHANGED
@@ -587,6 +587,9 @@ long oj_check_circular(VALUE obj, Out out) {
587
587
  slot_t *slot;
588
588
 
589
589
  if (Yes == out->opts->circular) {
590
+ if (NULL == out->circ_cache) {
591
+ oj_cache8_new(&out->circ_cache);
592
+ }
590
593
  if (0 == (id = oj_cache8_get(out->circ_cache, obj, &slot))) {
591
594
  out->circ_cnt++;
592
595
  id = out->circ_cnt;
@@ -831,28 +834,61 @@ void oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int arg
831
834
  *out->cur = '\0';
832
835
  if (Yes == copts->circular) {
833
836
  oj_cache8_delete(out->circ_cache);
837
+ out->circ_cache = NULL;
834
838
  }
835
839
  }
836
840
 
837
- void oj_write_obj_to_file(VALUE obj, const char *path, Options copts) {
838
- struct _out out;
839
- size_t size;
840
- FILE *f;
841
- int ok;
841
+ struct dump_str_arg {
842
+ VALUE obj;
843
+ Options copts;
844
+ Out out;
845
+ int argc;
846
+ VALUE *argv;
847
+ };
842
848
 
843
- oj_out_init(&out);
849
+ static VALUE dump_str_body(VALUE a) {
850
+ struct dump_str_arg *arg = (struct dump_str_arg *)a;
844
851
 
845
- out.omit_nil = copts->dump_opts.omit_nil;
846
- oj_dump_obj_to_json(obj, copts, &out);
847
- size = out.cur - out.buf;
848
- if (0 == (f = fopen(path, "w"))) {
849
- oj_out_free(&out);
850
- rb_raise(rb_eIOError, "%s", strerror(errno));
852
+ oj_dump_obj_to_json_using_params(arg->obj, arg->copts, arg->out, arg->argc, arg->argv);
853
+ if (0 == arg->out->buf) {
854
+ rb_raise(rb_eNoMemError, "Not enough memory.");
851
855
  }
852
- ok = (size == fwrite(out.buf, 1, size, f));
856
+ return rb_utf8_str_new_cstr(arg->out->buf);
857
+ }
853
858
 
854
- oj_out_free(&out);
859
+ static VALUE out_free_ensure(VALUE out) {
860
+ oj_out_free((Out)out);
861
+
862
+ return Qnil;
863
+ }
864
+
865
+ VALUE oj_dump_obj_to_str(VALUE obj, Options copts, Out out, int argc, VALUE *argv) {
866
+ struct dump_str_arg arg = {obj, copts, out, argc, argv};
867
+
868
+ return rb_ensure(dump_str_body, (VALUE)&arg, out_free_ensure, (VALUE)out);
869
+ }
855
870
 
871
+ struct write_arg {
872
+ VALUE obj;
873
+ VALUE stream;
874
+ const char *path;
875
+ Options copts;
876
+ Out out;
877
+ };
878
+
879
+ static VALUE write_file_body(VALUE a) {
880
+ struct write_arg *arg = (struct write_arg *)a;
881
+ Out out = arg->out;
882
+ size_t size;
883
+ FILE *f;
884
+ int ok;
885
+
886
+ oj_dump_obj_to_json(arg->obj, arg->copts, out);
887
+ size = out->cur - out->buf;
888
+ if (0 == (f = fopen(arg->path, "w"))) {
889
+ rb_raise(rb_eIOError, "%s", strerror(errno));
890
+ }
891
+ ok = (size == fwrite(out->buf, 1, size, f));
856
892
  if (!ok) {
857
893
  int err = ferror(f);
858
894
  fclose(f);
@@ -860,6 +896,18 @@ void oj_write_obj_to_file(VALUE obj, const char *path, Options copts) {
860
896
  rb_raise(rb_eIOError, "Write failed. [%d:%s]", err, strerror(err));
861
897
  }
862
898
  fclose(f);
899
+
900
+ return Qnil;
901
+ }
902
+
903
+ void oj_write_obj_to_file(VALUE obj, const char *path, Options copts) {
904
+ struct _out out;
905
+ struct write_arg arg = {obj, Qnil, path, copts, &out};
906
+
907
+ oj_out_init(&out);
908
+
909
+ out.omit_nil = copts->dump_opts.omit_nil;
910
+ rb_ensure(write_file_body, (VALUE)&arg, out_free_ensure, (VALUE)&out);
863
911
  }
864
912
 
865
913
  #if !IS_WINDOWS
@@ -879,22 +927,21 @@ static void write_ready(int fd) {
879
927
  }
880
928
  #endif
881
929
 
882
- void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts) {
883
- struct _out out;
884
- ssize_t size;
885
- VALUE clas = rb_obj_class(stream);
930
+ static VALUE write_stream_body(VALUE a) {
931
+ struct write_arg *arg = (struct write_arg *)a;
932
+ Out out = arg->out;
933
+ VALUE stream = arg->stream;
934
+ ssize_t size;
935
+ VALUE clas = rb_obj_class(stream);
886
936
  #if !IS_WINDOWS
887
937
  int fd;
888
938
  VALUE s;
889
939
  #endif
890
940
 
891
- oj_out_init(&out);
892
-
893
- out.omit_nil = copts->dump_opts.omit_nil;
894
- oj_dump_obj_to_json(obj, copts, &out);
895
- size = out.cur - out.buf;
941
+ oj_dump_obj_to_json(arg->obj, arg->copts, out);
942
+ size = out->cur - out->buf;
896
943
  if (oj_stringio_class == clas) {
897
- rb_funcall(stream, oj_write_id, 1, rb_str_new(out.buf, size));
944
+ rb_funcall(stream, oj_write_id, 1, rb_str_new(out->buf, size));
898
945
  #if !IS_WINDOWS
899
946
  } else if (rb_respond_to(stream, oj_fileno_id) && Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) &&
900
947
  0 != (fd = FIX2INT(s))) {
@@ -902,7 +949,7 @@ void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts) {
902
949
  ssize_t total = 0;
903
950
 
904
951
  while (true) {
905
- if (0 > (cnt = write(fd, out.buf + total, size - total))) {
952
+ if (0 > (cnt = write(fd, out->buf + total, size - total))) {
906
953
  if (EAGAIN != errno) {
907
954
  rb_raise(rb_eIOError, "write failed. %d %s.", errno, strerror(errno));
908
955
  break;
@@ -917,22 +964,32 @@ void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts) {
917
964
  }
918
965
  #endif
919
966
  } else if (rb_respond_to(stream, oj_write_id)) {
920
- rb_funcall(stream, oj_write_id, 1, rb_str_new(out.buf, size));
967
+ rb_funcall(stream, oj_write_id, 1, rb_str_new(out->buf, size));
921
968
  } else {
922
- oj_out_free(&out);
923
969
  rb_raise(rb_eArgError, "to_stream() expected an IO Object.");
924
970
  }
925
- oj_out_free(&out);
971
+ return Qnil;
972
+ }
973
+
974
+ void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts) {
975
+ struct _out out;
976
+ struct write_arg arg = {obj, stream, NULL, copts, &out};
977
+
978
+ oj_out_init(&out);
979
+
980
+ out.omit_nil = copts->dump_opts.omit_nil;
981
+ rb_ensure(write_stream_body, (VALUE)&arg, out_free_ensure, (VALUE)&out);
926
982
  }
927
983
 
928
984
  void oj_dump_str(VALUE obj, int depth, Out out, bool as_ok) {
929
985
  int idx = RB_ENCODING_GET(obj);
930
986
 
931
- if (oj_utf8_encoding_index != idx) {
987
+ if (oj_utf8_encoding_index != idx && !rb_enc_str_asciionly_p(obj)) {
932
988
  rb_encoding *enc = rb_enc_from_index(idx);
933
989
  obj = rb_str_conv_enc(obj, enc, oj_utf8_encoding);
934
990
  }
935
991
  oj_dump_cstr(RSTRING_PTR(obj), RSTRING_LEN(obj), 0, 0, out);
992
+ RB_GC_GUARD(obj);
936
993
  }
937
994
 
938
995
  void oj_dump_sym(VALUE obj, int depth, Out out, bool as_ok) {
@@ -968,6 +1025,7 @@ void oj_dump_raw_json(VALUE obj, int depth, Out out) {
968
1025
  if (0 < len) {
969
1026
  len--;
970
1027
  }
1028
+ assure_size(out, len + 10);
971
1029
  oj_dump_raw(sw->out.buf, len, out);
972
1030
  } else {
973
1031
  volatile VALUE jv;
@@ -975,6 +1033,7 @@ void oj_dump_raw_json(VALUE obj, int depth, Out out) {
975
1033
  TRACE(out->opts->trace, "raw_json", obj, depth + 1, TraceRubyIn);
976
1034
  jv = rb_funcall(obj, oj_raw_json_id, 2, RB_INT2NUM(depth), RB_INT2NUM(out->indent));
977
1035
  TRACE(out->opts->trace, "raw_json", obj, depth + 1, TraceRubyOut);
1036
+ StringValue(jv);
978
1037
  oj_dump_raw(RSTRING_PTR(jv), (size_t)RSTRING_LEN(jv), out);
979
1038
  }
980
1039
  }
@@ -1456,16 +1515,22 @@ void oj_dump_raw(const char *str, size_t cnt, Out out) {
1456
1515
  }
1457
1516
 
1458
1517
  void oj_out_init(Out out) {
1459
- out->buf = out->stack_buffer;
1460
- out->cur = out->buf;
1461
- out->end = out->buf + sizeof(out->stack_buffer) - BUFFER_EXTRA;
1462
- out->allocated = false;
1518
+ out->buf = out->stack_buffer;
1519
+ out->cur = out->buf;
1520
+ out->end = out->buf + sizeof(out->stack_buffer) - BUFFER_EXTRA;
1521
+ out->allocated = false;
1522
+ out->key_filter_off = false;
1523
+ out->circ_cache = NULL;
1463
1524
  }
1464
1525
 
1465
1526
  void oj_out_free(Out out) {
1466
1527
  if (out->allocated) {
1467
1528
  OJ_R_FREE(out->buf); // TBD
1468
1529
  }
1530
+ if (NULL != out->circ_cache) {
1531
+ oj_cache8_delete(out->circ_cache);
1532
+ out->circ_cache = NULL;
1533
+ }
1469
1534
  }
1470
1535
 
1471
1536
  void oj_grow_out(Out out, size_t len) {
@@ -1731,6 +1796,11 @@ void oj_dump_float(VALUE obj, int depth, Out out, bool as_ok) {
1731
1796
  size_t oj_dump_float_printf(char *buf, size_t blen, VALUE obj, double d, const char *format) {
1732
1797
  size_t cnt = snprintf(buf, blen, format, d);
1733
1798
 
1799
+ // snprintf() returns the length the output would have needed, which is
1800
+ // more than it wrote when the format asks for more room than buf has.
1801
+ if (blen <= cnt) {
1802
+ cnt = blen - 1;
1803
+ }
1734
1804
  // Round off issues at 16 significant digits so check for obvious ones of
1735
1805
  // 0001 and 9999.
1736
1806
  if (17 <= cnt && (0 == strcmp("0001", buf + cnt - 4) || 0 == strcmp("9999", buf + cnt - 4))) {
@@ -1742,6 +1812,21 @@ size_t oj_dump_float_printf(char *buf, size_t blen, VALUE obj, double d, const c
1742
1812
  return cnt;
1743
1813
  }
1744
1814
 
1815
+ // The only and except lists are the keys joined with a ':' and wrapped in one
1816
+ // at each end, so a key is in a list when ":key:" is in it. Matching in place
1817
+ // finds what searching the list for a copy of the key with the colons on it
1818
+ // found, without a buffer sized by the key.
1819
+ static bool key_listed(const char *list, const char *key, size_t klen) {
1820
+ const char *s;
1821
+
1822
+ for (s = list; NULL != (s = strchr(s, ':')); s++) {
1823
+ if (0 == strncmp(s + 1, key, klen) && ':' == s[klen + 1]) {
1824
+ return true;
1825
+ }
1826
+ }
1827
+ return false;
1828
+ }
1829
+
1745
1830
  bool oj_key_skip(VALUE key, const char *only, const char *except) {
1746
1831
  const char *skey;
1747
1832
 
@@ -1751,15 +1836,9 @@ bool oj_key_skip(VALUE key, const char *only, const char *except) {
1751
1836
  default: skey = NULL; break;
1752
1837
  }
1753
1838
  if (NULL != skey && '\0' != *skey) {
1754
- size_t size = strlen(skey);
1755
- char *buf = alloca(size + 3);
1756
-
1757
- buf[0] = ':';
1758
- strcpy(buf + 1, skey);
1759
- buf[size + 1] = ':';
1760
- buf[size + 2] = '\0';
1839
+ size_t klen = strlen(skey);
1761
1840
 
1762
- return ((NULL != only && NULL == strstr(only, buf)) || (NULL != except && NULL != strstr(except, buf)));
1841
+ return ((NULL != only && !key_listed(only, skey, klen)) || (NULL != except && key_listed(except, skey, klen)));
1763
1842
  }
1764
1843
  return NULL != only;
1765
1844
  }
data/ext/oj/dump.h CHANGED
@@ -70,9 +70,19 @@ inline static void assure_size(Out out, size_t len) {
70
70
  }
71
71
  }
72
72
 
73
+ // Bytes that fill_indent(), or the dump_opts equivalent used when :indent is a
74
+ // String, writes at this depth. nl_size is array_size or hash_size.
75
+ inline static size_t indent_len(Out out, int depth, size_t nl_size) {
76
+ if (out->opts->dump_opts.use) {
77
+ return depth * out->opts->dump_opts.indent_size + nl_size;
78
+ }
79
+ return depth * out->indent + 1;
80
+ }
81
+
73
82
  inline static void fill_indent(Out out, int cnt) {
74
83
  if (0 < out->indent) {
75
84
  cnt *= out->indent;
85
+ assure_size(out, cnt + 1);
76
86
  *out->cur++ = '\n';
77
87
  memset(out->cur, ' ', cnt);
78
88
  out->cur += cnt;
data/ext/oj/dump_compat.c CHANGED
@@ -162,6 +162,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
162
162
  }
163
163
  }
164
164
  } else {
165
+ assure_size(out, size);
165
166
  fill_indent(out, d2);
166
167
  }
167
168
  oj_dump_compat_val(RARRAY_AREF(a, i), d2, out, true);
@@ -298,9 +299,10 @@ static ID message_id = 0;
298
299
  static ID backtrace_id = 0;
299
300
 
300
301
  static void exception_alt(VALUE obj, int depth, Out out) {
301
- int d3 = depth + 2;
302
- size_t size = d3 * out->indent + 2;
303
- size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
302
+ int d3 = depth + 2;
303
+ size_t size = d3 * out->indent + 2;
304
+ size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
305
+ volatile VALUE bt;
304
306
 
305
307
  if (0 == message_id) {
306
308
  message_id = rb_intern("message");
@@ -319,7 +321,7 @@ static void exception_alt(VALUE obj, int depth, Out out) {
319
321
  if (0 < out->opts->dump_opts.after_size) {
320
322
  APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
321
323
  }
322
- oj_dump_str(rb_funcall(obj, message_id, 0), 0, out, false);
324
+ oj_dump_compat_val(rb_funcall(obj, message_id, 0), depth + 1, out, true);
323
325
  assure_size(out, size + sep_len + 6);
324
326
  *out->cur++ = ',';
325
327
  fill_indent(out, d3);
@@ -331,7 +333,12 @@ static void exception_alt(VALUE obj, int depth, Out out) {
331
333
  if (0 < out->opts->dump_opts.after_size) {
332
334
  APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
333
335
  }
334
- dump_array(rb_funcall(obj, backtrace_id, 0), depth, out, false);
336
+ bt = rb_funcall(obj, backtrace_id, 0);
337
+ if (T_ARRAY == rb_type(bt)) {
338
+ dump_array(bt, depth, out, false);
339
+ } else {
340
+ oj_dump_nil(Qnil, depth, out, false);
341
+ }
335
342
  fill_indent(out, depth);
336
343
  *out->cur++ = '}';
337
344
  *out->cur = '\0';
@@ -463,9 +470,6 @@ oj_add_to_json(int argc, VALUE *argv, VALUE self) {
463
470
 
464
471
  if (0 == argc) {
465
472
  for (a = oj_compat_codes; NULL != a->name; a++) {
466
- if (Qnil == a->clas || Qundef == a->clas) {
467
- a->clas = rb_const_get_at(rb_cObject, rb_intern(a->name));
468
- }
469
473
  a->active = true;
470
474
  }
471
475
  use_struct_alt = true;
@@ -496,10 +500,7 @@ oj_add_to_json(int argc, VALUE *argv, VALUE self) {
496
500
  continue;
497
501
  }
498
502
  for (a = oj_compat_codes; NULL != a->name; a++) {
499
- if (Qnil == a->clas || Qundef == a->clas) {
500
- a->clas = rb_const_get_at(rb_cObject, rb_intern(a->name));
501
- }
502
- if (*argv == a->clas) {
503
+ if (*argv == oj_code_class(a)) {
503
504
  a->active = true;
504
505
  break;
505
506
  }
@@ -556,12 +557,15 @@ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
556
557
  size_t cnt = 0;
557
558
 
558
559
  if (0.0 == d) {
559
- b = buf;
560
+ b = buf;
561
+ if (signbit(d)) {
562
+ *b++ = '-';
563
+ }
560
564
  *b++ = '0';
561
565
  *b++ = '.';
562
566
  *b++ = '0';
563
- *b++ = '\0';
564
- cnt = 3;
567
+ *b = '\0';
568
+ cnt = b - buf;
565
569
  } else if (OJ_INFINITY == d) {
566
570
  if (WordNan == out->opts->dump_opts.nan_dump) {
567
571
  strcpy(buf, "Infinity");
data/ext/oj/dump_object.c CHANGED
@@ -10,6 +10,24 @@ static const char hex_chars[17] = "0123456789abcdef";
10
10
 
11
11
  static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out);
12
12
 
13
+ // An odd class is dumped as {"^O":class,...} and is rebuilt with a create
14
+ // function when loaded so there is no place to put a "^i" circular id on it.
15
+ // Registering it in the circular cache would consume an id that is never
16
+ // written and any later occurrence would then be dumped as a "^r" that refers
17
+ // to nothing. Instead dump odd classes in full each time they are encountered
18
+ // just like Time, Rational, and Complex are.
19
+ static void dump_circular_obj(VALUE obj, VALUE clas, int depth, Out out) {
20
+ if (NULL != oj_get_odd(clas)) {
21
+ dump_obj_attrs(obj, clas, 0, depth, out);
22
+ } else {
23
+ long id = oj_check_circular(obj, out);
24
+
25
+ if (0 <= id) {
26
+ dump_obj_attrs(obj, clas, id, depth, out);
27
+ }
28
+ }
29
+ }
30
+
13
31
  static void dump_time(VALUE obj, Out out) {
14
32
  switch (out->opts->time_format) {
15
33
  case RubyTime:
@@ -47,11 +65,7 @@ static void dump_data(VALUE obj, int depth, Out out, bool as_ok) {
47
65
  oj_dump_cstr(str, len, 0, 0, out);
48
66
  }
49
67
  } else {
50
- long id = oj_check_circular(obj, out);
51
-
52
- if (0 <= id) {
53
- dump_obj_attrs(obj, clas, id, depth, out);
54
- }
68
+ dump_circular_obj(obj, clas, depth, out);
55
69
  }
56
70
  }
57
71
  }
@@ -74,11 +88,7 @@ static void dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
74
88
  oj_dump_raw(str, len, out);
75
89
  }
76
90
  } else {
77
- long id = oj_check_circular(obj, out);
78
-
79
- if (0 <= id) {
80
- dump_obj_attrs(obj, clas, id, depth, out);
81
- }
91
+ dump_circular_obj(obj, clas, depth, out);
82
92
  }
83
93
  }
84
94
 
@@ -133,6 +143,7 @@ static void dump_array_class(VALUE a, VALUE clas, int depth, Out out) {
133
143
  cnt--;
134
144
  for (i = 0; i <= cnt; i++) {
135
145
  if (out->opts->dump_opts.use) {
146
+ assure_size(out, size);
136
147
  if (0 < out->opts->dump_opts.array_size) {
137
148
  APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
138
149
  }
@@ -150,7 +161,7 @@ static void dump_array_class(VALUE a, VALUE clas, int depth, Out out) {
150
161
  *out->cur++ = ',';
151
162
  }
152
163
  }
153
- size = depth * out->indent + 1;
164
+ size = indent_len(out, depth, out->opts->dump_opts.array_size) + 1;
154
165
  assure_size(out, size);
155
166
  if (out->opts->dump_opts.use) {
156
167
  // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
@@ -340,7 +351,7 @@ static int dump_attr_cb(ID key, VALUE value, VALUE ov) {
340
351
  } else if (Yes == out->opts->ignore_under && '@' == *attr && '_' == attr[1]) {
341
352
  return ST_CONTINUE;
342
353
  }
343
- if (0 == strcmp("bt", attr) || 0 == strcmp("mesg", attr)) {
354
+ if ('@' != *attr && (0 == strcmp("bt", attr) || 0 == strcmp("mesg", attr) || 0 == strcmp("bt_locations", attr))) {
344
355
  return ST_CONTINUE;
345
356
  }
346
357
  assure_size(out, size);
@@ -601,6 +612,10 @@ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
601
612
  APPEND_CHARS(out->cur, name, len);
602
613
  *out->cur++ = '"';
603
614
  }
615
+ if (2 == cnt && rb_obj_is_kind_of(obj, rb_cRange)) {
616
+ assure_size(out, 8);
617
+ APPEND_CHARS(out->cur, ",\"excl\"", 7);
618
+ }
604
619
  *out->cur++ = ']';
605
620
  } else {
606
621
  fill_indent(out, d3);
@@ -634,6 +649,17 @@ static void dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
634
649
  oj_dump_obj_val(v, d3, out);
635
650
  *out->cur++ = ',';
636
651
  }
652
+ if (2 == cnt && rb_obj_is_kind_of(obj, rb_cRange)) {
653
+ VALUE beg;
654
+ VALUE end;
655
+ int excl;
656
+
657
+ rb_range_values(obj, &beg, &end, &excl);
658
+ assure_size(out, size);
659
+ fill_indent(out, d3);
660
+ oj_dump_obj_val(excl ? Qtrue : Qfalse, d3, out);
661
+ *out->cur++ = ',';
662
+ }
637
663
  }
638
664
  #else
639
665
  {
data/ext/oj/dump_strict.c CHANGED
@@ -135,6 +135,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
135
135
  cnt--;
136
136
  for (i = 0; i <= cnt; i++) {
137
137
  if (out->opts->dump_opts.use) {
138
+ assure_size(out, size);
138
139
  if (0 < out->opts->dump_opts.array_size) {
139
140
  APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
140
141
  }
@@ -156,7 +157,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
156
157
  *out->cur++ = ',';
157
158
  }
158
159
  }
159
- size = depth * out->indent + 1;
160
+ size = indent_len(out, depth, out->opts->dump_opts.array_size) + 1;
160
161
  assure_size(out, size);
161
162
  if (out->opts->dump_opts.use) {
162
163
  // printf("*** d2: %u indent: %u '%s'\n", d2, out->opts->dump_opts->indent_size,
data/ext/oj/extconf.rb CHANGED
@@ -31,6 +31,7 @@ have_func('stpcpy')
31
31
  have_func('pthread_mutex_init')
32
32
  have_func('getrlimit', 'sys/resource.h')
33
33
  have_func('rb_enc_interned_str')
34
+ have_func('rb_hash_new_capa', 'ruby.h')
34
35
  have_func('rb_ext_ractor_safe', 'ruby.h')
35
36
 
36
37
  dflags['OJ_DEBUG'] = true unless ENV['OJ_DEBUG'].nil?
@@ -47,6 +48,12 @@ if try_cflags('-msse2')
47
48
  $CPPFLAGS += ' -msse2'
48
49
  end
49
50
 
51
+ # Ensure C99 mode for compilers that default to C90 (e.g. GCC 4.x).
52
+ # simd.h uses C99 for-loop declarations that fail under -std=gnu90.
53
+ if !$CFLAGS.include?('-std=') && try_cflags('-std=gnu99')
54
+ $CFLAGS += ' -std=gnu99'
55
+ end
56
+
50
57
  if enable_config('trace-log', false)
51
58
  dflags['OJ_ENABLE_TRACE_LOG'] = 1
52
59
  end