oj 3.11.0 → 3.16.5

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 (173) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1421 -0
  3. data/README.md +20 -5
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +48 -38
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +60 -62
  9. data/ext/oj/cache8.h +8 -7
  10. data/ext/oj/circarray.c +35 -35
  11. data/ext/oj/circarray.h +11 -9
  12. data/ext/oj/code.c +156 -174
  13. data/ext/oj/code.h +19 -18
  14. data/ext/oj/compat.c +140 -197
  15. data/ext/oj/custom.c +737 -879
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +830 -835
  18. data/ext/oj/dump.h +65 -53
  19. data/ext/oj/dump_compat.c +566 -642
  20. data/ext/oj/dump_leaf.c +95 -182
  21. data/ext/oj/dump_object.c +518 -659
  22. data/ext/oj/dump_strict.c +301 -334
  23. data/ext/oj/encode.h +3 -4
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +27 -24
  26. data/ext/oj/err.h +38 -13
  27. data/ext/oj/extconf.rb +23 -7
  28. data/ext/oj/fast.c +1043 -1073
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +449 -423
  34. data/ext/oj/object.c +530 -576
  35. data/ext/oj/odd.c +155 -138
  36. data/ext/oj/odd.h +24 -22
  37. data/ext/oj/oj.c +1331 -993
  38. data/ext/oj/oj.h +306 -292
  39. data/ext/oj/parse.c +934 -938
  40. data/ext/oj/parse.h +73 -70
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +101 -0
  43. data/ext/oj/rails.c +795 -845
  44. data/ext/oj/rails.h +7 -7
  45. data/ext/oj/reader.c +132 -140
  46. data/ext/oj/reader.h +67 -78
  47. data/ext/oj/resolve.c +40 -59
  48. data/ext/oj/resolve.h +3 -2
  49. data/ext/oj/rxclass.c +67 -67
  50. data/ext/oj/rxclass.h +11 -9
  51. data/ext/oj/saj.c +441 -480
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +78 -111
  55. data/ext/oj/sparse.c +726 -730
  56. data/ext/oj/stream_writer.c +146 -165
  57. data/ext/oj/strict.c +103 -123
  58. data/ext/oj/string_writer.c +241 -253
  59. data/ext/oj/trace.c +29 -33
  60. data/ext/oj/trace.h +41 -11
  61. data/ext/oj/usual.c +1218 -0
  62. data/ext/oj/usual.h +69 -0
  63. data/ext/oj/util.c +103 -103
  64. data/ext/oj/util.h +3 -2
  65. data/ext/oj/val_stack.c +60 -49
  66. data/ext/oj/val_stack.h +79 -85
  67. data/ext/oj/validate.c +46 -0
  68. data/ext/oj/wab.c +307 -350
  69. data/lib/oj/active_support_helper.rb +1 -3
  70. data/lib/oj/bag.rb +8 -1
  71. data/lib/oj/easy_hash.rb +9 -9
  72. data/lib/oj/error.rb +1 -2
  73. data/lib/oj/json.rb +162 -150
  74. data/lib/oj/mimic.rb +9 -19
  75. data/lib/oj/saj.rb +20 -6
  76. data/lib/oj/schandler.rb +5 -4
  77. data/lib/oj/state.rb +12 -8
  78. data/lib/oj/version.rb +1 -2
  79. data/lib/oj.rb +2 -0
  80. data/pages/Compatibility.md +1 -1
  81. data/pages/InstallOptions.md +20 -0
  82. data/pages/JsonGem.md +15 -0
  83. data/pages/Modes.md +8 -3
  84. data/pages/Options.md +43 -5
  85. data/pages/Parser.md +309 -0
  86. data/pages/Rails.md +14 -2
  87. data/test/_test_active.rb +8 -9
  88. data/test/_test_active_mimic.rb +7 -8
  89. data/test/_test_mimic_rails.rb +17 -20
  90. data/test/activerecord/result_test.rb +12 -8
  91. data/test/activesupport6/encoding_test.rb +63 -28
  92. data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
  93. data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
  94. data/test/{activesupport5 → activesupport7}/encoding_test.rb +86 -50
  95. data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
  96. data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
  97. data/test/files.rb +15 -15
  98. data/test/foo.rb +17 -43
  99. data/test/helper.rb +16 -3
  100. data/test/isolated/shared.rb +3 -2
  101. data/test/json_gem/json_addition_test.rb +2 -2
  102. data/test/json_gem/json_common_interface_test.rb +8 -6
  103. data/test/json_gem/json_encoding_test.rb +0 -0
  104. data/test/json_gem/json_ext_parser_test.rb +1 -0
  105. data/test/json_gem/json_fixtures_test.rb +3 -2
  106. data/test/json_gem/json_generator_test.rb +71 -41
  107. data/test/json_gem/json_generic_object_test.rb +11 -11
  108. data/test/json_gem/json_parser_test.rb +54 -47
  109. data/test/json_gem/json_string_matching_test.rb +9 -9
  110. data/test/json_gem/test_helper.rb +12 -0
  111. data/test/mem.rb +34 -0
  112. data/test/perf.rb +22 -27
  113. data/test/perf_compat.rb +31 -33
  114. data/test/perf_dump.rb +50 -0
  115. data/test/perf_fast.rb +80 -82
  116. data/test/perf_file.rb +27 -29
  117. data/test/perf_object.rb +65 -69
  118. data/test/perf_once.rb +59 -0
  119. data/test/perf_parser.rb +183 -0
  120. data/test/perf_saj.rb +46 -54
  121. data/test/perf_scp.rb +58 -69
  122. data/test/perf_simple.rb +41 -39
  123. data/test/perf_strict.rb +74 -82
  124. data/test/perf_wab.rb +67 -69
  125. data/test/prec.rb +5 -5
  126. data/test/sample/change.rb +0 -1
  127. data/test/sample/dir.rb +0 -1
  128. data/test/sample/doc.rb +0 -1
  129. data/test/sample/file.rb +0 -1
  130. data/test/sample/group.rb +0 -1
  131. data/test/sample/hasprops.rb +0 -1
  132. data/test/sample/layer.rb +0 -1
  133. data/test/sample/rect.rb +0 -1
  134. data/test/sample/shape.rb +0 -1
  135. data/test/sample/text.rb +0 -1
  136. data/test/sample.rb +16 -16
  137. data/test/sample_json.rb +8 -8
  138. data/test/test_compat.rb +97 -45
  139. data/test/test_custom.rb +73 -51
  140. data/test/test_debian.rb +7 -10
  141. data/test/test_fast.rb +135 -79
  142. data/test/test_file.rb +41 -30
  143. data/test/test_gc.rb +16 -5
  144. data/test/test_generate.rb +21 -0
  145. data/test/test_hash.rb +15 -5
  146. data/test/test_integer_range.rb +9 -9
  147. data/test/test_null.rb +20 -20
  148. data/test/test_object.rb +99 -96
  149. data/test/test_parser.rb +11 -0
  150. data/test/test_parser_debug.rb +27 -0
  151. data/test/test_parser_saj.rb +337 -0
  152. data/test/test_parser_usual.rb +251 -0
  153. data/test/test_rails.rb +2 -2
  154. data/test/test_saj.rb +10 -8
  155. data/test/test_scp.rb +38 -40
  156. data/test/test_strict.rb +40 -32
  157. data/test/test_various.rb +165 -84
  158. data/test/test_wab.rb +48 -44
  159. data/test/test_writer.rb +47 -47
  160. data/test/tests.rb +13 -5
  161. data/test/tests_mimic.rb +12 -3
  162. data/test/tests_mimic_addition.rb +12 -3
  163. metadata +75 -127
  164. data/ext/oj/hash.c +0 -135
  165. data/ext/oj/hash.h +0 -18
  166. data/ext/oj/hash_test.c +0 -484
  167. data/test/activesupport4/decoding_test.rb +0 -108
  168. data/test/activesupport4/encoding_test.rb +0 -531
  169. data/test/activesupport4/test_helper.rb +0 -41
  170. data/test/activesupport5/test_helper.rb +0 -72
  171. data/test/bar.rb +0 -35
  172. data/test/baz.rb +0 -16
  173. data/test/zoo.rb +0 -13
data/ext/oj/encode.h CHANGED
@@ -1,15 +1,14 @@
1
1
  // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
2
3
 
3
4
  #ifndef OJ_ENCODE_H
4
5
  #define OJ_ENCODE_H
5
6
 
7
+ #include "oj.h"
6
8
  #include "ruby.h"
7
9
  #include "ruby/encoding.h"
8
10
 
9
- #include "oj.h"
10
-
11
- static inline VALUE
12
- oj_encode(VALUE rstr) {
11
+ static inline VALUE oj_encode(VALUE rstr) {
13
12
  rb_enc_associate(rstr, oj_utf8_encoding);
14
13
  return rstr;
15
14
  }
data/ext/oj/encoder.c ADDED
@@ -0,0 +1,43 @@
1
+ // Copyright (c) 2011, 2022 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "oj.h"
5
+
6
+ typedef struct _encoder {
7
+ int indent; // indention for dump, default 2
8
+ char circular; // YesNo
9
+ char escape_mode; // Escape_Mode
10
+ char mode; // Mode
11
+ char time_format; // TimeFormat
12
+ char bigdec_as_num; // YesNo
13
+ char to_hash; // YesNo
14
+ char to_json; // YesNo
15
+ char as_json; // YesNo
16
+ char raw_json; // YesNo
17
+ char trace; // YesNo
18
+ char sec_prec_set; // boolean (0 or 1)
19
+ char ignore_under; // YesNo - ignore attrs starting with _ if true in object and custom modes
20
+ int64_t int_range_min; // dump numbers below as string
21
+ int64_t int_range_max; // dump numbers above as string
22
+ const char* create_id; // 0 or string
23
+ size_t create_id_len; // length of create_id
24
+ int sec_prec; // second precision when dumping time
25
+ char float_prec; // float precision, linked to float_fmt
26
+ char float_fmt[7]; // float format for dumping, if empty use Ruby
27
+ struct _dumpOpts dump_opts;
28
+ struct _rxClass str_rx;
29
+ VALUE* ignore; // Qnil terminated array of classes or NULL
30
+ }* Encoder;
31
+
32
+ /*
33
+ rb_define_module_function(Oj, "encode", encode, -1);
34
+ rb_define_module_function(Oj, "to_file", to_file, -1); // or maybe just write
35
+ rb_define_module_function(Oj, "to_stream", to_stream, -1);
36
+ */
37
+
38
+ // write(to, obj)
39
+ // if to is a string then open file
40
+ // else if stream then write to stream
41
+ // handle non-blocking
42
+
43
+ // should each mode have a different encoder or use delegates like the parser?
data/ext/oj/err.c CHANGED
@@ -1,12 +1,12 @@
1
1
  // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
-
3
- #include <stdarg.h>
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
4
3
 
5
4
  #include "err.h"
6
5
 
7
- void
8
- oj_err_set(Err e, VALUE clas, const char *format, ...) {
9
- va_list ap;
6
+ #include <stdarg.h>
7
+
8
+ void oj_err_set(Err e, VALUE clas, const char *format, ...) {
9
+ va_list ap;
10
10
 
11
11
  va_start(ap, format);
12
12
  e->clas = clas;
@@ -14,40 +14,43 @@ oj_err_set(Err e, VALUE clas, const char *format, ...) {
14
14
  va_end(ap);
15
15
  }
16
16
 
17
- void
18
- oj_err_raise(Err e) {
17
+ void oj_err_raise(Err e) {
19
18
  rb_raise(e->clas, "%s", e->msg);
20
19
  }
21
20
 
22
- void
23
- _oj_err_set_with_location(Err err, VALUE eclas, const char *msg, const char *json, const char *current, const char* file, int line) {
24
- int n = 1;
25
- int col = 1;
21
+ void _oj_err_set_with_location(Err err,
22
+ VALUE eclas,
23
+ const char *msg,
24
+ const char *json,
25
+ const char *current,
26
+ const char *file,
27
+ int line) {
28
+ int n = 1;
29
+ int col = 1;
26
30
 
27
31
  for (; json < current && '\n' != *current; current--) {
28
- col++;
32
+ col++;
29
33
  }
30
34
  for (; json < current; current--) {
31
- if ('\n' == *current) {
32
- n++;
33
- }
35
+ if ('\n' == *current) {
36
+ n++;
37
+ }
34
38
  }
35
39
  oj_err_set(err, eclas, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
36
40
  }
37
41
 
38
- void
39
- _oj_raise_error(const char *msg, const char *json, const char *current, const char* file, int line) {
40
- struct _err err;
41
- int n = 1;
42
- int col = 1;
42
+ void _oj_raise_error(const char *msg, const char *json, const char *current, const char *file, int line) {
43
+ struct _err err;
44
+ int n = 1;
45
+ int col = 1;
43
46
 
44
47
  for (; json < current && '\n' != *current; current--) {
45
- col++;
48
+ col++;
46
49
  }
47
50
  for (; json < current; current--) {
48
- if ('\n' == *current) {
49
- n++;
50
- }
51
+ if ('\n' == *current) {
52
+ n++;
53
+ }
51
54
  }
52
55
  oj_err_set(&err, oj_parse_error_class, "%s at line %d, column %d [%s:%d]", msg, n, col, file, line);
53
56
  rb_raise(err.clas, "%s", err.msg);
data/ext/oj/err.h CHANGED
@@ -1,41 +1,66 @@
1
1
  // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
2
3
 
3
4
  #ifndef OJ_ERR_H
4
5
  #define OJ_ERR_H
5
6
 
7
+ #include <errno.h>
8
+
6
9
  #include "ruby.h"
10
+
7
11
  // Needed to silence 2.4.0 warnings.
8
12
  #ifndef NORETURN
9
- # define NORETURN(x) x
13
+ #define NORETURN(x) x
10
14
  #endif
11
15
 
16
+ #define OJ_ERR_START 300
17
+
18
+ typedef enum {
19
+ OJ_OK = 0,
20
+ OJ_ERR_MEMORY = ENOMEM,
21
+ OJ_ERR_PARSE = OJ_ERR_START,
22
+ OJ_ERR_READ,
23
+ OJ_ERR_WRITE,
24
+ OJ_ERR_OVERFLOW,
25
+ OJ_ERR_ARG,
26
+ OJ_ERR_TOO_MANY,
27
+ OJ_ERR_TYPE,
28
+ OJ_ERR_KEY,
29
+ OJ_ABORT,
30
+ OJ_ERR_LAST,
31
+ } ojStatus;
32
+
12
33
  #define set_error(err, eclas, msg, json, current) _oj_err_set_with_location(err, eclas, msg, json, current, FILE, LINE)
13
34
 
14
35
  typedef struct _err {
15
- VALUE clas;
16
- char msg[128];
36
+ VALUE clas;
37
+ char msg[128];
17
38
  } *Err;
18
39
 
19
- extern VALUE oj_parse_error_class;
40
+ extern VALUE oj_parse_error_class;
20
41
 
21
- extern void oj_err_set(Err e, VALUE clas, const char *format, ...);
22
- extern void _oj_err_set_with_location(Err err, VALUE eclas, const char *msg, const char *json, const char *current, const char* file, int line);
42
+ extern void oj_err_set(Err e, VALUE clas, const char *format, ...);
43
+ extern void _oj_err_set_with_location(Err err,
44
+ VALUE eclas,
45
+ const char *msg,
46
+ const char *json,
47
+ const char *current,
48
+ const char *file,
49
+ int line);
23
50
 
24
- NORETURN(extern void oj_err_raise(Err e));
51
+ NORETURN(extern void oj_err_raise(Err e));
25
52
 
26
53
  #define raise_error(msg, json, current) _oj_raise_error(msg, json, current, __FILE__, __LINE__)
27
54
 
28
- NORETURN(extern void _oj_raise_error(const char *msg, const char *json, const char *current, const char* file, int line));
29
-
55
+ NORETURN(
56
+ extern void _oj_raise_error(const char *msg, const char *json, const char *current, const char *file, int line));
30
57
 
31
- inline static void
32
- err_init(Err e) {
58
+ inline static void err_init(Err e) {
33
59
  e->clas = Qnil;
34
60
  *e->msg = '\0';
35
61
  }
36
62
 
37
- inline static int
38
- err_has(Err e) {
63
+ inline static int err_has(Err e) {
39
64
  return (Qnil != e->clas);
40
65
  }
41
66
 
data/ext/oj/extconf.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mkmf'
2
4
  require 'rbconfig'
3
5
 
@@ -6,7 +8,7 @@ dir_config(extension_name)
6
8
 
7
9
  parts = RUBY_DESCRIPTION.split(' ')
8
10
  type = parts[0]
9
- type = type[4..-1] if type.start_with?('tcs-')
11
+ type = type[4..] if type.start_with?('tcs-')
10
12
  is_windows = RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
11
13
  platform = RUBY_PLATFORM
12
14
  version = RUBY_VERSION.split('.')
@@ -23,16 +25,30 @@ dflags = {
23
25
  'RSTRUCT_LEN_RETURNS_INTEGER_OBJECT' => ('ruby' == type && '2' == version[0] && '4' == version[1] && '1' >= version[2]) ? 1 : 0,
24
26
  }
25
27
 
26
- have_func('rb_time_timespec')
27
- have_func('rb_ivar_count')
28
- have_func('rb_ivar_foreach')
28
+ # Support for compaction.
29
+ have_func('rb_gc_mark_movable')
29
30
  have_func('stpcpy')
30
- have_func('rb_data_object_wrap')
31
31
  have_func('pthread_mutex_init')
32
+ have_func('getrlimit', 'sys/resource.h')
33
+ have_func('rb_enc_interned_str')
34
+ have_func('rb_ext_ractor_safe', 'ruby.h')
32
35
 
33
36
  dflags['OJ_DEBUG'] = true unless ENV['OJ_DEBUG'].nil?
34
37
 
35
- dflags.each do |k,v|
38
+ if with_config('--with-sse42')
39
+ if try_cflags('-msse4.2')
40
+ $CPPFLAGS += ' -msse4.2'
41
+ dflags['OJ_USE_SSE4_2'] = 1
42
+ else
43
+ warn 'SSE 4.2 is not supported on this platform.'
44
+ end
45
+ end
46
+
47
+ if enable_config('trace-log', false)
48
+ dflags['OJ_ENABLE_TRACE_LOG'] = 1
49
+ end
50
+
51
+ dflags.each do |k, v|
36
52
  if v.nil?
37
53
  $CPPFLAGS += " -D#{k}"
38
54
  else
@@ -41,7 +57,7 @@ dflags.each do |k,v|
41
57
  end
42
58
 
43
59
  $CPPFLAGS += ' -Wall'
44
- #puts "*** $CPPFLAGS: #{$CPPFLAGS}"
60
+ # puts "*** $CPPFLAGS: #{$CPPFLAGS}"
45
61
  # Adding the __attribute__ flag only works with gcc compilers and even then it
46
62
  # does not work to check args with varargs so just remove the check.
47
63
  CONFIG['warnflags'].slice!(/ -Wsuggest-attribute=format/)