oj 3.0.4 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77898b80dc0daaabff6e620f0d9ef0b6eb26eac1
4
- data.tar.gz: 112de18472976923d86d57a62fa30f611e0c4bbc
3
+ metadata.gz: d4fd78480b3ee92e4d3934bd33045024639bc758
4
+ data.tar.gz: 920bd7a9dc6270ed8b8826c1db7311681ac7c074
5
5
  SHA512:
6
- metadata.gz: f59fca5e3442fcf3d46e5f931ec36657e54f359924e60707ed4962c11f9b7f402e41e2166befb257a7bf6686a4552a6be8553b84552908ca503a30bdd4f94ec1
7
- data.tar.gz: 9443804d9d931de71e5b9d3f2ba29d05141fb0dd86bd9700d285908d75e5566f50476b5887200c49dc3524637a076c3efb9f489adb4926bf4dd0403753eb5032
6
+ metadata.gz: c0017d120037458f4d502e183006492db64005e57b57906e8654bb0391c9ce2980c2f9f7792b80eadcb8df7d360070e60a1324c1124713be39fd13cdba61cbea
7
+ data.tar.gz: 391c4c4ff9abffc12050e4c0128bf8daa98e8bb811f53ed71dfd80ccab9a3a71dd2a1e549441f050f25d32f416026b660f63592b66d2c386d5af17e22d43e642
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [![{}j](http://www.ohler.com/dev/images/oj_hel_fire_64.png)](http://www.ohler.com/oj) gem
1
+ # [![{}j](http://www.ohler.com/dev/images/oj_comet_64.png)](http://www.ohler.com/oj) gem
2
2
 
3
3
  [![Build Status](https://img.shields.io/travis/ohler55/oj/master.svg)](http://travis-ci.org/ohler55/oj?branch=master) ![Gem](https://img.shields.io/gem/v/oj.svg) ![Gem](https://img.shields.io/gem/dt/oj.svg)
4
4
 
@@ -754,10 +754,12 @@ oj_define_mimic_json(int argc, VALUE *argv, VALUE self) {
754
754
  } else {
755
755
  generator = rb_define_module_under(ext, "Generator");
756
756
  }
757
-
757
+ if (!rb_const_defined_at(generator, rb_intern("State"))) {
758
+ rb_require("oj/state");
759
+ }
760
+
758
761
  // convince Ruby that the json gem has already been loaded
759
762
  // Pull in the JSON::State mimic file.
760
- rb_require("oj/state");
761
763
  state_class = rb_const_get_at(generator, rb_intern("State"));
762
764
  // TBD create all modules in mimic_loaded
763
765
 
@@ -211,22 +211,10 @@ dump_bigdecimal(VALUE obj, int depth, Out out, bool as_ok) {
211
211
  }
212
212
 
213
213
  static void
214
- dump_time(VALUE obj, int depth, Out out, bool as_ok) {
214
+ dump_sec_nano(VALUE obj, time_t sec, long nsec, Out out) {
215
215
  char buf[64];
216
216
  struct tm *tm;
217
217
  long one = 1000000000;
218
- #if HAS_RB_TIME_TIMESPEC
219
- struct timespec ts = rb_time_timespec(obj);
220
- time_t sec = ts.tv_sec;
221
- long nsec = ts.tv_nsec;
222
- #else
223
- time_t sec = NUM2LONG(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
224
- #if HAS_NANO_TIME
225
- long long nsec = rb_num2ll(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
226
- #else
227
- long long nsec = rb_num2ll(rb_funcall2(obj, oj_tv_usec_id, 0, 0)) * 1000;
228
- #endif
229
- #endif
230
218
  long tzsecs = NUM2LONG(rb_funcall2(obj, oj_utc_offset_id, 0, 0));
231
219
  int tzhour, tzmin;
232
220
  char tzsign = '+';
@@ -251,7 +239,6 @@ dump_time(VALUE obj, int depth, Out out, bool as_ok) {
251
239
  // 2012-01-05T23:58:07.123456000+09:00 or 2012/01/05 23:58:07 +0900
252
240
  sec += tzsecs;
253
241
  tm = gmtime(&sec);
254
- #if 1
255
242
  if (0 > tzsecs) {
256
243
  tzsign = '-';
257
244
  tzhour = (int)(tzsecs / -3600);
@@ -260,16 +247,6 @@ dump_time(VALUE obj, int depth, Out out, bool as_ok) {
260
247
  tzhour = (int)(tzsecs / 3600);
261
248
  tzmin = (int)(tzsecs / 60) - (tzhour * 60);
262
249
  }
263
- #else
264
- if (0 > tm->tm_gmtoff) {
265
- tzsign = '-';
266
- tzhour = (int)(tm->tm_gmtoff / -3600);
267
- tzmin = (int)(tm->tm_gmtoff / -60) - (tzhour * 60);
268
- } else {
269
- tzhour = (int)(tm->tm_gmtoff / 3600);
270
- tzmin = (int)(tm->tm_gmtoff / 60) - (tzhour * 60);
271
- }
272
- #endif
273
250
  if (!xml_time) {
274
251
  len = sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d %c%02d%02d",
275
252
  tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
@@ -312,6 +289,34 @@ dump_time(VALUE obj, int depth, Out out, bool as_ok) {
312
289
  oj_dump_cstr(buf, len, 0, 0, out);
313
290
  }
314
291
 
292
+ static void
293
+ dump_time(VALUE obj, int depth, Out out, bool as_ok) {
294
+ #if HAS_RB_TIME_TIMESPEC
295
+ struct timespec ts = rb_time_timespec(obj);
296
+ time_t sec = ts.tv_sec;
297
+ long nsec = ts.tv_nsec;
298
+ #else
299
+ time_t sec = NUM2LONG(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
300
+ #if HAS_NANO_TIME
301
+ long long nsec = rb_num2ll(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
302
+ #else
303
+ long long nsec = rb_num2ll(rb_funcall2(obj, oj_tv_usec_id, 0, 0)) * 1000;
304
+ #endif
305
+ #endif
306
+ dump_sec_nano(obj, sec, nsec, out);
307
+ }
308
+
309
+ static void
310
+ dump_timewithzone(VALUE obj, int depth, Out out, bool as_ok) {
311
+ time_t sec = NUM2LONG(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
312
+ #if HAS_NANO_TIME
313
+ long long nsec = rb_num2ll(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
314
+ #else
315
+ long long nsec = rb_num2ll(rb_funcall2(obj, oj_tv_usec_id, 0, 0)) * 1000;
316
+ #endif
317
+ dump_sec_nano(obj, sec, nsec, out);
318
+ }
319
+
315
320
  static void
316
321
  dump_to_s(VALUE obj, int depth, Out out, bool as_ok) {
317
322
  volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
@@ -329,16 +334,27 @@ static struct _NamedFunc dump_map[] = {
329
334
  { "Range", dump_to_s },
330
335
  { "Regexp", dump_to_s },
331
336
  { "Time", dump_time },
337
+ { "ActiveSupport::TimeWithZone", dump_timewithzone },
332
338
  { NULL, NULL },
333
339
  };
334
340
 
341
+ static VALUE activerecord_base = Qundef;
342
+
343
+ static void
344
+ dump_activerecord(VALUE obj, int depth, Out out, bool as_ok) {
345
+ volatile VALUE attrs = rb_funcall(obj, rb_intern("attributes"), 0);
346
+
347
+ out->argc = 0;
348
+ dump_rails_val(attrs, depth, out, true);
349
+ }
350
+
335
351
  static ROpt
336
352
  create_opt(ROptTable rot, VALUE clas) {
337
353
  ROpt ro;
338
354
  NamedFunc nf;
339
355
  const char *classname = rb_class2name(clas);
340
356
  int olen = rot->len;
341
-
357
+
342
358
  rot->len++;
343
359
  if (NULL == rot->table) {
344
360
  rot->alen = 256;
@@ -373,7 +389,17 @@ create_opt(ROptTable rot, VALUE clas) {
373
389
  }
374
390
  }
375
391
  if (ro->dump == dump_obj_attrs) {
376
- if (Qtrue == rb_class_inherited_p(clas, rb_cStruct)) { // check before enumerable
392
+ if (Qundef == activerecord_base) {
393
+ // If not defined let an exception be raised.
394
+ VALUE ar = rb_const_get_at(rb_cObject, rb_intern("ActiveRecord"));
395
+
396
+ if (Qundef != ar) {
397
+ activerecord_base = rb_const_get_at(ar, rb_intern("Base"));
398
+ }
399
+ }
400
+ if (Qundef != activerecord_base && Qtrue == rb_class_inherited_p(clas, activerecord_base)) {
401
+ ro->dump = dump_activerecord;
402
+ } else if (Qtrue == rb_class_inherited_p(clas, rb_cStruct)) { // check before enumerable
377
403
  ro->dump = dump_struct;
378
404
  } else if (Qtrue == rb_class_inherited_p(clas, rb_mEnumerable)) {
379
405
  ro->dump = dump_enumerable;
@@ -898,7 +924,8 @@ dump_array(VALUE a, int depth, Out out, bool as_ok) {
898
924
  return;
899
925
  }
900
926
  }
901
- if (as_ok && !oj_rails_array_opt && rb_respond_to(a, oj_as_json_id)) {
927
+ //if (!oj_rails_array_opt && as_ok && 0 < out->argc && rb_respond_to(a, oj_as_json_id)) {
928
+ if (as_ok && 0 < out->argc && rb_respond_to(a, oj_as_json_id)) {
902
929
  dump_as_json(a, depth, out, false);
903
930
  return;
904
931
  }
@@ -1029,11 +1056,8 @@ dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
1029
1056
  return;
1030
1057
  }
1031
1058
  }
1032
- // Nothing good can come from calling as_json on a hash which is supposed
1033
- // to be a primitive so if the type is a hash and the class is also a hash
1034
- // then do not call as_json.
1035
- //if (!oj_rails_hash_opt && as_ok && rb_cHash != rb_obj_class(obj) && rb_respond_to(obj, oj_as_json_id)) {
1036
- if (!oj_rails_hash_opt && as_ok && rb_respond_to(obj, oj_as_json_id)) {
1059
+ //if (!oj_rails_hash_opt && 0 < out->argc && as_ok && rb_respond_to(obj, oj_as_json_id)) {
1060
+ if (0 < out->argc && as_ok && rb_respond_to(obj, oj_as_json_id)) {
1037
1061
  dump_as_json(obj, depth, out, false);
1038
1062
  return;
1039
1063
  }
@@ -1,31 +1,6 @@
1
1
  /* resolve.c
2
2
  * Copyright (c) 2012, Peter Ohler
3
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
4
  */
30
5
 
31
6
  #include <stdlib.h>
@@ -1,31 +1,6 @@
1
1
  /* resolve.h
2
2
  * Copyright (c) 2011, Peter Ohler
3
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
4
  */
30
5
 
31
6
  #ifndef __OJ_RESOLVE_H__
@@ -92,13 +92,13 @@ module JSON
92
92
  end # Parser
93
93
  end # Ext
94
94
 
95
- State = ::JSON::Ext::Generator::State
95
+ State = ::JSON::Ext::Generator::State unless defined?(::JSON::State)
96
96
 
97
97
  begin
98
98
  Object.send(:remove_const, :Parser)
99
99
  rescue
100
100
  end
101
- Parser = ::JSON::Ext::Parser
101
+ Parser = ::JSON::Ext::Parser unless defined?(::JSON::Parser)
102
102
  self.parser = ::JSON::Ext::Parser
103
103
  self.generator = ::JSON::Ext::Generator
104
104
 
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '3.0.4'
4
+ VERSION = '3.0.5'
5
5
  end
@@ -10,7 +10,7 @@ require 'oj'
10
10
 
11
11
  Oj::Rails.set_encoder()
12
12
  Oj::Rails.set_decoder()
13
- Oj::Rails.optimize(Array, BigDecimal, Hash, Range, Regexp, Time)
13
+ Oj::Rails.optimize(Array, BigDecimal, Hash, Range, Regexp, Time, ActiveSupport::TimeWithZone)
14
14
  ```
15
15
 
16
16
  Some of the Oj options are supported as arguments to the encoder if called
@@ -58,6 +58,9 @@ The classes that can be put in optimized mode are:
58
58
  * Range
59
59
  * Regexp
60
60
  * Time
61
+ * ActiveSupport::TimeWithZone
62
+ * any class inheriting from ActiveRecord::Base
63
+ * any other class where all attributes should be dumped
61
64
 
62
65
  The ActiveSupport decoder is the JSON.parse() method. Calling the
63
66
  Oj::Rails.set_decoder() method replaces that method with the Oj equivelant.
@@ -11,7 +11,7 @@ require 'oj'
11
11
  # Sets the ActiveSupport emcoder to be Oj and also wraps the setting of
12
12
  # globals.
13
13
  Oj::Rails.set_encoder()
14
- Oj::Rails.optimize(BigDecimal, Time, Range, Regexp)
14
+ Oj::Rails.optimize(Hash, Array, BigDecimal, Time, Range, Regexp, ActiveSupport::TimeWithZone)
15
15
 
16
16
  class TestJSONEncoding < ActiveSupport::TestCase
17
17
  include TimeZoneTestHelpers
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.0.4
4
+ version: 3.0.5
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-05-01 00:00:00.000000000 Z
11
+ date: 2017-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler