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
@@ -0,0 +1,254 @@
1
+ /* dump_leaf.c
2
+ * Copyright (c) 2012, 2017, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #include <errno.h>
7
+
8
+ #include "oj.h"
9
+ #include "dump.h"
10
+
11
+ static void dump_leaf(Leaf leaf, int depth, Out out);
12
+
13
+ extern void dump_cstr(const char *str, size_t cnt, int is_sym, int escape1, Out out);
14
+
15
+ static void
16
+ grow(Out out, size_t len) {
17
+ size_t size = out->end - out->buf;
18
+ long pos = out->cur - out->buf;
19
+ char *buf;
20
+
21
+ size *= 2;
22
+ if (size <= len * 2 + pos) {
23
+ size += len;
24
+ }
25
+ if (out->allocated) {
26
+ buf = REALLOC_N(out->buf, char, (size + BUFFER_EXTRA));
27
+ } else {
28
+ buf = ALLOC_N(char, (size + BUFFER_EXTRA));
29
+ out->allocated = 1;
30
+ memcpy(buf, out->buf, out->end - out->buf + BUFFER_EXTRA);
31
+ }
32
+ if (0 == buf) {
33
+ rb_raise(rb_eNoMemError, "Failed to create string. [%d:%s]\n", ENOSPC, strerror(ENOSPC));
34
+ }
35
+ out->buf = buf;
36
+ out->end = buf + size;
37
+ out->cur = out->buf + pos;
38
+ }
39
+
40
+
41
+ inline static void
42
+ dump_chars(const char *s, size_t size, Out out) {
43
+ if (out->end - out->cur <= (long)size) {
44
+ grow(out, size);
45
+ }
46
+ memcpy(out->cur, s, size);
47
+ out->cur += size;
48
+ *out->cur = '\0';
49
+ }
50
+
51
+ static void
52
+ dump_leaf_str(Leaf leaf, Out out) {
53
+ switch (leaf->value_type) {
54
+ case STR_VAL:
55
+ dump_cstr(leaf->str, strlen(leaf->str), 0, 0, out);
56
+ break;
57
+ case RUBY_VAL:
58
+ dump_cstr(rb_string_value_cstr(&leaf->value), RSTRING_LEN(leaf->value), 0, 0, out);
59
+ break;
60
+ case COL_VAL:
61
+ default:
62
+ rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
63
+ break;
64
+ }
65
+ }
66
+
67
+ static void
68
+ dump_leaf_fixnum(Leaf leaf, Out out) {
69
+ switch (leaf->value_type) {
70
+ case STR_VAL:
71
+ dump_chars(leaf->str, strlen(leaf->str), out);
72
+ break;
73
+ case RUBY_VAL:
74
+ if (T_BIGNUM == rb_type(leaf->value)) {
75
+ oj_dump_bignum(leaf->value, 0, out, false);
76
+ } else {
77
+ oj_dump_fixnum(leaf->value, 0, out, false);
78
+ }
79
+ break;
80
+ case COL_VAL:
81
+ default:
82
+ rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
83
+ break;
84
+ }
85
+ }
86
+
87
+ static void
88
+ dump_leaf_float(Leaf leaf, Out out) {
89
+ switch (leaf->value_type) {
90
+ case STR_VAL:
91
+ dump_chars(leaf->str, strlen(leaf->str), out);
92
+ break;
93
+ case RUBY_VAL:
94
+ oj_dump_float(leaf->value, 0, out, false);
95
+ break;
96
+ case COL_VAL:
97
+ default:
98
+ rb_raise(rb_eTypeError, "Unexpected value type %02x.\n", leaf->value_type);
99
+ break;
100
+ }
101
+ }
102
+
103
+ static void
104
+ dump_leaf_array(Leaf leaf, int depth, Out out) {
105
+ size_t size;
106
+ int d2 = depth + 1;
107
+
108
+ size = 2;
109
+ if (out->end - out->cur <= (long)size) {
110
+ grow(out, size);
111
+ }
112
+ *out->cur++ = '[';
113
+ if (0 == leaf->elements) {
114
+ *out->cur++ = ']';
115
+ } else {
116
+ Leaf first = leaf->elements->next;
117
+ Leaf e = first;
118
+
119
+ size = d2 * out->indent + 2;
120
+ do {
121
+ if (out->end - out->cur <= (long)size) {
122
+ grow(out, size);
123
+ }
124
+ fill_indent(out, d2);
125
+ dump_leaf(e, d2, out);
126
+ if (e->next != first) {
127
+ *out->cur++ = ',';
128
+ }
129
+ e = e->next;
130
+ } while (e != first);
131
+ size = depth * out->indent + 1;
132
+ if (out->end - out->cur <= (long)size) {
133
+ grow(out, size);
134
+ }
135
+ fill_indent(out, depth);
136
+ *out->cur++ = ']';
137
+ }
138
+ *out->cur = '\0';
139
+ }
140
+
141
+ static void
142
+ dump_leaf_hash(Leaf leaf, int depth, Out out) {
143
+ size_t size;
144
+ int d2 = depth + 1;
145
+
146
+ size = 2;
147
+ if (out->end - out->cur <= (long)size) {
148
+ grow(out, size);
149
+ }
150
+ *out->cur++ = '{';
151
+ if (0 == leaf->elements) {
152
+ *out->cur++ = '}';
153
+ } else {
154
+ Leaf first = leaf->elements->next;
155
+ Leaf e = first;
156
+
157
+ size = d2 * out->indent + 2;
158
+ do {
159
+ if (out->end - out->cur <= (long)size) {
160
+ grow(out, size);
161
+ }
162
+ fill_indent(out, d2);
163
+ dump_cstr(e->key, strlen(e->key), 0, 0, out);
164
+ *out->cur++ = ':';
165
+ dump_leaf(e, d2, out);
166
+ if (e->next != first) {
167
+ *out->cur++ = ',';
168
+ }
169
+ e = e->next;
170
+ } while (e != first);
171
+ size = depth * out->indent + 1;
172
+ if (out->end - out->cur <= (long)size) {
173
+ grow(out, size);
174
+ }
175
+ fill_indent(out, depth);
176
+ *out->cur++ = '}';
177
+ }
178
+ *out->cur = '\0';
179
+ }
180
+
181
+ static void
182
+ dump_leaf(Leaf leaf, int depth, Out out) {
183
+ switch (leaf->rtype) {
184
+ case T_NIL:
185
+ oj_dump_nil(Qnil, 0, out, false);
186
+ break;
187
+ case T_TRUE:
188
+ oj_dump_true(Qtrue, 0, out, false);
189
+ break;
190
+ case T_FALSE:
191
+ oj_dump_false(Qfalse, 0, out, false);
192
+ break;
193
+ case T_STRING:
194
+ dump_leaf_str(leaf, out);
195
+ break;
196
+ case T_FIXNUM:
197
+ dump_leaf_fixnum(leaf, out);
198
+ break;
199
+ case T_FLOAT:
200
+ dump_leaf_float(leaf, out);
201
+ break;
202
+ case T_ARRAY:
203
+ dump_leaf_array(leaf, depth, out);
204
+ break;
205
+ case T_HASH:
206
+ dump_leaf_hash(leaf, depth, out);
207
+ break;
208
+ default:
209
+ rb_raise(rb_eTypeError, "Unexpected type %02x.\n", leaf->rtype);
210
+ break;
211
+ }
212
+ }
213
+
214
+ void
215
+ oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
216
+ if (0 == out->buf) {
217
+ out->buf = ALLOC_N(char, 4096);
218
+ out->end = out->buf + 4095 - BUFFER_EXTRA; // 1 less than end plus extra for possible errors
219
+ out->allocated = 1;
220
+ }
221
+ out->cur = out->buf;
222
+ out->circ_cnt = 0;
223
+ out->opts = copts;
224
+ out->hash_cnt = 0;
225
+ out->indent = copts->indent;
226
+ dump_leaf(leaf, 0, out);
227
+ }
228
+
229
+ void
230
+ oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
231
+ char buf[4096];
232
+ struct _Out out;
233
+ size_t size;
234
+ FILE *f;
235
+
236
+ out.buf = buf;
237
+ out.end = buf + sizeof(buf) - BUFFER_EXTRA;
238
+ out.allocated = 0;
239
+ out.omit_nil = copts->dump_opts.omit_nil;
240
+ oj_dump_leaf_to_json(leaf, copts, &out);
241
+ size = out.cur - out.buf;
242
+ if (0 == (f = fopen(path, "w"))) {
243
+ rb_raise(rb_eIOError, "%s\n", strerror(errno));
244
+ }
245
+ if (size != fwrite(out.buf, 1, size, f)) {
246
+ int err = ferror(f);
247
+
248
+ rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
249
+ }
250
+ if (out.allocated) {
251
+ xfree(out.buf);
252
+ }
253
+ fclose(f);
254
+ }