oj 2.18.5 → 3.16.11

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 (166) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +53 -221
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +54 -72
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +61 -63
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +38 -67
  11. data/ext/oj/circarray.h +16 -42
  12. data/ext/oj/code.c +214 -0
  13. data/ext/oj/code.h +40 -0
  14. data/ext/oj/compat.c +194 -110
  15. data/ext/oj/custom.c +1074 -0
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1276 -2494
  18. data/ext/oj/dump.h +110 -0
  19. data/ext/oj/dump_compat.c +897 -0
  20. data/ext/oj/dump_leaf.c +162 -0
  21. data/ext/oj/dump_object.c +710 -0
  22. data/ext/oj/dump_strict.c +399 -0
  23. data/ext/oj/encode.h +7 -42
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -53
  26. data/ext/oj/err.h +49 -46
  27. data/ext/oj/extconf.rb +33 -32
  28. data/ext/oj/fast.c +1082 -1098
  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 +919 -0
  34. data/ext/oj/object.c +545 -625
  35. data/ext/oj/odd.c +158 -168
  36. data/ext/oj/odd.h +32 -58
  37. data/ext/oj/oj.c +1727 -2080
  38. data/ext/oj/oj.h +334 -259
  39. data/ext/oj/parse.c +974 -753
  40. data/ext/oj/parse.h +97 -90
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +1478 -0
  44. data/ext/oj/rails.h +18 -0
  45. data/ext/oj/reader.c +136 -163
  46. data/ext/oj/reader.h +76 -112
  47. data/ext/oj/resolve.c +45 -94
  48. data/ext/oj/resolve.h +7 -34
  49. data/ext/oj/rxclass.c +144 -0
  50. data/ext/oj/rxclass.h +26 -0
  51. data/ext/oj/saj.c +445 -511
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +82 -143
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +749 -644
  57. data/ext/oj/stream_writer.c +329 -0
  58. data/ext/oj/strict.c +114 -112
  59. data/ext/oj/string_writer.c +517 -0
  60. data/ext/oj/trace.c +72 -0
  61. data/ext/oj/trace.h +55 -0
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +136 -0
  65. data/ext/oj/util.h +20 -0
  66. data/ext/oj/val_stack.c +75 -72
  67. data/ext/oj/val_stack.h +94 -127
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +586 -0
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +21 -13
  73. data/lib/oj/error.rb +10 -12
  74. data/lib/oj/json.rb +188 -0
  75. data/lib/oj/mimic.rb +165 -26
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +135 -0
  79. data/lib/oj/version.rb +2 -3
  80. data/lib/oj.rb +3 -31
  81. data/pages/Advanced.md +22 -0
  82. data/pages/Compatibility.md +25 -0
  83. data/pages/Custom.md +23 -0
  84. data/pages/Encoding.md +65 -0
  85. data/pages/InstallOptions.md +20 -0
  86. data/pages/JsonGem.md +94 -0
  87. data/pages/Modes.md +161 -0
  88. data/pages/Options.md +337 -0
  89. data/pages/Parser.md +309 -0
  90. data/pages/Rails.md +167 -0
  91. data/pages/Security.md +20 -0
  92. data/pages/WAB.md +13 -0
  93. metadata +126 -163
  94. data/ext/oj/hash.c +0 -163
  95. data/ext/oj/hash.h +0 -46
  96. data/ext/oj/hash_test.c +0 -512
  97. data/test/_test_active.rb +0 -76
  98. data/test/_test_active_mimic.rb +0 -96
  99. data/test/_test_mimic_rails.rb +0 -126
  100. data/test/activesupport_datetime_test.rb +0 -23
  101. data/test/bug.rb +0 -51
  102. data/test/bug2.rb +0 -10
  103. data/test/bug3.rb +0 -46
  104. data/test/bug_fast.rb +0 -32
  105. data/test/bug_load.rb +0 -24
  106. data/test/crash.rb +0 -111
  107. data/test/curl/curl_oj.rb +0 -46
  108. data/test/curl/get_oj.rb +0 -24
  109. data/test/curl/just_curl.rb +0 -31
  110. data/test/curl/just_oj.rb +0 -51
  111. data/test/example.rb +0 -11
  112. data/test/files.rb +0 -29
  113. data/test/foo.rb +0 -24
  114. data/test/helper.rb +0 -27
  115. data/test/io.rb +0 -48
  116. data/test/isolated/shared.rb +0 -310
  117. data/test/isolated/test_mimic_after.rb +0 -13
  118. data/test/isolated/test_mimic_alone.rb +0 -12
  119. data/test/isolated/test_mimic_as_json.rb +0 -45
  120. data/test/isolated/test_mimic_before.rb +0 -13
  121. data/test/isolated/test_mimic_define.rb +0 -28
  122. data/test/isolated/test_mimic_rails_after.rb +0 -22
  123. data/test/isolated/test_mimic_rails_before.rb +0 -21
  124. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  125. data/test/isolated/test_mimic_redefine.rb +0 -15
  126. data/test/mod.rb +0 -16
  127. data/test/perf.rb +0 -107
  128. data/test/perf_compat.rb +0 -128
  129. data/test/perf_fast.rb +0 -164
  130. data/test/perf_file.rb +0 -64
  131. data/test/perf_object.rb +0 -138
  132. data/test/perf_saj.rb +0 -109
  133. data/test/perf_scp.rb +0 -151
  134. data/test/perf_simple.rb +0 -287
  135. data/test/perf_strict.rb +0 -128
  136. data/test/rails.rb +0 -50
  137. data/test/russian.rb +0 -18
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -55
  151. data/test/sample_json.rb +0 -37
  152. data/test/struct.rb +0 -29
  153. data/test/test_compat.rb +0 -398
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -458
  156. data/test/test_file.rb +0 -245
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_object.rb +0 -745
  160. data/test/test_saj.rb +0 -186
  161. data/test/test_scp.rb +0 -396
  162. data/test/test_serializer.rb +0 -59
  163. data/test/test_strict.rb +0 -254
  164. data/test/test_various.rb +0 -1383
  165. data/test/test_writer.rb +0 -308
  166. data/test/write_timebars.rb +0 -31
@@ -0,0 +1,329 @@
1
+ // Copyright (c) 2012, 2017 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include <errno.h>
5
+ #include <ruby.h>
6
+
7
+ #include "encode.h"
8
+ #include "mem.h"
9
+
10
+ extern VALUE Oj;
11
+
12
+ static void stream_writer_free(void *ptr) {
13
+ StreamWriter sw;
14
+
15
+ if (0 == ptr) {
16
+ return;
17
+ }
18
+ sw = (StreamWriter)ptr;
19
+ OJ_R_FREE(sw->sw.out.buf);
20
+ OJ_R_FREE(sw->sw.types);
21
+ OJ_R_FREE(ptr);
22
+ }
23
+
24
+ static const rb_data_type_t oj_stream_writer_type = {
25
+ "Oj/stream_writer",
26
+ {
27
+ NULL,
28
+ stream_writer_free,
29
+ NULL,
30
+ },
31
+ 0,
32
+ 0,
33
+ };
34
+
35
+ static void stream_writer_reset_buf(StreamWriter sw) {
36
+ sw->sw.out.cur = sw->sw.out.buf;
37
+ *sw->sw.out.cur = '\0';
38
+ }
39
+
40
+ static void stream_writer_write(StreamWriter sw) {
41
+ ssize_t size = sw->sw.out.cur - sw->sw.out.buf;
42
+
43
+ switch (sw->type) {
44
+ case STRING_IO:
45
+ case STREAM_IO:
46
+ case FILE_IO: {
47
+ volatile VALUE rs = rb_utf8_str_new(sw->sw.out.buf, size);
48
+ rb_funcall(sw->stream, oj_write_id, 1, rs);
49
+ break;
50
+ }
51
+ default: rb_raise(rb_eArgError, "expected an IO Object.");
52
+ }
53
+ stream_writer_reset_buf(sw);
54
+ }
55
+
56
+ static VALUE buffer_size_sym = Qundef;
57
+
58
+ /* Document-method: new
59
+ * call-seq: new(io, options)
60
+ *
61
+ * Creates a new StreamWriter. Options are supported according the specified
62
+ * mode or the mode in the default options. Note that if mimic_JSON or
63
+ * Oj.optimize_rails has not been called then the behavior of the modes may
64
+ * not be the same as if they were.
65
+ *
66
+ * In addition to the regular dump options for the various modes a
67
+ * _:buffer_size_ option is available. It should be set to a positive
68
+ * integer. It is considered a hint of how large the initial internal buffer
69
+ * should be and also a hint on when to flush.
70
+ *
71
+ * - *io* [_IO_] stream to write to
72
+ * - *options* [_Hash_] formatting options
73
+ */
74
+ static VALUE stream_writer_new(int argc, VALUE *argv, VALUE self) {
75
+ StreamWriterType type = STREAM_IO;
76
+ int fd = 0;
77
+ VALUE stream = argv[0];
78
+ VALUE clas = rb_obj_class(stream);
79
+ StreamWriter sw;
80
+ #if !IS_WINDOWS
81
+ VALUE s;
82
+ #endif
83
+
84
+ if (oj_stringio_class == clas) {
85
+ type = STRING_IO;
86
+ #if !IS_WINDOWS
87
+ } else if (rb_respond_to(stream, oj_fileno_id) && Qnil != (s = rb_funcall(stream, oj_fileno_id, 0)) &&
88
+ 0 != (fd = FIX2INT(s))) {
89
+ type = FILE_IO;
90
+ #endif
91
+ } else if (rb_respond_to(stream, oj_write_id)) {
92
+ type = STREAM_IO;
93
+ } else {
94
+ rb_raise(rb_eArgError, "expected an IO Object.");
95
+ }
96
+ sw = OJ_R_ALLOC(struct _streamWriter);
97
+ if (2 == argc && T_HASH == rb_type(argv[1])) {
98
+ volatile VALUE v;
99
+ int buf_size = 0;
100
+
101
+ if (Qundef == buffer_size_sym) {
102
+ buffer_size_sym = ID2SYM(rb_intern("buffer_size"));
103
+ rb_gc_register_address(&buffer_size_sym);
104
+ }
105
+ if (Qnil != (v = rb_hash_lookup(argv[1], buffer_size_sym))) {
106
+ if (rb_cInteger != rb_obj_class(v)) {
107
+ OJ_R_FREE(sw);
108
+ rb_raise(rb_eArgError, ":buffer size must be a Integer.");
109
+ }
110
+ buf_size = FIX2INT(v);
111
+ }
112
+ oj_str_writer_init(&sw->sw, buf_size);
113
+ oj_parse_options(argv[1], &sw->sw.opts);
114
+ sw->flush_limit = buf_size;
115
+ } else {
116
+ oj_str_writer_init(&sw->sw, 4096);
117
+ sw->flush_limit = 0;
118
+ }
119
+ sw->sw.out.indent = sw->sw.opts.indent;
120
+ sw->stream = stream;
121
+ sw->type = type;
122
+ sw->fd = fd;
123
+
124
+ return TypedData_Wrap_Struct(oj_stream_writer_class, &oj_stream_writer_type, sw);
125
+ }
126
+
127
+ /* Document-method: push_key
128
+ * call-seq: push_key(key)
129
+ *
130
+ * Pushes a key onto the JSON document. The key will be used for the next push
131
+ * if currently in a JSON object and ignored otherwise. If a key is provided on
132
+ * the next push then that new key will be ignored.
133
+ *
134
+ * - *key* [_String_] the key pending for the next push
135
+ */
136
+ static VALUE stream_writer_push_key(VALUE self, VALUE key) {
137
+ StreamWriter sw;
138
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
139
+
140
+ oj_str_writer_push_key(&sw->sw, StringValuePtr(key));
141
+ if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
142
+ stream_writer_write(sw);
143
+ }
144
+ return Qnil;
145
+ }
146
+
147
+ /* Document-method: push_object
148
+ * call-seq: push_object(key=nil)
149
+ *
150
+ * Pushes an object onto the JSON document. Future pushes will be to this object
151
+ * until a pop() is called.
152
+ *
153
+ * - *key* [_String_] the key if adding to an object in the JSON document
154
+ */
155
+ static VALUE stream_writer_push_object(int argc, VALUE *argv, VALUE self) {
156
+ StreamWriter sw;
157
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
158
+
159
+ switch (argc) {
160
+ case 0: oj_str_writer_push_object(&sw->sw, 0); break;
161
+ case 1:
162
+ if (Qnil == argv[0]) {
163
+ oj_str_writer_push_object(&sw->sw, 0);
164
+ } else {
165
+ oj_str_writer_push_object(&sw->sw, StringValuePtr(argv[0]));
166
+ }
167
+ break;
168
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
169
+ }
170
+ if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
171
+ stream_writer_write(sw);
172
+ }
173
+ return Qnil;
174
+ }
175
+
176
+ /* Document-method: push_array
177
+ * call-seq: push_array(key=nil)
178
+ *
179
+ * Pushes an array onto the JSON document. Future pushes will be to this object
180
+ * until a pop() is called.
181
+ *
182
+ * - *key* [_String_] the key if adding to an object in the JSON document
183
+ */
184
+ static VALUE stream_writer_push_array(int argc, VALUE *argv, VALUE self) {
185
+ StreamWriter sw;
186
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
187
+
188
+ switch (argc) {
189
+ case 0: oj_str_writer_push_array(&sw->sw, 0); break;
190
+ case 1:
191
+ if (Qnil == argv[0]) {
192
+ oj_str_writer_push_array(&sw->sw, 0);
193
+ } else {
194
+ oj_str_writer_push_array(&sw->sw, StringValuePtr(argv[0]));
195
+ }
196
+ break;
197
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_object'."); break;
198
+ }
199
+ if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
200
+ stream_writer_write(sw);
201
+ }
202
+ return Qnil;
203
+ }
204
+
205
+ /* Document-method: push_value
206
+ * call-seq: push_value(value, key=nil)
207
+ *
208
+ * Pushes a value onto the JSON document.
209
+ * - *value* [_Object_] value to add to the JSON document
210
+ * - *key* [_String_] the key if adding to an object in the JSON document
211
+ */
212
+ static VALUE stream_writer_push_value(int argc, VALUE *argv, VALUE self) {
213
+ StreamWriter sw;
214
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
215
+
216
+ switch (argc) {
217
+ case 1: oj_str_writer_push_value((StrWriter)sw, *argv, 0); break;
218
+ case 2:
219
+ if (Qnil == argv[1]) {
220
+ oj_str_writer_push_value((StrWriter)sw, *argv, 0);
221
+ } else {
222
+ oj_str_writer_push_value((StrWriter)sw, *argv, StringValuePtr(argv[1]));
223
+ }
224
+ break;
225
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_value'."); break;
226
+ }
227
+ if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
228
+ stream_writer_write(sw);
229
+ }
230
+ return Qnil;
231
+ }
232
+
233
+ /* Document-method: push_json
234
+ * call-seq: push_json(value, key=nil)
235
+ *
236
+ * Pushes a string onto the JSON document. The String must be a valid JSON
237
+ * encoded string. No additional checking is done to verify the validity of the
238
+ * string.
239
+ * - *value* [_Object_] value to add to the JSON document
240
+ * - *key* [_String_] the key if adding to an object in the JSON document
241
+ */
242
+ static VALUE stream_writer_push_json(int argc, VALUE *argv, VALUE self) {
243
+ StreamWriter sw;
244
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
245
+
246
+ switch (argc) {
247
+ case 1: oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0); break;
248
+ case 2:
249
+ if (Qnil == argv[1]) {
250
+ oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), 0);
251
+ } else {
252
+ oj_str_writer_push_json((StrWriter)sw, StringValuePtr(*argv), StringValuePtr(argv[1]));
253
+ }
254
+ break;
255
+ default: rb_raise(rb_eArgError, "Wrong number of argument to 'push_json'."); break;
256
+ }
257
+ if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
258
+ stream_writer_write(sw);
259
+ }
260
+ return Qnil;
261
+ }
262
+
263
+ /* Document-method: pop
264
+ * call-seq: pop()
265
+ *
266
+ * Pops up a level in the JSON document closing the array or object that is
267
+ * currently open.
268
+ */
269
+ static VALUE stream_writer_pop(VALUE self) {
270
+ StreamWriter sw;
271
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
272
+
273
+ oj_str_writer_pop(&sw->sw);
274
+ if (sw->flush_limit < sw->sw.out.cur - sw->sw.out.buf) {
275
+ stream_writer_write(sw);
276
+ }
277
+ return Qnil;
278
+ }
279
+
280
+ /* Document-method: pop_all
281
+ * call-seq: pop_all()
282
+ *
283
+ * Pops all level in the JSON document closing all the array or object that is
284
+ * currently open.
285
+ */
286
+ static VALUE stream_writer_pop_all(VALUE self) {
287
+ StreamWriter sw;
288
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
289
+
290
+ oj_str_writer_pop_all(&sw->sw);
291
+ stream_writer_write(sw);
292
+
293
+ return Qnil;
294
+ }
295
+
296
+ /* Document-method: flush
297
+ * call-seq: flush()
298
+ *
299
+ * Flush any remaining characters in the buffer.
300
+ */
301
+ static VALUE stream_writer_flush(VALUE self) {
302
+ StreamWriter sw;
303
+ TypedData_Get_Struct(self, struct _streamWriter, &oj_stream_writer_type, sw);
304
+ stream_writer_write(sw);
305
+
306
+ return Qnil;
307
+ }
308
+
309
+ /* Document-class: Oj::StreamWriter
310
+ *
311
+ * Supports building a JSON document one element at a time. Build the IO stream
312
+ * document by pushing values into the document. Pushing an array or an object
313
+ * will create that element in the JSON document and subsequent pushes will add
314
+ * the elements to that array or object until a pop() is called.
315
+ */
316
+ void oj_stream_writer_init(void) {
317
+ oj_stream_writer_class = rb_define_class_under(Oj, "StreamWriter", rb_cObject);
318
+ rb_gc_register_address(&oj_stream_writer_class);
319
+ rb_undef_alloc_func(oj_stream_writer_class);
320
+ rb_define_module_function(oj_stream_writer_class, "new", stream_writer_new, -1);
321
+ rb_define_method(oj_stream_writer_class, "push_key", stream_writer_push_key, 1);
322
+ rb_define_method(oj_stream_writer_class, "push_object", stream_writer_push_object, -1);
323
+ rb_define_method(oj_stream_writer_class, "push_array", stream_writer_push_array, -1);
324
+ rb_define_method(oj_stream_writer_class, "push_value", stream_writer_push_value, -1);
325
+ rb_define_method(oj_stream_writer_class, "push_json", stream_writer_push_json, -1);
326
+ rb_define_method(oj_stream_writer_class, "pop", stream_writer_pop, 0);
327
+ rb_define_method(oj_stream_writer_class, "pop_all", stream_writer_pop_all, 0);
328
+ rb_define_method(oj_stream_writer_class, "flush", stream_writer_flush, 0);
329
+ }
data/ext/oj/strict.c CHANGED
@@ -1,185 +1,187 @@
1
- /* strict.c
2
- * Copyright (c) 2012, Peter Ohler
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * - Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- *
11
- * - Redistributions in binary form must reproduce the above copyright notice,
12
- * this list of conditions and the following disclaimer in the documentation
13
- * and/or other materials provided with the distribution.
14
- *
15
- * - Neither the name of Peter Ohler nor the names of its contributors may be
16
- * used to endorse or promote products derived from this software without
17
- * specific prior written permission.
18
- *
19
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
- */
1
+ // Copyright (c) 2012 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
30
3
 
31
- #include <stdlib.h>
32
4
  #include <stdio.h>
5
+ #include <stdlib.h>
33
6
  #include <string.h>
34
7
  #include <unistd.h>
35
8
 
36
- #include "oj.h"
9
+ #include "encode.h"
37
10
  #include "err.h"
11
+ #include "intern.h"
12
+ #include "oj.h"
38
13
  #include "parse.h"
39
- #include "encode.h"
14
+ #include "trace.h"
40
15
 
41
- static void
42
- noop_end(struct _ParseInfo *pi) {
16
+ VALUE oj_cstr_to_value(const char *str, size_t len, size_t cache_str) {
17
+ volatile VALUE rstr = Qnil;
18
+
19
+ if (len < cache_str) {
20
+ rstr = oj_str_intern(str, len);
21
+ } else {
22
+ rstr = rb_utf8_str_new(str, len);
23
+ }
24
+ return rstr;
43
25
  }
44
26
 
45
- static VALUE
46
- noop_hash_key(struct _ParseInfo *pi, const char *key, size_t klen) {
27
+ VALUE oj_calc_hash_key(ParseInfo pi, Val parent) {
28
+ volatile VALUE rkey = parent->key_val;
29
+
30
+ if (Qundef != rkey) {
31
+ return rkey;
32
+ }
33
+ if (Yes != pi->options.cache_keys) {
34
+ if (Yes == pi->options.sym_key) {
35
+ rkey = ID2SYM(rb_intern3(parent->key, parent->klen, oj_utf8_encoding));
36
+ } else {
37
+ rkey = rb_utf8_str_new(parent->key, parent->klen);
38
+ OBJ_FREEZE(rkey); // frozen when used as a Hash key anyway
39
+ }
40
+ return rkey;
41
+ }
42
+ if (Yes == pi->options.sym_key) {
43
+ rkey = oj_sym_intern(parent->key, parent->klen);
44
+ } else {
45
+ rkey = oj_str_intern(parent->key, parent->klen);
46
+ }
47
+ return rkey;
48
+ }
49
+
50
+ static void hash_end(ParseInfo pi) {
51
+ TRACE_PARSE_HASH_END(pi->options.trace, pi);
52
+ }
53
+
54
+ static void array_end(ParseInfo pi) {
55
+ TRACE_PARSE_ARRAY_END(pi->options.trace, pi);
56
+ }
57
+
58
+ static VALUE noop_hash_key(ParseInfo pi, const char *key, size_t klen) {
47
59
  return Qundef;
48
60
  }
49
61
 
50
- static void
51
- add_value(ParseInfo pi, VALUE val) {
62
+ static void add_value(ParseInfo pi, VALUE val) {
63
+ TRACE_PARSE_CALL(pi->options.trace, "add_value", pi, val);
52
64
  pi->stack.head->val = val;
53
65
  }
54
66
 
55
- static void
56
- add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
57
- volatile VALUE rstr = rb_str_new(str, len);
67
+ static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
68
+ volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
58
69
 
59
- rstr = oj_encode(rstr);
60
70
  pi->stack.head->val = rstr;
71
+ TRACE_PARSE_CALL(pi->options.trace, "add_string", pi, rstr);
61
72
  }
62
73
 
63
- static void
64
- add_num(ParseInfo pi, NumInfo ni) {
74
+ static void add_num(ParseInfo pi, NumInfo ni) {
65
75
  if (ni->infinity || ni->nan) {
66
- oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
76
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
67
77
  }
68
78
  pi->stack.head->val = oj_num_as_value(ni);
79
+ TRACE_PARSE_CALL(pi->options.trace, "add_number", pi, pi->stack.head->val);
69
80
  }
70
81
 
71
- static VALUE
72
- start_hash(ParseInfo pi) {
82
+ static VALUE start_hash(ParseInfo pi) {
73
83
  if (Qnil != pi->options.hash_class) {
74
- return rb_class_new_instance(0, NULL, pi->options.hash_class);
84
+ return rb_class_new_instance(0, NULL, pi->options.hash_class);
75
85
  }
86
+ TRACE_PARSE_IN(pi->options.trace, "start_hash", pi);
76
87
  return rb_hash_new();
77
88
  }
78
89
 
79
- static VALUE
80
- calc_hash_key(ParseInfo pi, Val parent) {
81
- volatile VALUE rkey = parent->key_val;
90
+ static void hash_set_cstr(ParseInfo pi, Val parent, const char *str, size_t len, const char *orig) {
91
+ volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
82
92
 
83
- if (Qundef == rkey) {
84
- rkey = rb_str_new(parent->key, parent->klen);
85
- }
86
- rkey = oj_encode(rkey);
87
- if (Yes == pi->options.sym_key) {
88
- rkey = rb_str_intern(rkey);
89
- }
90
- return rkey;
93
+ rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), rstr);
94
+ TRACE_PARSE_CALL(pi->options.trace, "set_string", pi, rstr);
91
95
  }
92
96
 
93
- static void
94
- hash_set_cstr(ParseInfo pi, Val parent, const char *str, size_t len, const char *orig) {
95
- volatile VALUE rstr = rb_str_new(str, len);
96
-
97
- rstr = oj_encode(rstr);
98
- rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), rstr);
99
- }
97
+ static void hash_set_num(ParseInfo pi, Val parent, NumInfo ni) {
98
+ volatile VALUE v;
100
99
 
101
- static void
102
- hash_set_num(struct _ParseInfo *pi, Val parent, NumInfo ni) {
103
100
  if (ni->infinity || ni->nan) {
104
- oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
101
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
105
102
  }
106
- rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), oj_num_as_value(ni));
103
+ v = oj_num_as_value(ni);
104
+ rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), v);
105
+ TRACE_PARSE_CALL(pi->options.trace, "set_number", pi, v);
107
106
  }
108
107
 
109
- static void
110
- hash_set_value(ParseInfo pi, Val parent, VALUE value) {
111
- rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), value);
108
+ static void hash_set_value(ParseInfo pi, Val parent, VALUE value) {
109
+ rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), value);
110
+ TRACE_PARSE_CALL(pi->options.trace, "set_value", pi, value);
112
111
  }
113
112
 
114
- static VALUE
115
- start_array(ParseInfo pi) {
113
+ static VALUE start_array(ParseInfo pi) {
114
+ TRACE_PARSE_IN(pi->options.trace, "start_array", pi);
116
115
  return rb_ary_new();
117
116
  }
118
117
 
119
- static void
120
- array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
121
- volatile VALUE rstr = rb_str_new(str, len);
118
+ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) {
119
+ volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
122
120
 
123
- rstr = oj_encode(rstr);
124
121
  rb_ary_push(stack_peek(&pi->stack)->val, rstr);
122
+ TRACE_PARSE_CALL(pi->options.trace, "append_string", pi, rstr);
125
123
  }
126
124
 
127
- static void
128
- array_append_num(ParseInfo pi, NumInfo ni) {
125
+ static void array_append_num(ParseInfo pi, NumInfo ni) {
126
+ volatile VALUE v;
127
+
129
128
  if (ni->infinity || ni->nan) {
130
- oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
129
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
131
130
  }
132
- rb_ary_push(stack_peek(&pi->stack)->val, oj_num_as_value(ni));
131
+ v = oj_num_as_value(ni);
132
+ rb_ary_push(stack_peek(&pi->stack)->val, v);
133
+ TRACE_PARSE_CALL(pi->options.trace, "append_number", pi, v);
133
134
  }
134
135
 
135
- static void
136
- array_append_value(ParseInfo pi, VALUE value) {
136
+ static void array_append_value(ParseInfo pi, VALUE value) {
137
137
  rb_ary_push(stack_peek(&pi->stack)->val, value);
138
- }
139
-
140
- void
141
- oj_set_strict_callbacks(ParseInfo pi) {
142
- pi->start_hash = start_hash;
143
- pi->end_hash = noop_end;
144
- pi->hash_key = noop_hash_key;
145
- pi->hash_set_cstr = hash_set_cstr;
146
- pi->hash_set_num = hash_set_num;
147
- pi->hash_set_value = hash_set_value;
148
- pi->start_array = start_array;
149
- pi->end_array = noop_end;
150
- pi->array_append_cstr = array_append_cstr;
151
- pi->array_append_num = array_append_num;
138
+ TRACE_PARSE_CALL(pi->options.trace, "append_value", pi, value);
139
+ }
140
+
141
+ void oj_set_strict_callbacks(ParseInfo pi) {
142
+ pi->start_hash = start_hash;
143
+ pi->end_hash = hash_end;
144
+ pi->hash_key = noop_hash_key;
145
+ pi->hash_set_cstr = hash_set_cstr;
146
+ pi->hash_set_num = hash_set_num;
147
+ pi->hash_set_value = hash_set_value;
148
+ pi->start_array = start_array;
149
+ pi->end_array = array_end;
150
+ pi->array_append_cstr = array_append_cstr;
151
+ pi->array_append_num = array_append_num;
152
152
  pi->array_append_value = array_append_value;
153
- pi->add_cstr = add_cstr;
154
- pi->add_num = add_num;
155
- pi->add_value = add_value;
156
- pi->expect_value = 1;
153
+ pi->add_cstr = add_cstr;
154
+ pi->add_num = add_num;
155
+ pi->add_value = add_value;
156
+ pi->expect_value = 1;
157
157
  }
158
158
 
159
159
  VALUE
160
160
  oj_strict_parse(int argc, VALUE *argv, VALUE self) {
161
- struct _ParseInfo pi;
161
+ struct _parseInfo pi;
162
162
 
163
- pi.options = oj_default_options;
164
- pi.handler = Qnil;
163
+ parse_info_init(&pi);
164
+ pi.options = oj_default_options;
165
+ pi.handler = Qnil;
165
166
  pi.err_class = Qnil;
166
167
  oj_set_strict_callbacks(&pi);
167
168
 
168
169
  if (T_STRING == rb_type(*argv)) {
169
- return oj_pi_parse(argc, argv, &pi, 0, 0, 1);
170
+ return oj_pi_parse(argc, argv, &pi, 0, 0, true);
170
171
  } else {
171
- return oj_pi_sparse(argc, argv, &pi, 0);
172
+ return oj_pi_sparse(argc, argv, &pi, 0);
172
173
  }
173
174
  }
174
175
 
175
176
  VALUE
176
177
  oj_strict_parse_cstr(int argc, VALUE *argv, char *json, size_t len) {
177
- struct _ParseInfo pi;
178
+ struct _parseInfo pi;
178
179
 
179
- pi.options = oj_default_options;
180
- pi.handler = Qnil;
180
+ parse_info_init(&pi);
181
+ pi.options = oj_default_options;
182
+ pi.handler = Qnil;
181
183
  pi.err_class = Qnil;
182
184
  oj_set_strict_callbacks(&pi);
183
185
 
184
- return oj_pi_parse(argc, argv, &pi, json, len, 1);
186
+ return oj_pi_parse(argc, argv, &pi, json, len, true);
185
187
  }