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/odd.c ADDED
@@ -0,0 +1,230 @@
1
+ /* odd.c
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #include <string.h>
7
+
8
+ #include "odd.h"
9
+
10
+ static struct _Odd _odds[4]; // bump up if new initial Odd classes are added
11
+ static struct _Odd *odds = _odds;
12
+ static long odd_cnt = 0;
13
+ static ID sec_id;
14
+ static ID sec_fraction_id;
15
+ static ID to_f_id;
16
+ static ID numerator_id;
17
+ static ID denominator_id;
18
+ static ID rational_id;
19
+ static VALUE rational_class;
20
+
21
+ static void
22
+ set_class(Odd odd, const char *classname) {
23
+ const char **np;
24
+ ID *idp;
25
+
26
+ odd->classname = classname;
27
+ odd->clen = strlen(classname);
28
+ odd->clas = rb_const_get(rb_cObject, rb_intern(classname));
29
+ odd->create_obj = odd->clas;
30
+ odd->create_op = rb_intern("new");
31
+ odd->is_module = (T_MODULE == rb_type(odd->clas));
32
+ odd->raw = 0;
33
+ for (np = odd->attr_names, idp = odd->attrs; 0 != *np; np++, idp++) {
34
+ *idp = rb_intern(*np);
35
+ }
36
+ *idp = 0;
37
+ }
38
+
39
+ static VALUE
40
+ get_datetime_secs(VALUE obj) {
41
+ VALUE rsecs = rb_funcall(obj, sec_id, 0);
42
+ VALUE rfrac = rb_funcall(obj, sec_fraction_id, 0);
43
+ long sec = NUM2LONG(rsecs);
44
+ long long num = rb_num2ll(rb_funcall(rfrac, numerator_id, 0));
45
+ long long den = rb_num2ll(rb_funcall(rfrac, denominator_id, 0));
46
+
47
+ #if DATETIME_1_8
48
+ num *= 86400;
49
+ #endif
50
+ num += sec * den;
51
+
52
+ return rb_funcall(rb_cObject, rational_id, 2, rb_ll2inum(num), rb_ll2inum(den));
53
+ }
54
+
55
+ void
56
+ oj_odd_init() {
57
+ Odd odd;
58
+ const char **np;
59
+
60
+ sec_id = rb_intern("sec");
61
+ sec_fraction_id = rb_intern("sec_fraction");
62
+ to_f_id = rb_intern("to_f");
63
+ numerator_id = rb_intern("numerator");
64
+ denominator_id = rb_intern("denominator");
65
+ rational_id = rb_intern("Rational");
66
+ rational_class = rb_const_get(rb_cObject, rational_id);
67
+
68
+ memset(_odds, 0, sizeof(_odds));
69
+ odd = odds;
70
+ // Rational
71
+ np = odd->attr_names;
72
+ *np++ = "numerator";
73
+ *np++ = "denominator";
74
+ *np = 0;
75
+ set_class(odd, "Rational");
76
+ odd->create_obj = rb_cObject;
77
+ odd->create_op = rational_id;
78
+ odd->attr_cnt = 2;
79
+ // Date
80
+ odd++;
81
+ np = odd->attr_names;
82
+ *np++ = "year";
83
+ *np++ = "month";
84
+ *np++ = "day";
85
+ *np++ = "start";
86
+ *np++ = 0;
87
+ set_class(odd, "Date");
88
+ odd->attr_cnt = 4;
89
+ // DateTime
90
+ odd++;
91
+ np = odd->attr_names;
92
+ *np++ = "year";
93
+ *np++ = "month";
94
+ *np++ = "day";
95
+ *np++ = "hour";
96
+ *np++ = "min";
97
+ *np++ = "sec";
98
+ *np++ = "offset";
99
+ *np++ = "start";
100
+ *np++ = 0;
101
+ set_class(odd, "DateTime");
102
+ odd->attr_cnt = 8;
103
+ odd->attrFuncs[5] = get_datetime_secs;
104
+ // Range
105
+ odd++;
106
+ np = odd->attr_names;
107
+ *np++ = "begin";
108
+ *np++ = "end";
109
+ *np++ = "exclude_end?";
110
+ *np++ = 0;
111
+ set_class(odd, "Range");
112
+ odd->attr_cnt = 3;
113
+
114
+ odd_cnt = odd - odds + 1;
115
+ }
116
+
117
+ Odd
118
+ oj_get_odd(VALUE clas) {
119
+ Odd odd;
120
+ const char *classname = NULL;
121
+
122
+ for (odd = odds + odd_cnt - 1; odds <= odd; odd--) {
123
+ if (clas == odd->clas) {
124
+ return odd;
125
+ }
126
+ if (odd->is_module) {
127
+ if (NULL == classname) {
128
+ classname = rb_class2name(clas);
129
+ }
130
+ if (0 == strncmp(odd->classname, classname, odd->clen) &&
131
+ ':' == classname[odd->clen]) {
132
+ return odd;
133
+ }
134
+ }
135
+ }
136
+ return NULL;
137
+ }
138
+
139
+ Odd
140
+ oj_get_oddc(const char *classname, size_t len) {
141
+ Odd odd;
142
+
143
+ for (odd = odds + odd_cnt - 1; odds <= odd; odd--) {
144
+ if (len == odd->clen && 0 == strncmp(classname, odd->classname, len)) {
145
+ return odd;
146
+ }
147
+ if (odd->is_module &&
148
+ 0 == strncmp(odd->classname, classname, odd->clen) &&
149
+ ':' == classname[odd->clen]) {
150
+ return odd;
151
+ }
152
+ }
153
+ return 0;
154
+ }
155
+
156
+ OddArgs
157
+ oj_odd_alloc_args(Odd odd) {
158
+ OddArgs oa = ALLOC_N(struct _OddArgs, 1);
159
+ VALUE *a;
160
+ int i;
161
+
162
+ oa->odd = odd;
163
+ for (i = odd->attr_cnt, a = oa->args; 0 < i; i--, a++) {
164
+ *a = Qnil;
165
+ }
166
+ return oa;
167
+ }
168
+
169
+ void
170
+ oj_odd_free(OddArgs args) {
171
+ xfree(args);
172
+ }
173
+
174
+ int
175
+ oj_odd_set_arg(OddArgs args, const char *key, size_t klen, VALUE value) {
176
+ const char **np;
177
+ VALUE *vp;
178
+ int i;
179
+
180
+ for (i = args->odd->attr_cnt, np = args->odd->attr_names, vp = args->args; 0 < i; i--, np++, vp++) {
181
+ if (0 == strncmp(key, *np, klen) && '\0' == *((*np) + klen)) {
182
+ *vp = value;
183
+ return 0;
184
+ }
185
+ }
186
+ return -1;
187
+ }
188
+
189
+ void
190
+ oj_reg_odd(VALUE clas, VALUE create_object, VALUE create_method, int mcnt, VALUE *members, bool raw) {
191
+ Odd odd;
192
+ const char **np;
193
+ ID *ap;
194
+ AttrGetFunc *fp;
195
+
196
+ if (_odds == odds) {
197
+ odds = ALLOC_N(struct _Odd, odd_cnt + 1);
198
+
199
+ memcpy(odds, _odds, sizeof(struct _Odd) * odd_cnt);
200
+ } else {
201
+ REALLOC_N(odds, struct _Odd, odd_cnt + 1);
202
+ }
203
+ odd = odds + odd_cnt;
204
+ odd->clas = clas;
205
+ odd->classname = strdup(rb_class2name(clas));
206
+ odd->clen = strlen(odd->classname);
207
+ odd->create_obj = create_object;
208
+ odd->create_op = SYM2ID(create_method);
209
+ odd->attr_cnt = mcnt;
210
+ odd->is_module = (T_MODULE == rb_type(clas));
211
+ odd->raw = raw;
212
+ for (ap = odd->attrs, np = odd->attr_names, fp = odd->attrFuncs; 0 < mcnt; mcnt--, ap++, np++, members++, fp++) {
213
+ *fp = 0;
214
+ switch (rb_type(*members)) {
215
+ case T_STRING:
216
+ *np = strdup(rb_string_value_ptr(members));
217
+ break;
218
+ case T_SYMBOL:
219
+ *np = rb_id2name(SYM2ID(*members));
220
+ break;
221
+ default:
222
+ rb_raise(rb_eArgError, "registered member identifiers must be Strings or Symbols.");
223
+ break;
224
+ }
225
+ *ap = rb_intern(*np);
226
+ }
227
+ *np = 0;
228
+ *ap = 0;
229
+ odd_cnt++;
230
+ }
data/ext/oj/odd.h ADDED
@@ -0,0 +1,44 @@
1
+ /* odd.h
2
+ * Copyright (c) 2011, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #ifndef __OJ_ODD_H__
7
+ #define __OJ_ODD_H__
8
+
9
+ #include <stdbool.h>
10
+
11
+ #include "ruby.h"
12
+
13
+ #define MAX_ODD_ARGS 10
14
+
15
+ typedef VALUE (*AttrGetFunc)(VALUE obj);
16
+
17
+ typedef struct _Odd {
18
+ const char *classname;
19
+ size_t clen;
20
+ VALUE clas; // Ruby class or module
21
+ VALUE create_obj;
22
+ ID create_op;
23
+ int attr_cnt;
24
+ bool is_module;
25
+ bool raw;
26
+ const char *attr_names[MAX_ODD_ARGS]; // NULL terminated attr names
27
+ ID attrs[MAX_ODD_ARGS]; // 0 terminated attr IDs
28
+ AttrGetFunc attrFuncs[MAX_ODD_ARGS];
29
+ } *Odd;
30
+
31
+ typedef struct _OddArgs {
32
+ Odd odd;
33
+ VALUE args[MAX_ODD_ARGS];
34
+ } *OddArgs;
35
+
36
+ extern void oj_odd_init(void);
37
+ extern Odd oj_get_odd(VALUE clas);
38
+ extern Odd oj_get_oddc(const char *classname, size_t len);
39
+ extern OddArgs oj_odd_alloc_args(Odd odd);
40
+ extern void oj_odd_free(OddArgs args);
41
+ extern int oj_odd_set_arg(OddArgs args, const char *key, size_t klen, VALUE value);
42
+ extern void oj_reg_odd(VALUE clas, VALUE create_object, VALUE create_method, int mcnt, VALUE *members, bool raw);
43
+
44
+ #endif /* __OJ_ODD_H__ */