oj 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +17 -23
  3. data/README.md +74 -425
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +4 -0
  6. data/ext/oj/circarray.c +68 -0
  7. data/ext/oj/circarray.h +23 -0
  8. data/ext/oj/code.c +227 -0
  9. data/ext/oj/code.h +40 -0
  10. data/ext/oj/compat.c +243 -0
  11. data/ext/oj/custom.c +1097 -0
  12. data/ext/oj/dump.c +766 -1534
  13. data/ext/oj/dump.h +92 -0
  14. data/ext/oj/dump_compat.c +937 -0
  15. data/ext/oj/dump_leaf.c +254 -0
  16. data/ext/oj/dump_object.c +810 -0
  17. data/ext/oj/dump_rails.c +329 -0
  18. data/ext/oj/dump_strict.c +416 -0
  19. data/ext/oj/encode.h +51 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +17 -7
  23. data/ext/oj/fast.c +213 -180
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +817 -0
  28. data/ext/oj/mimic_rails.c +806 -0
  29. data/ext/oj/mimic_rails.h +17 -0
  30. data/ext/oj/object.c +752 -0
  31. data/ext/oj/odd.c +230 -0
  32. data/ext/oj/odd.h +44 -0
  33. data/ext/oj/oj.c +1288 -929
  34. data/ext/oj/oj.h +240 -69
  35. data/ext/oj/parse.c +1014 -0
  36. data/ext/oj/parse.h +92 -0
  37. data/ext/oj/reader.c +223 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +127 -0
  40. data/ext/oj/{cache.h → resolve.h} +6 -13
  41. data/ext/oj/rxclass.c +133 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +77 -175
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +911 -0
  46. data/ext/oj/stream_writer.c +301 -0
  47. data/ext/oj/strict.c +162 -0
  48. data/ext/oj/string_writer.c +480 -0
  49. data/ext/oj/val_stack.c +98 -0
  50. data/ext/oj/val_stack.h +188 -0
  51. data/lib/oj/active_support_helper.rb +41 -0
  52. data/lib/oj/bag.rb +6 -10
  53. data/lib/oj/easy_hash.rb +52 -0
  54. data/lib/oj/json.rb +172 -0
  55. data/lib/oj/mimic.rb +260 -5
  56. data/lib/oj/saj.rb +13 -10
  57. data/lib/oj/schandler.rb +142 -0
  58. data/lib/oj/state.rb +131 -0
  59. data/lib/oj/version.rb +1 -1
  60. data/lib/oj.rb +11 -23
  61. data/pages/Advanced.md +22 -0
  62. data/pages/Compatibility.md +25 -0
  63. data/pages/Custom.md +23 -0
  64. data/pages/Encoding.md +65 -0
  65. data/pages/JsonGem.md +79 -0
  66. data/pages/Modes.md +140 -0
  67. data/pages/Options.md +250 -0
  68. data/pages/Rails.md +60 -0
  69. data/pages/Security.md +20 -0
  70. data/test/_test_active.rb +76 -0
  71. data/test/_test_active_mimic.rb +96 -0
  72. data/test/_test_mimic_rails.rb +126 -0
  73. data/test/activesupport4/decoding_test.rb +105 -0
  74. data/test/activesupport4/encoding_test.rb +531 -0
  75. data/test/activesupport4/test_helper.rb +41 -0
  76. data/test/activesupport5/decoding_test.rb +125 -0
  77. data/test/activesupport5/encoding_test.rb +483 -0
  78. data/test/activesupport5/encoding_test_cases.rb +90 -0
  79. data/test/activesupport5/test_helper.rb +50 -0
  80. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  81. data/test/helper.rb +27 -0
  82. data/test/isolated/shared.rb +310 -0
  83. data/test/isolated/test_mimic_after.rb +13 -0
  84. data/test/isolated/test_mimic_alone.rb +12 -0
  85. data/test/isolated/test_mimic_as_json.rb +45 -0
  86. data/test/isolated/test_mimic_before.rb +13 -0
  87. data/test/isolated/test_mimic_define.rb +28 -0
  88. data/test/isolated/test_mimic_rails_after.rb +22 -0
  89. data/test/isolated/test_mimic_rails_before.rb +21 -0
  90. data/test/isolated/test_mimic_redefine.rb +15 -0
  91. data/test/json_gem/json_addition_test.rb +216 -0
  92. data/test/json_gem/json_common_interface_test.rb +143 -0
  93. data/test/json_gem/json_encoding_test.rb +109 -0
  94. data/test/json_gem/json_ext_parser_test.rb +20 -0
  95. data/test/json_gem/json_fixtures_test.rb +35 -0
  96. data/test/json_gem/json_generator_test.rb +383 -0
  97. data/test/json_gem/json_generic_object_test.rb +90 -0
  98. data/test/json_gem/json_parser_test.rb +470 -0
  99. data/test/json_gem/json_string_matching_test.rb +42 -0
  100. data/test/json_gem/test_helper.rb +18 -0
  101. data/test/perf_compat.rb +130 -0
  102. data/test/perf_fast.rb +9 -9
  103. data/test/perf_file.rb +64 -0
  104. data/test/{perf_obj.rb → perf_object.rb} +24 -10
  105. data/test/perf_scp.rb +151 -0
  106. data/test/perf_strict.rb +32 -113
  107. data/test/sample.rb +2 -3
  108. data/test/test_compat.rb +474 -0
  109. data/test/test_custom.rb +355 -0
  110. data/test/test_debian.rb +53 -0
  111. data/test/test_fast.rb +66 -16
  112. data/test/test_file.rb +237 -0
  113. data/test/test_gc.rb +49 -0
  114. data/test/test_hash.rb +29 -0
  115. data/test/test_null.rb +376 -0
  116. data/test/test_object.rb +1010 -0
  117. data/test/test_saj.rb +16 -16
  118. data/test/test_scp.rb +417 -0
  119. data/test/test_strict.rb +410 -0
  120. data/test/test_various.rb +815 -0
  121. data/test/test_writer.rb +308 -0
  122. data/test/tests.rb +9 -902
  123. data/test/tests_mimic.rb +14 -0
  124. data/test/tests_mimic_addition.rb +7 -0
  125. metadata +253 -38
  126. data/ext/oj/cache.c +0 -148
  127. data/ext/oj/foo.rb +0 -6
  128. data/ext/oj/load.c +0 -1049
  129. data/test/a.rb +0 -38
  130. data/test/perf1.rb +0 -64
  131. data/test/perf2.rb +0 -76
  132. data/test/perf_obj_old.rb +0 -213
  133. data/test/test_mimic.rb +0 -208
data/ext/oj/scp.c ADDED
@@ -0,0 +1,224 @@
1
+ /* scp.c
2
+ * Copyright (c) 2012, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #include <stdlib.h>
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+ #include <math.h>
10
+ #include <sys/types.h>
11
+ #include <unistd.h>
12
+
13
+ #include "oj.h"
14
+ #include "parse.h"
15
+ #include "encode.h"
16
+
17
+ static VALUE
18
+ noop_start(ParseInfo pi) {
19
+ return Qnil;
20
+ }
21
+
22
+ static void
23
+ noop_end(ParseInfo pi) {
24
+ }
25
+
26
+ static void
27
+ noop_add_value(ParseInfo pi, VALUE val) {
28
+ }
29
+
30
+ static void
31
+ noop_add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
32
+ }
33
+
34
+ static void
35
+ noop_add_num(ParseInfo pi, NumInfo ni) {
36
+ }
37
+
38
+ static VALUE
39
+ noop_hash_key(struct _ParseInfo *pi, const char *key, size_t klen) {
40
+ return Qundef;
41
+ }
42
+
43
+ static void
44
+ noop_hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
45
+ }
46
+
47
+ static void
48
+ noop_hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
49
+ }
50
+
51
+ static void
52
+ noop_hash_set_value(ParseInfo pi, Val kval, VALUE value) {
53
+ }
54
+
55
+ static void
56
+ noop_array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
57
+ }
58
+
59
+ static void
60
+ noop_array_append_num(ParseInfo pi, NumInfo ni) {
61
+ }
62
+
63
+ static void
64
+ noop_array_append_value(ParseInfo pi, VALUE value) {
65
+ }
66
+
67
+ static void
68
+ add_value(ParseInfo pi, VALUE val) {
69
+ rb_funcall(pi->handler, oj_add_value_id, 1, val);
70
+ }
71
+
72
+ static void
73
+ add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
74
+ volatile VALUE rstr = rb_str_new(str, len);
75
+
76
+ rstr = oj_encode(rstr);
77
+ rb_funcall(pi->handler, oj_add_value_id, 1, rstr);
78
+ }
79
+
80
+ static void
81
+ add_num(ParseInfo pi, NumInfo ni) {
82
+ rb_funcall(pi->handler, oj_add_value_id, 1, oj_num_as_value(ni));
83
+ }
84
+
85
+ static VALUE
86
+ start_hash(ParseInfo pi) {
87
+ return rb_funcall(pi->handler, oj_hash_start_id, 0);
88
+ }
89
+
90
+ static void
91
+ end_hash(ParseInfo pi) {
92
+ rb_funcall(pi->handler, oj_hash_end_id, 0);
93
+ }
94
+
95
+ static VALUE
96
+ start_array(ParseInfo pi) {
97
+ return rb_funcall(pi->handler, oj_array_start_id, 0);
98
+ }
99
+
100
+ static void
101
+ end_array(ParseInfo pi) {
102
+ rb_funcall(pi->handler, oj_array_end_id, 0);
103
+ }
104
+
105
+ static VALUE
106
+ calc_hash_key(ParseInfo pi, Val kval) {
107
+ volatile VALUE rkey = kval->key_val;
108
+
109
+ if (Qundef == rkey) {
110
+ rkey = rb_str_new(kval->key, kval->klen);
111
+ rkey = oj_encode(rkey);
112
+ if (Yes == pi->options.sym_key) {
113
+ rkey = rb_str_intern(rkey);
114
+ }
115
+ }
116
+ return rkey;
117
+ }
118
+
119
+ static VALUE
120
+ hash_key(struct _ParseInfo *pi, const char *key, size_t klen) {
121
+ return rb_funcall(pi->handler, oj_hash_key_id, 1, rb_str_new(key, klen));
122
+ }
123
+
124
+ static void
125
+ hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
126
+ volatile VALUE rstr = rb_str_new(str, len);
127
+
128
+ rstr = oj_encode(rstr);
129
+ rb_funcall(pi->handler, oj_hash_set_id, 3, stack_peek(&pi->stack)->val, calc_hash_key(pi, kval), rstr);
130
+ }
131
+
132
+ static void
133
+ hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
134
+ rb_funcall(pi->handler, oj_hash_set_id, 3, stack_peek(&pi->stack)->val, calc_hash_key(pi, kval), oj_num_as_value(ni));
135
+ }
136
+
137
+ static void
138
+ hash_set_value(ParseInfo pi, Val kval, VALUE value) {
139
+ rb_funcall(pi->handler, oj_hash_set_id, 3, stack_peek(&pi->stack)->val, calc_hash_key(pi, kval), value);
140
+ }
141
+
142
+ static void
143
+ array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
144
+ volatile VALUE rstr = rb_str_new(str, len);
145
+
146
+ rstr = oj_encode(rstr);
147
+ rb_funcall(pi->handler, oj_array_append_id, 2, stack_peek(&pi->stack)->val, rstr);
148
+ }
149
+
150
+ static void
151
+ array_append_num(ParseInfo pi, NumInfo ni) {
152
+ rb_funcall(pi->handler, oj_array_append_id, 2, stack_peek(&pi->stack)->val, oj_num_as_value(ni));
153
+ }
154
+
155
+ static void
156
+ array_append_value(ParseInfo pi, VALUE value) {
157
+ rb_funcall(pi->handler, oj_array_append_id, 2, stack_peek(&pi->stack)->val, value);
158
+ }
159
+
160
+ VALUE
161
+ oj_sc_parse(int argc, VALUE *argv, VALUE self) {
162
+ struct _ParseInfo pi;
163
+ VALUE input = argv[1];
164
+
165
+ parse_info_init(&pi);
166
+ pi.err_class = Qnil;
167
+ pi.max_depth = 0;
168
+ pi.options = oj_default_options;
169
+ if (3 == argc) {
170
+ oj_parse_options(argv[2], &pi.options);
171
+ }
172
+ if (rb_block_given_p()) {
173
+ pi.proc = Qnil;
174
+ } else {
175
+ pi.proc = Qundef;
176
+ }
177
+ pi.handler = *argv;
178
+
179
+ pi.start_hash = rb_respond_to(pi.handler, oj_hash_start_id) ? start_hash : noop_start;
180
+ pi.end_hash = rb_respond_to(pi.handler, oj_hash_end_id) ? end_hash : noop_end;
181
+ pi.hash_key = rb_respond_to(pi.handler, oj_hash_key_id) ? hash_key : noop_hash_key;
182
+ pi.start_array = rb_respond_to(pi.handler, oj_array_start_id) ? start_array : noop_start;
183
+ pi.end_array = rb_respond_to(pi.handler, oj_array_end_id) ? end_array : noop_end;
184
+ if (rb_respond_to(pi.handler, oj_hash_set_id)) {
185
+ pi.hash_set_value = hash_set_value;
186
+ pi.hash_set_cstr = hash_set_cstr;
187
+ pi.hash_set_num = hash_set_num;
188
+ pi.expect_value = 1;
189
+ } else {
190
+ pi.hash_set_value = noop_hash_set_value;
191
+ pi.hash_set_cstr = noop_hash_set_cstr;
192
+ pi.hash_set_num = noop_hash_set_num;
193
+ pi.expect_value = 0;
194
+ }
195
+ if (rb_respond_to(pi.handler, oj_array_append_id)) {
196
+ pi.array_append_value = array_append_value;
197
+ pi.array_append_cstr = array_append_cstr;
198
+ pi.array_append_num = array_append_num;
199
+ pi.expect_value = 1;
200
+ } else {
201
+ pi.array_append_value = noop_array_append_value;
202
+ pi.array_append_cstr = noop_array_append_cstr;
203
+ pi.array_append_num = noop_array_append_num;
204
+ pi.expect_value = 0;
205
+ }
206
+ if (rb_respond_to(pi.handler, oj_add_value_id)) {
207
+ pi.add_cstr = add_cstr;
208
+ pi.add_num = add_num;
209
+ pi.add_value = add_value;
210
+ pi.expect_value = 1;
211
+ } else {
212
+ pi.add_cstr = noop_add_cstr;
213
+ pi.add_num = noop_add_num;
214
+ pi.add_value = noop_add_value;
215
+ pi.expect_value = 0;
216
+ }
217
+ pi.has_callbacks = true;
218
+
219
+ if (T_STRING == rb_type(input)) {
220
+ return oj_pi_parse(argc - 1, argv + 1, &pi, 0, 0, 1);
221
+ } else {
222
+ return oj_pi_sparse(argc - 1, argv + 1, &pi, 0);
223
+ }
224
+ }