oj 3.11.1 → 3.11.6

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 (65) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -1
  3. data/ext/oj/buf.h +34 -38
  4. data/ext/oj/cache8.c +59 -62
  5. data/ext/oj/cache8.h +8 -7
  6. data/ext/oj/circarray.c +33 -35
  7. data/ext/oj/circarray.h +11 -9
  8. data/ext/oj/code.c +170 -174
  9. data/ext/oj/code.h +21 -20
  10. data/ext/oj/compat.c +159 -166
  11. data/ext/oj/custom.c +802 -851
  12. data/ext/oj/dump.c +766 -778
  13. data/ext/oj/dump.h +49 -51
  14. data/ext/oj/dump_compat.c +1 -0
  15. data/ext/oj/dump_leaf.c +116 -157
  16. data/ext/oj/dump_object.c +609 -628
  17. data/ext/oj/dump_strict.c +318 -327
  18. data/ext/oj/encode.h +3 -4
  19. data/ext/oj/err.c +39 -25
  20. data/ext/oj/err.h +24 -15
  21. data/ext/oj/extconf.rb +2 -1
  22. data/ext/oj/fast.c +1042 -1041
  23. data/ext/oj/hash.c +62 -66
  24. data/ext/oj/hash.h +7 -6
  25. data/ext/oj/hash_test.c +450 -443
  26. data/ext/oj/mimic_json.c +412 -402
  27. data/ext/oj/object.c +559 -528
  28. data/ext/oj/odd.c +123 -128
  29. data/ext/oj/odd.h +27 -25
  30. data/ext/oj/oj.c +1123 -924
  31. data/ext/oj/oj.h +286 -298
  32. data/ext/oj/parse.c +938 -930
  33. data/ext/oj/parse.h +70 -69
  34. data/ext/oj/rails.c +836 -839
  35. data/ext/oj/rails.h +7 -7
  36. data/ext/oj/reader.c +135 -140
  37. data/ext/oj/reader.h +66 -79
  38. data/ext/oj/resolve.c +43 -43
  39. data/ext/oj/resolve.h +3 -2
  40. data/ext/oj/rxclass.c +67 -68
  41. data/ext/oj/rxclass.h +12 -10
  42. data/ext/oj/saj.c +451 -479
  43. data/ext/oj/scp.c +93 -103
  44. data/ext/oj/sparse.c +770 -730
  45. data/ext/oj/stream_writer.c +120 -149
  46. data/ext/oj/strict.c +71 -86
  47. data/ext/oj/string_writer.c +198 -243
  48. data/ext/oj/trace.c +29 -33
  49. data/ext/oj/trace.h +14 -11
  50. data/ext/oj/util.c +103 -103
  51. data/ext/oj/util.h +3 -2
  52. data/ext/oj/val_stack.c +47 -47
  53. data/ext/oj/val_stack.h +79 -86
  54. data/ext/oj/wab.c +291 -309
  55. data/lib/oj/bag.rb +1 -0
  56. data/lib/oj/easy_hash.rb +5 -4
  57. data/lib/oj/mimic.rb +0 -12
  58. data/lib/oj/version.rb +1 -1
  59. data/test/activerecord/result_test.rb +7 -2
  60. data/test/foo.rb +35 -32
  61. data/test/test_fast.rb +32 -2
  62. data/test/test_generate.rb +21 -0
  63. data/test/test_hash.rb +10 -0
  64. data/test/test_scp.rb +1 -1
  65. metadata +4 -2
data/ext/oj/code.c CHANGED
@@ -1,232 +1,228 @@
1
1
  // Copyright (c) 2017 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
  #include "code.h"
5
+
4
6
  #include "dump.h"
5
7
 
6
- inline static VALUE
7
- resolve_classname(VALUE mod, const char *classname) {
8
- VALUE clas = Qundef;
9
- ID ci = rb_intern(classname);
8
+ inline static VALUE resolve_classname(VALUE mod, const char *classname) {
9
+ VALUE clas = Qundef;
10
+ ID ci = rb_intern(classname);
10
11
 
11
12
  if (rb_const_defined_at(mod, ci)) {
12
- clas = rb_const_get_at(mod, ci);
13
+ clas = rb_const_get_at(mod, ci);
13
14
  }
14
15
  return clas;
15
16
  }
16
17
 
17
- static VALUE
18
- path2class(const char *name) {
19
- char class_name[1024];
20
- VALUE clas;
21
- char *end = class_name + sizeof(class_name) - 1;
22
- char *s;
23
- const char *n = name;
18
+ static VALUE path2class(const char *name) {
19
+ char class_name[1024];
20
+ VALUE clas;
21
+ char * end = class_name + sizeof(class_name) - 1;
22
+ char * s;
23
+ const char *n = name;
24
24
 
25
25
  clas = rb_cObject;
26
26
  for (s = class_name; '\0' != *n; n++) {
27
- if (':' == *n) {
28
- *s = '\0';
29
- n++;
30
- if (':' != *n) {
31
- return Qundef;
32
- }
33
- if (Qundef == (clas = resolve_classname(clas, class_name))) {
34
- return Qundef;
35
- }
36
- s = class_name;
37
- } else if (end <= s) {
38
- return Qundef;
39
- } else {
40
- *s++ = *n;
41
- }
27
+ if (':' == *n) {
28
+ *s = '\0';
29
+ n++;
30
+ if (':' != *n) {
31
+ return Qundef;
32
+ }
33
+ if (Qundef == (clas = resolve_classname(clas, class_name))) {
34
+ return Qundef;
35
+ }
36
+ s = class_name;
37
+ } else if (end <= s) {
38
+ return Qundef;
39
+ } else {
40
+ *s++ = *n;
41
+ }
42
42
  }
43
43
  *s = '\0';
44
44
 
45
45
  return resolve_classname(clas, class_name);
46
46
  }
47
47
 
48
- bool
49
- oj_code_dump(Code codes, VALUE obj, int depth, Out out) {
50
- VALUE clas = rb_obj_class(obj);
51
- Code c = codes;
48
+ bool oj_code_dump(Code codes, VALUE obj, int depth, Out out) {
49
+ VALUE clas = rb_obj_class(obj);
50
+ Code c = codes;
52
51
 
53
52
  for (; NULL != c->name; c++) {
54
- if (Qundef == c->clas) { // indicates not defined
55
- continue;
56
- }
57
- if (Qnil == c->clas) {
58
- c->clas = path2class(c->name);
59
- }
60
- if (clas == c->clas && c->active) {
61
- c->encode(obj, depth, out);
62
- return true;
63
- }
53
+ if (Qundef == c->clas) { // indicates not defined
54
+ continue;
55
+ }
56
+ if (Qnil == c->clas) {
57
+ c->clas = path2class(c->name);
58
+ }
59
+ if (clas == c->clas && c->active) {
60
+ c->encode(obj, depth, out);
61
+ return true;
62
+ }
64
63
  }
65
64
  return false;
66
65
  }
67
66
 
68
67
  VALUE
69
68
  oj_code_load(Code codes, VALUE clas, VALUE args) {
70
- Code c = codes;
69
+ Code c = codes;
71
70
 
72
71
  for (; NULL != c->name; c++) {
73
- if (Qundef == c->clas) { // indicates not defined
74
- continue;
75
- }
76
- if (Qnil == c->clas) {
77
- c->clas = path2class(c->name);
78
- }
79
- if (clas == c->clas) {
80
- if (NULL == c->decode) {
81
- break;
82
- }
83
- return c->decode(clas, args);
84
- }
72
+ if (Qundef == c->clas) { // indicates not defined
73
+ continue;
74
+ }
75
+ if (Qnil == c->clas) {
76
+ c->clas = path2class(c->name);
77
+ }
78
+ if (clas == c->clas) {
79
+ if (NULL == c->decode) {
80
+ break;
81
+ }
82
+ return c->decode(clas, args);
83
+ }
85
84
  }
86
85
  return Qnil;
87
86
  }
88
87
 
89
- void
90
- oj_code_set_active(Code codes, VALUE clas, bool active) {
91
- Code c = codes;
88
+ void oj_code_set_active(Code codes, VALUE clas, bool active) {
89
+ Code c = codes;
92
90
 
93
91
  for (; NULL != c->name; c++) {
94
- if (Qundef == c->clas) { // indicates not defined
95
- continue;
96
- }
97
- if (Qnil == c->clas) {
98
- c->clas = path2class(c->name);
99
- }
100
- if (clas == c->clas || Qnil == clas) {
101
- c->active = active;
102
- if (Qnil != clas) {
103
- break;
104
- }
105
- }
92
+ if (Qundef == c->clas) { // indicates not defined
93
+ continue;
94
+ }
95
+ if (Qnil == c->clas) {
96
+ c->clas = path2class(c->name);
97
+ }
98
+ if (clas == c->clas || Qnil == clas) {
99
+ c->active = active;
100
+ if (Qnil != clas) {
101
+ break;
102
+ }
103
+ }
106
104
  }
107
105
  }
108
106
 
109
- bool
110
- oj_code_has(Code codes, VALUE clas, bool encode) {
111
- Code c = codes;
107
+ bool oj_code_has(Code codes, VALUE clas, bool encode) {
108
+ Code c = codes;
112
109
 
113
110
  for (; NULL != c->name; c++) {
114
- if (Qundef == c->clas) { // indicates not defined
115
- continue;
116
- }
117
- if (Qnil == c->clas) {
118
- c->clas = path2class(c->name);
119
- }
120
- if (clas == c->clas) {
121
- if (encode) {
122
- return c->active && NULL != c->encode;
123
- } else {
124
- return c->active && NULL != c->decode;
125
- }
126
- }
111
+ if (Qundef == c->clas) { // indicates not defined
112
+ continue;
113
+ }
114
+ if (Qnil == c->clas) {
115
+ c->clas = path2class(c->name);
116
+ }
117
+ if (clas == c->clas) {
118
+ if (encode) {
119
+ return c->active && NULL != c->encode;
120
+ } else {
121
+ return c->active && NULL != c->decode;
122
+ }
123
+ }
127
124
  }
128
125
  return false;
129
126
  }
130
127
 
131
- void
132
- oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class) {
133
- int d2 = depth + 1;
134
- int d3 = d2 + 1;
135
- size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
136
- const char *classname = rb_obj_classname(obj);
137
- size_t len = strlen(classname);
138
- size_t size = d2 * out->indent + 10 + len + out->opts->create_id_len + sep_len;
139
- bool no_comma = true;
128
+ void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class) {
129
+ int d2 = depth + 1;
130
+ int d3 = d2 + 1;
131
+ size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
132
+ const char *classname = rb_obj_classname(obj);
133
+ size_t len = strlen(classname);
134
+ size_t size = d2 * out->indent + 10 + len + out->opts->create_id_len + sep_len;
135
+ bool no_comma = true;
140
136
 
141
137
  assure_size(out, size);
142
138
  *out->cur++ = '{';
143
139
 
144
140
  if (with_class) {
145
- fill_indent(out, d2);
146
- *out->cur++ = '"';
147
- memcpy(out->cur, out->opts->create_id, out->opts->create_id_len);
148
- out->cur += out->opts->create_id_len;
149
- *out->cur++ = '"';
150
- if (0 < out->opts->dump_opts.before_size) {
151
- strcpy(out->cur, out->opts->dump_opts.before_sep);
152
- out->cur += out->opts->dump_opts.before_size;
153
- }
154
- *out->cur++ = ':';
155
- if (0 < out->opts->dump_opts.after_size) {
156
- strcpy(out->cur, out->opts->dump_opts.after_sep);
157
- out->cur += out->opts->dump_opts.after_size;
158
- }
159
- *out->cur++ = '"';
160
- memcpy(out->cur, classname, len);
161
- out->cur += len;
162
- *out->cur++ = '"';
163
- no_comma = false;
141
+ fill_indent(out, d2);
142
+ *out->cur++ = '"';
143
+ memcpy(out->cur, out->opts->create_id, out->opts->create_id_len);
144
+ out->cur += out->opts->create_id_len;
145
+ *out->cur++ = '"';
146
+ if (0 < out->opts->dump_opts.before_size) {
147
+ strcpy(out->cur, out->opts->dump_opts.before_sep);
148
+ out->cur += out->opts->dump_opts.before_size;
149
+ }
150
+ *out->cur++ = ':';
151
+ if (0 < out->opts->dump_opts.after_size) {
152
+ strcpy(out->cur, out->opts->dump_opts.after_sep);
153
+ out->cur += out->opts->dump_opts.after_size;
154
+ }
155
+ *out->cur++ = '"';
156
+ memcpy(out->cur, classname, len);
157
+ out->cur += len;
158
+ *out->cur++ = '"';
159
+ no_comma = false;
164
160
  }
165
161
  size = d3 * out->indent + 2;
166
162
  for (; NULL != attrs->name; attrs++) {
167
- assure_size(out, size + attrs->len + sep_len + 2);
168
- if (no_comma) {
169
- no_comma = false;
170
- } else {
171
- *out->cur++ = ',';
172
- }
173
- fill_indent(out, d2);
174
- *out->cur++ = '"';
175
- memcpy(out->cur, attrs->name, attrs->len);
176
- out->cur += attrs->len;
177
- *out->cur++ = '"';
178
- if (0 < out->opts->dump_opts.before_size) {
179
- strcpy(out->cur, out->opts->dump_opts.before_sep);
180
- out->cur += out->opts->dump_opts.before_size;
181
- }
182
- *out->cur++ = ':';
183
- if (0 < out->opts->dump_opts.after_size) {
184
- strcpy(out->cur, out->opts->dump_opts.after_sep);
185
- out->cur += out->opts->dump_opts.after_size;
186
- }
187
- if (Qundef == attrs->value) {
188
- if (Qundef != attrs->time) {
189
- switch (out->opts->time_format) {
190
- case RubyTime: oj_dump_ruby_time(attrs->time, out); break;
191
- case XmlTime: oj_dump_xml_time(attrs->time, out); break;
192
- case UnixZTime: oj_dump_time(attrs->time, out, true); break;
193
- case UnixTime:
194
- default: oj_dump_time(attrs->time, out, false); break;
195
- }
196
- } else {
197
- char buf[32];
198
- char *b = buf + sizeof(buf) - 1;
199
- int neg = 0;
200
- long num = attrs->num;
201
-
202
- if (0 > num) {
203
- neg = 1;
204
- num = -num;
205
- }
206
- *b-- = '\0';
207
- if (0 < num) {
208
- for (; 0 < num; num /= 10, b--) {
209
- *b = (num % 10) + '0';
210
- }
211
- if (neg) {
212
- *b = '-';
213
- } else {
214
- b++;
215
- }
216
- } else {
217
- *b = '0';
218
- }
219
- assure_size(out, (sizeof(buf) - (b - buf)));
220
- for (; '\0' != *b; b++) {
221
- *out->cur++ = *b;
222
- }
223
- }
224
- } else {
225
- oj_dump_compat_val(attrs->value, d3, out, true);
226
- }
163
+ assure_size(out, size + attrs->len + sep_len + 2);
164
+ if (no_comma) {
165
+ no_comma = false;
166
+ } else {
167
+ *out->cur++ = ',';
168
+ }
169
+ fill_indent(out, d2);
170
+ *out->cur++ = '"';
171
+ memcpy(out->cur, attrs->name, attrs->len);
172
+ out->cur += attrs->len;
173
+ *out->cur++ = '"';
174
+ if (0 < out->opts->dump_opts.before_size) {
175
+ strcpy(out->cur, out->opts->dump_opts.before_sep);
176
+ out->cur += out->opts->dump_opts.before_size;
177
+ }
178
+ *out->cur++ = ':';
179
+ if (0 < out->opts->dump_opts.after_size) {
180
+ strcpy(out->cur, out->opts->dump_opts.after_sep);
181
+ out->cur += out->opts->dump_opts.after_size;
182
+ }
183
+ if (Qundef == attrs->value) {
184
+ if (Qundef != attrs->time) {
185
+ switch (out->opts->time_format) {
186
+ case RubyTime: oj_dump_ruby_time(attrs->time, out); break;
187
+ case XmlTime: oj_dump_xml_time(attrs->time, out); break;
188
+ case UnixZTime: oj_dump_time(attrs->time, out, true); break;
189
+ case UnixTime:
190
+ default: oj_dump_time(attrs->time, out, false); break;
191
+ }
192
+ } else {
193
+ char buf[32];
194
+ char *b = buf + sizeof(buf) - 1;
195
+ int neg = 0;
196
+ long num = attrs->num;
197
+
198
+ if (0 > num) {
199
+ neg = 1;
200
+ num = -num;
201
+ }
202
+ *b-- = '\0';
203
+ if (0 < num) {
204
+ for (; 0 < num; num /= 10, b--) {
205
+ *b = (num % 10) + '0';
206
+ }
207
+ if (neg) {
208
+ *b = '-';
209
+ } else {
210
+ b++;
211
+ }
212
+ } else {
213
+ *b = '0';
214
+ }
215
+ assure_size(out, (sizeof(buf) - (b - buf)));
216
+ for (; '\0' != *b; b++) {
217
+ *out->cur++ = *b;
218
+ }
219
+ }
220
+ } else {
221
+ oj_dump_compat_val(attrs->value, d3, out, true);
222
+ }
227
223
  }
228
224
  assure_size(out, depth * out->indent + 2);
229
225
  fill_indent(out, depth);
230
226
  *out->cur++ = '}';
231
- *out->cur = '\0';
227
+ *out->cur = '\0';
232
228
  }
data/ext/oj/code.h CHANGED
@@ -1,4 +1,5 @@
1
1
  // Copyright (c) 2017 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_CODE_H
4
5
  #define OJ_CODE_H
@@ -7,33 +8,33 @@
7
8
 
8
9
  #include "oj.h"
9
10
 
10
- typedef void (*EncodeFunc)(VALUE obj, int depth, Out out);
11
- typedef VALUE (*DecodeFunc)(VALUE clas, VALUE args);
11
+ typedef void (*EncodeFunc)(VALUE obj, int depth, Out out);
12
+ typedef VALUE (*DecodeFunc)(VALUE clas, VALUE args);
12
13
 
13
14
  typedef struct _code {
14
- const char *name;
15
- VALUE clas;
16
- EncodeFunc encode;
17
- DecodeFunc decode;
18
- bool active; // For compat mode.
19
- } *Code;
15
+ const char *name;
16
+ VALUE clas;
17
+ EncodeFunc encode;
18
+ DecodeFunc decode;
19
+ bool active; // For compat mode.
20
+ } * Code;
20
21
 
21
22
  // Used by encode functions.
22
23
  typedef struct _attr {
23
- const char *name;
24
- int len;
25
- VALUE value;
26
- long num;
27
- VALUE time;
28
- } *Attr;
24
+ const char *name;
25
+ int len;
26
+ VALUE value;
27
+ long num;
28
+ VALUE time;
29
+ } * Attr;
29
30
 
30
- extern bool oj_code_dump(Code codes, VALUE obj, int depth, Out out);
31
- extern VALUE oj_code_load(Code codes, VALUE clas, VALUE args);
32
- extern void oj_code_set_active(Code codes, VALUE clas, bool active);
33
- extern bool oj_code_has(Code codes, VALUE clas, bool encode);
31
+ extern bool oj_code_dump(Code codes, VALUE obj, int depth, Out out);
32
+ extern VALUE oj_code_load(Code codes, VALUE clas, VALUE args);
33
+ extern void oj_code_set_active(Code codes, VALUE clas, bool active);
34
+ extern bool oj_code_has(Code codes, VALUE clas, bool encode);
34
35
 
35
- extern void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class);
36
+ extern void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class);
36
37
 
37
- extern struct _code oj_compat_codes[];
38
+ extern struct _code oj_compat_codes[];
38
39
 
39
40
  #endif /* OJ_CODE_H */