oj 2.11.4 → 2.11.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of oj might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b53b303c6deecb0860b8b962cb5e8bdc37c0c80
4
- data.tar.gz: ece33a05b8cd4f63b323cbf34dfb3a3bad67dd24
3
+ metadata.gz: 8d1ef3e0a7644077283534c35c890016691f9592
4
+ data.tar.gz: 671cfd43d8f6b32f3d84a389326e090ca42fa2f2
5
5
  SHA512:
6
- metadata.gz: 95e7432d06edccdc616488728fb49bfd65393cf3451fe40b869de89397e0820ab64f8f5d1b252ac435d86798efdbd057fece5877655a6f97e66890d1f676ce2e
7
- data.tar.gz: 6edd99edb2d3160a221c168ce30d5779cbec202908c91135c763f57df2024e6a758b6f2ff6f40bfe0ae7c68937accab77af260b951266a9b2021e00096fbf8a8
6
+ metadata.gz: e443fc1c93a8399fa30e5bf48e10547bbf5baf7a5cd749a9e37b8fd234d4e8bb15dbc1942012f5c5cf7eff60064d5a37447e6a69f7beaea7bbdcb4dfea54bd0c
7
+ data.tar.gz: 323cb80c48f393bca6ffe4b11dae19af16ce88b2b6f2f5052264de1e3487fafd04ab61ce4daadb76eb1be5737a1fb13c47adec5572451f24ede11be8777be89a
data/README.md CHANGED
@@ -26,11 +26,18 @@ Follow [@peterohler on Twitter](http://twitter.com/#!/peterohler) for announceme
26
26
 
27
27
  [![Build Status](https://secure.travis-ci.org/ohler55/oj.png?branch=master)](http://travis-ci.org/ohler55/oj)
28
28
 
29
- ## Current Release 2.11.4
29
+ ## Release 2.11.5
30
+
31
+ - Fixed issue with rails as_json not being called for Structs.
32
+
33
+ - Added support for anonymous Structs with object mode encoding. Note that this
34
+ will result in a new anonymous Struct for each instance.
35
+
36
+ ## Release 2.11.4
30
37
 
31
38
  - DateTime second encoding is now always a Rational to preserve accuracy.
32
39
 
33
- - Fixed buf in the Oj.load() callback feature that caused an endless loop when
40
+ - Fixed bug in the Oj.load() callback feature that caused an endless loop when
34
41
  a StringIO was used with a JSON that was a number.
35
42
 
36
43
  [Older release notes](http://www.ohler.com/dev/oj_misc/release_notes.html).
@@ -71,9 +78,9 @@ Oj.default_options = {:mode => :compat }
71
78
  ## Compatibility
72
79
 
73
80
  ### Ruby
74
- Oj is compatible with Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.1 and RBX. Support for
75
- JRuby has been removed as JRuby no longer supports C extensions and there are
76
- bugs in the older versions that are not being fixed.
81
+ Oj is compatible with Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1, 2.2 and RBX.
82
+ Support for JRuby has been removed as JRuby no longer supports C extensions and
83
+ there are bugs in the older versions that are not being fixed.
77
84
 
78
85
  ### Rails
79
86
  Although up until 4.1 Rails uses [multi_json](https://github.com/intridea/multi_json), an [issue in Rails](https://github.com/rails/rails/issues/9212) causes ActiveSupport to fail to make use Oj for JSON handling.
@@ -109,7 +116,7 @@ they are not more than `String`s. Always check inputs from untrusted sources.
109
116
 
110
117
  ```ruby
111
118
  require 'oj'
112
-
119
+
113
120
  h = { 'one' => 1, 'array' => [ true, false ] }
114
121
  json = Oj.dump(h)
115
122
 
@@ -1212,7 +1212,6 @@ dump_data_comp(VALUE obj, int depth, Out out) {
1212
1212
  } else {
1213
1213
  dump_val(aj, depth, out);
1214
1214
  }
1215
-
1216
1215
  } else if (Yes == out->opts->to_json && rb_respond_to(obj, oj_to_json_id)) {
1217
1216
  volatile VALUE rs;
1218
1217
  const char *s;
@@ -1628,6 +1627,17 @@ dump_struct_comp(VALUE obj, int depth, Out out) {
1628
1627
  rb_raise(rb_eTypeError, "%s.to_hash() did not return a Hash.\n", rb_class2name(rb_obj_class(obj)));
1629
1628
  }
1630
1629
  dump_hash(h, Qundef, depth, out->opts->mode, out);
1630
+ } else if (rb_respond_to(obj, oj_as_json_id)) {
1631
+ volatile VALUE aj = rb_funcall(obj, oj_as_json_id, 0);
1632
+
1633
+ // Catch the obvious brain damaged recursive dumping.
1634
+ if (aj == obj) {
1635
+ volatile VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
1636
+
1637
+ dump_cstr(rb_string_value_ptr((VALUE*)&rstr), RSTRING_LEN(rstr), 0, 0, out);
1638
+ } else {
1639
+ dump_val(aj, depth, out);
1640
+ }
1631
1641
  } else if (Yes == out->opts->to_json && rb_respond_to(obj, oj_to_json_id)) {
1632
1642
  volatile VALUE rs = rb_funcall(obj, oj_to_json_id, 0);
1633
1643
  const char *s;
@@ -1668,11 +1678,35 @@ dump_struct_obj(VALUE obj, int depth, Out out) {
1668
1678
  *out->cur++ = '"';
1669
1679
  *out->cur++ = ':';
1670
1680
  *out->cur++ = '[';
1671
- fill_indent(out, d3);
1672
- *out->cur++ = '"';
1673
- memcpy(out->cur, class_name, len);
1674
- out->cur += len;
1675
- *out->cur++ = '"';
1681
+ if ('#' == *class_name) {
1682
+ VALUE ma = rb_struct_s_members(clas);
1683
+ const char *name;
1684
+ int cnt = (int)RARRAY_LEN(ma);
1685
+
1686
+ *out->cur++ = '[';
1687
+ for (i = 0; i < cnt; i++) {
1688
+ name = rb_id2name(SYM2ID(rb_ary_entry(ma, i)));
1689
+ len = strlen(name);
1690
+ size = len + 3;
1691
+ if (out->end - out->cur <= (long)size) {
1692
+ grow(out, size);
1693
+ }
1694
+ if (0 < i) {
1695
+ *out->cur++ = ',';
1696
+ }
1697
+ *out->cur++ = '"';
1698
+ memcpy(out->cur, name, len);
1699
+ out->cur += len;
1700
+ *out->cur++ = '"';
1701
+ }
1702
+ *out->cur++ = ']';
1703
+ } else {
1704
+ fill_indent(out, d3);
1705
+ *out->cur++ = '"';
1706
+ memcpy(out->cur, class_name, len);
1707
+ out->cur += len;
1708
+ *out->cur++ = '"';
1709
+ }
1676
1710
  *out->cur++ = ',';
1677
1711
  size = d3 * out->indent + 2;
1678
1712
  #ifdef RSTRUCT_LEN
@@ -192,15 +192,28 @@ hat_value(ParseInfo pi, Val parent, const char *key, size_t klen, volatile VALUE
192
192
 
193
193
  if (2 == klen && 'u' == key[1]) {
194
194
  VALUE sc;
195
+ VALUE e1;
195
196
 
196
197
  if (0 == len) {
197
198
  oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "Invalid struct data");
198
199
  return 1;
199
200
  }
200
-
201
- // If struct is not defined then we let this fail and raise an exception.
202
- sc = oj_name2struct(pi, *RARRAY_PTR(value));
203
-
201
+ e1 = *RARRAY_PTR(value);
202
+ // check for anonymous Struct
203
+ if (T_ARRAY == rb_type(e1)) {
204
+ VALUE args[1024];
205
+ VALUE rstr;
206
+ int i, cnt = (int)RARRAY_LEN(e1);
207
+
208
+ for (i = 0; i < cnt; i++) {
209
+ rstr = rb_ary_entry(e1, i);
210
+ args[i] = rb_funcall(rstr, oj_to_sym_id, 0);
211
+ }
212
+ sc = rb_funcall2(rb_cStruct, oj_new_id, cnt, args);
213
+ } else {
214
+ // If struct is not defined then we let this fail and raise an exception.
215
+ sc = oj_name2struct(pi, *RARRAY_PTR(value));
216
+ }
204
217
  // Create a properly initialized struct instance without calling the initialize method.
205
218
  parent->val = rb_obj_alloc(sc);
206
219
  // If the JSON array has more entries than the struct class allows, we record an error.
@@ -1231,6 +1231,14 @@ str_writer_to_s(VALUE self) {
1231
1231
 
1232
1232
  // StreamWriter
1233
1233
 
1234
+ /* Document-class: Oj::StreamWriter
1235
+ *
1236
+ * Supports building a JSON document one element at a time. Build the IO stream
1237
+ * document by pushing values into the document. Pushing an array or an object
1238
+ * will create that element in the JSON document and subsequent pushes will add
1239
+ * the elements to that array or object until a pop() is called.
1240
+ */
1241
+
1234
1242
  static void
1235
1243
  stream_writer_free(void *ptr) {
1236
1244
  StreamWriter sw;
@@ -1271,14 +1279,10 @@ stream_writer_reset_buf(StreamWriter sw) {
1271
1279
  *sw->sw.out.cur = '\0';
1272
1280
  }
1273
1281
 
1274
- /* call-seq: new(options)
1275
- *
1276
- * Creates a new StreamWriter.
1277
- * @param [Hash] options formating options
1278
- */
1279
- /* call-seq: new(options)
1282
+ /* call-seq: new(io, options)
1280
1283
  *
1281
1284
  * Creates a new StreamWriter.
1285
+ * @param [IO] io stream to write to
1282
1286
  * @param [Hash] options formating options
1283
1287
  */
1284
1288
  static VALUE
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '2.11.4'
4
+ VERSION = '2.11.5'
5
5
  end
@@ -79,6 +79,20 @@ class SharedMimicTest < Minitest::Test
79
79
  end
80
80
  # TBD options
81
81
 
82
+ def test_dump_struct
83
+ # anonymous Struct
84
+ s = Struct.new(:a, :b, :c)
85
+ o = s.new(1, 'two', [true, false])
86
+ json = JSON.dump(o)
87
+ # Rails add the as_json method and changes the behavior.
88
+ if o.respond_to?(:as_json)
89
+ assert_equal(%|{"a":1,"b":"two","c":[true,false]}|, json)
90
+ else
91
+ j = '"' + o.to_s.gsub('"', '\\"') + '"'
92
+ assert_equal(j, json)
93
+ end
94
+ end
95
+
82
96
  # load
83
97
  def test_load_string
84
98
  json = %{{"a":1,"b":[true,false]}}
@@ -7,8 +7,9 @@ require 'helper'
7
7
 
8
8
  begin
9
9
  require 'rails/all'
10
- rescue Exception
11
- Process.exit!(true)
10
+ rescue Exception => e
11
+ puts "*** #{e.class}: #{e.message}"
12
+ Process.exit!(false)
12
13
  end
13
14
 
14
15
  Oj.mimic_JSON
@@ -9,6 +9,7 @@ Oj.mimic_JSON
9
9
  begin
10
10
  require 'rails/all'
11
11
  rescue Exception
12
+ puts "*** require of rails failed"
12
13
  Process.exit!(true)
13
14
  end
14
15
 
@@ -185,15 +185,11 @@ class ObjectJuice < Minitest::Test
185
185
  def test_encode
186
186
  opts = Oj.default_options
187
187
  Oj.default_options = { :ascii_only => false }
188
- unless 'jruby' == $ruby
189
- dump_and_load("ぴーたー", false)
190
- end
188
+ dump_and_load("ぴーたー", false)
191
189
  Oj.default_options = { :ascii_only => true }
192
190
  json = Oj.dump("ぴーたー")
193
191
  assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
194
- unless 'jruby' == $ruby
195
- dump_and_load("ぴーたー", false)
196
- end
192
+ dump_and_load("ぴーたー", false)
197
193
  Oj.default_options = opts
198
194
  end
199
195
 
@@ -410,17 +406,23 @@ class ObjectJuice < Minitest::Test
410
406
  end
411
407
 
412
408
  def test_json_struct
413
- unless 'jruby' == RUBY_DESCRIPTION.split(' ')[0]
414
- obj = Stuck.new(false, 7)
415
- dump_and_load(obj, false)
416
- end
409
+ obj = Stuck.new(false, 7)
410
+ dump_and_load(obj, false)
417
411
  end
418
412
 
419
413
  def test_json_struct2
420
- unless 'jruby' == RUBY_DESCRIPTION.split(' ')[0]
421
- obj = One::Stuck2.new(false, 7)
422
- dump_and_load(obj, false)
423
- end
414
+ obj = One::Stuck2.new(false, 7)
415
+ dump_and_load(obj, false)
416
+ end
417
+
418
+ def test_json_anonymous_struct
419
+ s = Struct.new(:x, :y)
420
+ obj = s.new(1, 2)
421
+ json = Oj.dump(obj, :indent => 2, :mode => :object)
422
+ #puts json
423
+ loaded = Oj.object_load(json);
424
+ assert_equal(obj.members, loaded.members)
425
+ assert_equal(obj.values, loaded.values)
424
426
  end
425
427
 
426
428
  def test_json_non_str_hash
@@ -352,12 +352,12 @@ class ScpTest < Minitest::Test
352
352
  def test_socket_close
353
353
  json = %{{"one":true,"two":false}}
354
354
  Thread.start(json) do |j|
355
- server = TCPServer.new(6969)
355
+ server = TCPServer.new(8080)
356
356
  c = server.accept()
357
357
  c.puts json
358
358
  c.close
359
359
  end
360
- sock = TCPSocket.new('localhost', 6969)
360
+ sock = TCPSocket.new('localhost', 8080)
361
361
  handler = Closer.new(sock)
362
362
  err = nil
363
363
  begin
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: 2.11.4
4
+ version: 2.11.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: 2015-01-20 00:00:00.000000000 Z
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -177,9 +177,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  version: '0'
178
178
  requirements: []
179
179
  rubyforge_project: oj
180
- rubygems_version: 2.2.2
180
+ rubygems_version: 2.4.5
181
181
  signing_key:
182
182
  specification_version: 4
183
183
  summary: A fast JSON parser and serializer.
184
184
  test_files: []
185
- has_rdoc: true