oj 3.1.0 → 3.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 921337fe55a2f80cc6537cb652c5d54a98dd39d3
4
- data.tar.gz: acdf0cc3bdaed92fd5ecb0a7239cfcf88873c59b
3
+ metadata.gz: 35634d84fa9efd54d2c6a5c82572386ab76dcbd9
4
+ data.tar.gz: b53f05e6cbb2159d88b800165e5e843bd0f8f10f
5
5
  SHA512:
6
- metadata.gz: d51c9c25369ca4c5d9b7cd5670e1d4d87ce3458b9e8760d1d893c37f48f4775b145f9fad1b2aba78ba5a157981d7cda854e3c091dd9cf4256f1c9bb096699bd8
7
- data.tar.gz: bdc8810b381c0a6f822432443ba201a2ed37aa30c8fe3f2e2ce00c446cae53a4cfdd90cb796e29cb2bcf60e34c62b607cdd90bf8bbc7b1ca773ba840caed5899
6
+ metadata.gz: 6a7fd255fb03915f2d5bef8d5d9093693a7544345b9dcc3a5208d26db2009b264797a1879e70e1d25e2144813d90e690cbab992092fc65fa0b7aaf22e93fa265
7
+ data.tar.gz: 29166d47b6bdd0ef4fde76c532fd6227f163786ca1f9dd4b5f2a9f507658d078a8ef47ff25ffb59dfe98e5981f70a964e7b62c8703f246fa183f946d00ae115b
@@ -132,40 +132,48 @@ oj_code_has(Code codes, VALUE clas, bool encode) {
132
132
  }
133
133
 
134
134
  void
135
- oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out) {
135
+ oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class) {
136
136
  int d2 = depth + 1;
137
137
  int d3 = d2 + 1;
138
138
  size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
139
139
  const char *classname = rb_obj_classname(obj);
140
140
  size_t len = strlen(classname);
141
141
  size_t size = d2 * out->indent + 10 + len + out->opts->create_id_len + sep_len;
142
-
142
+ bool no_comma = true;
143
+
143
144
  assure_size(out, size);
144
145
  *out->cur++ = '{';
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
146
 
147
+ if (with_class) {
148
+ fill_indent(out, d2);
149
+ *out->cur++ = '"';
150
+ memcpy(out->cur, out->opts->create_id, out->opts->create_id_len);
151
+ out->cur += out->opts->create_id_len;
152
+ *out->cur++ = '"';
153
+ if (0 < out->opts->dump_opts.before_size) {
154
+ strcpy(out->cur, out->opts->dump_opts.before_sep);
155
+ out->cur += out->opts->dump_opts.before_size;
156
+ }
157
+ *out->cur++ = ':';
158
+ if (0 < out->opts->dump_opts.after_size) {
159
+ strcpy(out->cur, out->opts->dump_opts.after_sep);
160
+ out->cur += out->opts->dump_opts.after_size;
161
+ }
162
+ *out->cur++ = '"';
163
+ memcpy(out->cur, classname, len);
164
+ out->cur += len;
165
+ *out->cur++ = '"';
166
+ no_comma = false;
167
+ }
164
168
  size = d3 * out->indent + 2;
165
169
  for (; NULL != attrs->name; attrs++) {
166
170
  assure_size(out, size + attrs->len + sep_len + 2);
167
- *out->cur++ = ',';
168
- fill_indent(out, d3);
171
+ if (no_comma) {
172
+ no_comma = false;
173
+ } else {
174
+ *out->cur++ = ',';
175
+ }
176
+ fill_indent(out, d2);
169
177
  *out->cur++ = '"';
170
178
  memcpy(out->cur, attrs->name, attrs->len);
171
179
  out->cur += attrs->len;
@@ -35,7 +35,7 @@ extern VALUE oj_code_load(Code codes, VALUE clas, VALUE args);
35
35
  extern void oj_code_set_active(Code codes, VALUE clas, bool active);
36
36
  extern bool oj_code_has(Code codes, VALUE clas, bool encode);
37
37
 
38
- extern void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out);
38
+ extern void oj_code_attrs(VALUE obj, Attr attrs, int depth, Out out, bool with_class);
39
39
 
40
40
  extern struct _Code oj_compat_codes[];
41
41
 
@@ -26,7 +26,7 @@ dump_obj_str(VALUE obj, int depth, Out out) {
26
26
  };
27
27
  attrs->value = rb_funcall(obj, oj_to_s_id, 0);
28
28
 
29
- oj_code_attrs(obj, attrs, depth, out);
29
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
30
30
  }
31
31
 
32
32
 
@@ -64,7 +64,7 @@ complex_dump(VALUE obj, int depth, Out out) {
64
64
  attrs[0].value = rb_funcall(obj, real_id, 0);
65
65
  attrs[1].value = rb_funcall(obj, imag_id, 0);
66
66
 
67
- oj_code_attrs(obj, attrs, depth, out);
67
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
68
68
  }
69
69
 
70
70
  static VALUE
@@ -84,7 +84,7 @@ date_dump(VALUE obj, int depth, Out out) {
84
84
  };
85
85
  attrs->value = rb_funcall(obj, rb_intern("iso8601"), 0);
86
86
 
87
- oj_code_attrs(obj, attrs, depth, out);
87
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
88
88
  }
89
89
 
90
90
  static VALUE
@@ -120,7 +120,7 @@ openstruct_dump(VALUE obj, int depth, Out out) {
120
120
  }
121
121
  attrs->value = rb_funcall(obj, table_id, 0);
122
122
 
123
- oj_code_attrs(obj, attrs, depth, out);
123
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
124
124
  }
125
125
 
126
126
  static VALUE
@@ -143,7 +143,7 @@ range_dump(VALUE obj, int depth, Out out) {
143
143
  attrs[1].value = rb_funcall(obj, oj_end_id, 0);
144
144
  attrs[2].value = rb_funcall(obj, oj_exclude_end_id, 0);
145
145
 
146
- oj_code_attrs(obj, attrs, depth, out);
146
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
147
147
  }
148
148
 
149
149
  static VALUE
@@ -174,7 +174,7 @@ rational_dump(VALUE obj, int depth, Out out) {
174
174
  attrs[0].value = rb_funcall(obj, numerator_id, 0);
175
175
  attrs[1].value = rb_funcall(obj, denominator_id, 0);
176
176
 
177
- oj_code_attrs(obj, attrs, depth, out);
177
+ oj_code_attrs(obj, attrs, depth, out, Yes == out->opts->create_ok);
178
178
  }
179
179
 
180
180
  static VALUE
@@ -199,13 +199,23 @@ regexp_load(VALUE clas, VALUE args) {
199
199
 
200
200
  static void
201
201
  time_dump(VALUE obj, int depth, Out out) {
202
- struct _Attr attrs[] = {
203
- { "time", 4, Qundef, 0, Qundef },
204
- { NULL, 0, Qnil },
205
- };
206
- attrs->time = obj;
207
-
208
- oj_code_attrs(obj, attrs, depth, out);
202
+ if (Yes == out->opts->create_ok) {
203
+ struct _Attr attrs[] = {
204
+ { "time", 4, Qundef, 0, Qundef },
205
+ { NULL, 0, Qnil },
206
+ };
207
+ attrs->time = obj;
208
+
209
+ oj_code_attrs(obj, attrs, depth, out, true);
210
+ } else {
211
+ switch (out->opts->time_format) {
212
+ case RubyTime: oj_dump_ruby_time(obj, out); break;
213
+ case XmlTime: oj_dump_xml_time(obj, out); break;
214
+ case UnixZTime: oj_dump_time(obj, out, true); break;
215
+ case UnixTime:
216
+ default: oj_dump_time(obj, out, false); break;
217
+ }
218
+ }
209
219
  }
210
220
 
211
221
  static VALUE
@@ -340,7 +350,7 @@ dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
340
350
 
341
351
  assure_size(out, 2);
342
352
  *out->cur++ = '{';
343
- if (NULL != out->opts->create_id) {
353
+ if (NULL != out->opts->create_id && Yes == out->opts->create_ok) {
344
354
  const char *classname = rb_class2name(clas);
345
355
  int clen = (int)strlen(classname);
346
356
  size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
@@ -557,7 +567,7 @@ dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out) {
557
567
 
558
568
  assure_size(out, 2);
559
569
  *out->cur++ = '{';
560
- if (Qundef != clas) {
570
+ if (Qundef != clas && NULL != out->opts->create_id && Yes == out->opts->create_ok) {
561
571
  size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
562
572
  const char *classname = rb_obj_classname(obj);
563
573
  size_t len = strlen(classname);
@@ -224,7 +224,7 @@ bigdecimal_alt(VALUE obj, int depth, Out out) {
224
224
  }
225
225
  attrs[0].value = rb_funcall(obj, _dump_id, 0);
226
226
 
227
- oj_code_attrs(obj, attrs, depth, out);
227
+ oj_code_attrs(obj, attrs, depth, out, true);
228
228
  }
229
229
 
230
230
  static ID real_id = 0;
@@ -245,7 +245,7 @@ complex_alt(VALUE obj, int depth, Out out) {
245
245
  attrs[0].value = rb_funcall(obj, real_id, 0);
246
246
  attrs[1].value = rb_funcall(obj, imag_id, 0);
247
247
 
248
- oj_code_attrs(obj, attrs, depth, out);
248
+ oj_code_attrs(obj, attrs, depth, out, true);
249
249
  }
250
250
 
251
251
  static ID year_id = 0;
@@ -273,7 +273,7 @@ date_alt(VALUE obj, int depth, Out out) {
273
273
  attrs[2].value = rb_funcall(obj, day_id, 0);
274
274
  attrs[3].value = rb_funcall(obj, start_id, 0);
275
275
 
276
- oj_code_attrs(obj, attrs, depth, out);
276
+ oj_code_attrs(obj, attrs, depth, out, true);
277
277
  }
278
278
 
279
279
  static ID hour_id = 0;
@@ -313,7 +313,7 @@ datetime_alt(VALUE obj, int depth, Out out) {
313
313
  attrs[6].value = rb_funcall(rb_funcall(obj, offset_id, 0), oj_to_s_id, 0);
314
314
  attrs[7].value = rb_funcall(obj, start_id, 0);
315
315
 
316
- oj_code_attrs(obj, attrs, depth, out);
316
+ oj_code_attrs(obj, attrs, depth, out, true);
317
317
  }
318
318
 
319
319
  static ID message_id = 0;
@@ -381,7 +381,7 @@ openstruct_alt(VALUE obj, int depth, Out out) {
381
381
  }
382
382
  attrs[0].value = rb_funcall(obj, table_id, 0);
383
383
 
384
- oj_code_attrs(obj, attrs, depth, out);
384
+ oj_code_attrs(obj, attrs, depth, out, true);
385
385
  }
386
386
 
387
387
  static void
@@ -434,7 +434,7 @@ rational_alt(VALUE obj, int depth, Out out) {
434
434
  attrs[0].value = rb_funcall(obj, numerator_id, 0);
435
435
  attrs[1].value = rb_funcall(obj, denominator_id, 0);
436
436
 
437
- oj_code_attrs(obj, attrs, depth, out);
437
+ oj_code_attrs(obj, attrs, depth, out, true);
438
438
  }
439
439
 
440
440
  static ID options_id = 0;
@@ -454,7 +454,7 @@ regexp_alt(VALUE obj, int depth, Out out) {
454
454
  attrs[0].value = rb_funcall(obj, options_id, 0);
455
455
  attrs[1].value = rb_funcall(obj, source_id, 0);
456
456
 
457
- oj_code_attrs(obj, attrs, depth, out);
457
+ oj_code_attrs(obj, attrs, depth, out, true);
458
458
  }
459
459
 
460
460
  static void
@@ -469,7 +469,7 @@ time_alt(VALUE obj, int depth, Out out) {
469
469
  attrs[0].num = ts.tv_sec;
470
470
  attrs[1].num = ts.tv_nsec;
471
471
 
472
- oj_code_attrs(obj, attrs, depth, out);
472
+ oj_code_attrs(obj, attrs, depth, out, true);
473
473
  }
474
474
 
475
475
  struct _Code oj_compat_codes[] = {
@@ -753,6 +753,8 @@ mimic_state(VALUE self) {
753
753
  void
754
754
  oj_mimic_json_methods(VALUE json) {
755
755
  VALUE json_error;
756
+ VALUE generator;
757
+ VALUE ext;
756
758
 
757
759
  rb_define_module_function(json, "create_id=", mimic_set_create_id, 1);
758
760
  rb_define_module_function(json, "create_id", mimic_create_id, 0);
@@ -796,6 +798,22 @@ oj_mimic_json_methods(VALUE json) {
796
798
  } else {
797
799
  rb_define_class_under(json, "NestingError", json_error);
798
800
  }
801
+
802
+ if (rb_const_defined_at(json, rb_intern("Ext"))) {
803
+ ext = rb_const_get_at(json, rb_intern("Ext"));
804
+ } else {
805
+ ext = rb_define_module_under(json, "Ext");
806
+ }
807
+ if (rb_const_defined_at(ext, rb_intern("Generator"))) {
808
+ generator = rb_const_get_at(ext, rb_intern("Generator"));
809
+ } else {
810
+ generator = rb_define_module_under(ext, "Generator");
811
+ }
812
+ if (!rb_const_defined_at(generator, rb_intern("State"))) {
813
+ rb_require("oj/state");
814
+ }
815
+ // Pull in the JSON::State mimic file.
816
+ state_class = rb_const_get_at(generator, rb_intern("State"));
799
817
  }
800
818
 
801
819
  /* Document-module: JSON
@@ -804,11 +822,9 @@ oj_mimic_json_methods(VALUE json) {
804
822
  */
805
823
  VALUE
806
824
  oj_define_mimic_json(int argc, VALUE *argv, VALUE self) {
807
- VALUE ext;
808
825
  VALUE dummy;
809
826
  VALUE verbose;
810
827
  VALUE json;
811
- VALUE generator;
812
828
 
813
829
  // Either set the paths to indicate JSON has been loaded or replaces the
814
830
  // methods if it has been loaded.
@@ -832,23 +848,7 @@ oj_define_mimic_json(int argc, VALUE *argv, VALUE self) {
832
848
  rb_funcall2(Oj, rb_intern("mimic_loaded"), 0, 0);
833
849
  }
834
850
  }
835
- if (rb_const_defined_at(json, rb_intern("Ext"))) {
836
- ext = rb_const_get_at(json, rb_intern("Ext"));
837
- } else {
838
- ext = rb_define_module_under(json, "Ext");
839
- }
840
- if (rb_const_defined_at(ext, rb_intern("Generator"))) {
841
- generator = rb_const_get_at(ext, rb_intern("Generator"));
842
- } else {
843
- generator = rb_define_module_under(ext, "Generator");
844
- }
845
- if (!rb_const_defined_at(generator, rb_intern("State"))) {
846
- rb_require("oj/state");
847
- }
848
-
849
- // convince Ruby that the json gem has already been loaded
850
- // Pull in the JSON::State mimic file.
851
- state_class = rb_const_get_at(generator, rb_intern("State"));
851
+
852
852
  // TBD create all modules in mimic_loaded
853
853
 
854
854
  oj_mimic_json_methods(json);
@@ -898,7 +898,7 @@ oj_mimic_rails_init() {
898
898
  rb_define_module_function(rails, "optimize", rails_optimize, -1);
899
899
  rb_define_module_function(rails, "deoptimize", rails_deoptimize, -1);
900
900
  rb_define_module_function(rails, "optimized?", rails_optimized, 1);
901
- rb_define_module_function(rails, "mimic_JSON?", rails_mimic_json, 0);
901
+ rb_define_module_function(rails, "mimic_JSON", rails_mimic_json, 0);
902
902
 
903
903
  rb_define_module_function(rails, "set_encoder", rails_set_encoder, 0);
904
904
  rb_define_module_function(rails, "set_decoder", rails_set_decoder, 0);
@@ -66,6 +66,10 @@ oj_str_writer_init(StrWriter sw) {
66
66
  sw->out.opts = &sw->opts;
67
67
  sw->out.indent = sw->opts.indent;
68
68
  sw->out.depth = 0;
69
+ sw->out.argc = 0;
70
+ sw->out.argv = NULL;
71
+ sw->out.caller = 0;
72
+ sw->out.ropts = NULL;
69
73
  }
70
74
 
71
75
  void
@@ -140,24 +144,34 @@ oj_str_writer_push_array(StrWriter sw, const char *key) {
140
144
 
141
145
  void
142
146
  oj_str_writer_push_value(StrWriter sw, VALUE val, const char *key) {
147
+ Out out = &sw->out;
148
+
143
149
  if (sw->keyWritten) {
144
150
  sw->keyWritten = 0;
145
151
  } else {
146
152
  long size;
147
153
 
148
154
  key_check(sw, key);
149
- size = sw->depth * sw->out.indent + 3;
150
- assure_size(&sw->out, size);
155
+ size = sw->depth * out->indent + 3;
156
+ assure_size(out, size);
151
157
  maybe_comma(sw);
152
158
  if (0 < sw->depth) {
153
159
  fill_indent(&sw->out, sw->depth);
154
160
  }
155
161
  if (0 != key) {
156
- oj_dump_cstr(key, strlen(key), 0, 0, &sw->out);
157
- *sw->out.cur++ = ':';
162
+ oj_dump_cstr(key, strlen(key), 0, 0, out);
163
+ *out->cur++ = ':';
158
164
  }
159
165
  }
160
- oj_dump_custom_val(val, sw->depth, &sw->out, true);
166
+ switch (out->opts->mode) {
167
+ case StrictMode: oj_dump_strict_val(val, sw->depth, out); break;
168
+ case NullMode: oj_dump_null_val(val, sw->depth, out); break;
169
+ case ObjectMode: oj_dump_obj_val(val, sw->depth, out); break;
170
+ case CompatMode: oj_dump_compat_val(val, sw->depth, out, Yes == out->opts->to_json); break;
171
+ case RailsMode: oj_dump_rails_val(val, sw->depth, out); break;
172
+ case CustomMode: oj_dump_custom_val(val, sw->depth, out, true); break;
173
+ default: oj_dump_custom_val(val, sw->depth, out, true); break;
174
+ }
161
175
  }
162
176
 
163
177
  void
@@ -236,7 +250,11 @@ str_writer_free(void *ptr) {
236
250
  /* Document-method: new
237
251
  * call-seq: new(io, options)
238
252
  *
239
- * Creates a new StringWriter.
253
+ * Creates a new StringWriter. Options are supported according the the
254
+ * specified mode or the mode in the default options. Note that if mimic_JSON
255
+ * or Oj.optimize_rails has not been called then the behavior of the modes may
256
+ * not be the same as if they were.
257
+ *
240
258
  * - *io* [_IO_] stream to write to
241
259
  * - *options* [_Hash_] formating options
242
260
  */
@@ -248,6 +266,8 @@ str_writer_new(int argc, VALUE *argv, VALUE self) {
248
266
  if (1 == argc) {
249
267
  oj_parse_options(argv[0], &sw->opts);
250
268
  }
269
+ sw->out.argc = argc - 1;
270
+ sw->out.argv = argv + 1;
251
271
  sw->out.indent = sw->opts.indent;
252
272
 
253
273
  return Data_Wrap_Struct(oj_string_writer_class, 0, str_writer_free, sw);
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '3.1.0'
4
+ VERSION = '3.1.2'
5
5
  end
@@ -4,9 +4,12 @@
4
4
  %w(lib ext test).each do |dir|
5
5
  $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
6
6
  end
7
+
7
8
  require 'rails'
9
+ require 'active_support/json'
8
10
  require 'oj'
9
11
 
10
- Oj.mimic_JSON
12
+ Oj.optimize_rails
13
+
14
+ JSON.pretty_generate({'a'=>1,'b'=>[true,false]})
11
15
 
12
- nil.to_json
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-04 00:00:00.000000000 Z
11
+ date: 2017-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler