oj 2.12.11 → 2.12.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13e345b717fb9e6ce7ab0c1f7e96c23586148c16
4
+ data.tar.gz: 19dd28489fcd97c301e4ea744f3b9114c3de107b
5
+ SHA512:
6
+ metadata.gz: cdc08e19e3621678e07531b89831fda57c23b4b4efa1f50ec94573058e0f82864dab1cfd62b4a9bc4a97732d17fb77c49d1f88bad7c7503334cc78e15981095e
7
+ data.tar.gz: 46a77fd33b9b2b2424082f84e401d39bad85da74887e35cc4450310c4b0798f490e440a4a21e2987868e0f0c07486020bf885a1ec4d85314682164d0b7537d9a
data/README.md CHANGED
@@ -26,7 +26,11 @@ 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.12.11
29
+ ## Current Release 2.12.12
30
+
31
+ - Thanks to asurin for adding support for arguments to to_json() that rails uses.
32
+
33
+ ## Release 2.12.11
30
34
 
31
35
  - Oj::ParseError is now thrown instead of SyntaxError when there are multiple
32
36
  JSON documents in a string or file and there is no proc or block associated
@@ -59,7 +59,7 @@
59
59
  typedef unsigned long ulong;
60
60
 
61
61
  static void raise_strict(VALUE obj);
62
- static void dump_val(VALUE obj, int depth, Out out);
62
+ static void dump_val(VALUE obj, int depth, Out out, int argc, VALUE *argv);
63
63
  static void dump_nil(Out out);
64
64
  static void dump_true(Out out);
65
65
  static void dump_false(Out out);
@@ -87,7 +87,7 @@ static void dump_data_strict(VALUE obj, Out out);
87
87
  static void dump_data_null(VALUE obj, Out out);
88
88
  static void dump_data_comp(VALUE obj, int depth, Out out);
89
89
  static void dump_data_obj(VALUE obj, int depth, Out out);
90
- static void dump_obj_comp(VALUE obj, int depth, Out out);
90
+ static void dump_obj_comp(VALUE obj, int depth, Out out, int argc, VALUE *argv);
91
91
  static void dump_obj_obj(VALUE obj, int depth, Out out);
92
92
  static void dump_struct_comp(VALUE obj, int depth, Out out);
93
93
  static void dump_struct_obj(VALUE obj, int depth, Out out);
@@ -728,7 +728,7 @@ dump_array(VALUE a, VALUE clas, int depth, Out out) {
728
728
  }
729
729
  }
730
730
  }
731
- dump_val(rb_ary_entry(a, i), d2, out);
731
+ dump_val(rb_ary_entry(a, i), d2, out, 0, 0);
732
732
  if (i < cnt) {
733
733
  *out->cur++ = ',';
734
734
  }
@@ -806,7 +806,7 @@ hash_cb_strict(VALUE key, VALUE value, Out out) {
806
806
  out->cur += out->opts->dump_opts->after_size;
807
807
  }
808
808
  }
809
- dump_val(value, depth, out);
809
+ dump_val(value, depth, out, 0, 0);
810
810
  out->depth = depth;
811
811
  *out->cur++ = ',';
812
812
 
@@ -870,7 +870,7 @@ hash_cb_compat(VALUE key, VALUE value, Out out) {
870
870
  out->cur += out->opts->dump_opts->after_size;
871
871
  }
872
872
  }
873
- dump_val(value, depth, out);
873
+ dump_val(value, depth, out, 0, 0);
874
874
  out->depth = depth;
875
875
  *out->cur++ = ',';
876
876
 
@@ -889,11 +889,11 @@ hash_cb_object(VALUE key, VALUE value, Out out) {
889
889
  if (rb_type(key) == T_STRING) {
890
890
  dump_str_obj(key, Qundef, depth, out);
891
891
  *out->cur++ = ':';
892
- dump_val(value, depth, out);
892
+ dump_val(value, depth, out, 0, 0);
893
893
  } else if (rb_type(key) == T_SYMBOL) {
894
894
  dump_sym_obj(key, out);
895
895
  *out->cur++ = ':';
896
- dump_val(value, depth, out);
896
+ dump_val(value, depth, out, 0, 0);
897
897
  } else {
898
898
  int d2 = depth + 1;
899
899
  long s2 = size + out->indent + 1;
@@ -921,13 +921,13 @@ hash_cb_object(VALUE key, VALUE value, Out out) {
921
921
  *out->cur++ = ':';
922
922
  *out->cur++ = '[';
923
923
  fill_indent(out, d2);
924
- dump_val(key, d2, out);
924
+ dump_val(key, d2, out, 0, 0);
925
925
  if (out->end - out->cur <= (long)s2) {
926
926
  grow(out, s2);
927
927
  }
928
928
  *out->cur++ = ',';
929
929
  fill_indent(out, d2);
930
- dump_val(value, d2, out);
930
+ dump_val(value, d2, out, 0, 0);
931
931
  if (out->end - out->cur <= (long)size) {
932
932
  grow(out, size);
933
933
  }
@@ -1266,7 +1266,7 @@ dump_data_comp(VALUE obj, int depth, Out out) {
1266
1266
 
1267
1267
  dump_cstr(rb_string_value_ptr((VALUE*)&rstr), RSTRING_LEN(rstr), 0, 0, out);
1268
1268
  } else {
1269
- dump_val(aj, depth, out);
1269
+ dump_val(aj, depth, out, 0, 0);
1270
1270
  }
1271
1271
  } else if (Yes == out->opts->to_json && rb_respond_to(obj, oj_to_json_id)) {
1272
1272
  volatile VALUE rs;
@@ -1349,7 +1349,7 @@ dump_data_obj(VALUE obj, int depth, Out out) {
1349
1349
  }
1350
1350
 
1351
1351
  static void
1352
- dump_obj_comp(VALUE obj, int depth, Out out) {
1352
+ dump_obj_comp(VALUE obj, int depth, Out out, int argc, VALUE *argv) {
1353
1353
  if (rb_respond_to(obj, oj_to_hash_id)) {
1354
1354
  volatile VALUE h = rb_funcall(obj, oj_to_hash_id, 0);
1355
1355
 
@@ -1358,7 +1358,7 @@ dump_obj_comp(VALUE obj, int depth, Out out) {
1358
1358
  }
1359
1359
  dump_hash(h, Qundef, depth, out->opts->mode, out);
1360
1360
  } else if (rb_respond_to(obj, oj_as_json_id)) {
1361
- volatile VALUE aj = rb_funcall(obj, oj_as_json_id, 0);
1361
+ volatile VALUE aj = rb_funcall2(obj, oj_as_json_id, argc, argv);
1362
1362
 
1363
1363
  // Catch the obvious brain damaged recursive dumping.
1364
1364
  if (aj == obj) {
@@ -1366,7 +1366,7 @@ dump_obj_comp(VALUE obj, int depth, Out out) {
1366
1366
 
1367
1367
  dump_cstr(rb_string_value_ptr((VALUE*)&rstr), RSTRING_LEN(rstr), 0, 0, out);
1368
1368
  } else {
1369
- dump_val(aj, depth, out);
1369
+ dump_val(aj, depth, out, 0, 0);
1370
1370
  }
1371
1371
  } else if (Yes == out->opts->to_json && rb_respond_to(obj, oj_to_json_id)) {
1372
1372
  volatile VALUE rs;
@@ -1477,7 +1477,7 @@ dump_attr_cb(ID key, VALUE value, Out out) {
1477
1477
  dump_cstr(buf, strlen(buf), 0, 0, out);
1478
1478
  }
1479
1479
  *out->cur++ = ':';
1480
- dump_val(value, depth, out);
1480
+ dump_val(value, depth, out, 0, 0);
1481
1481
  out->depth = depth;
1482
1482
  *out->cur++ = ',';
1483
1483
 
@@ -1631,7 +1631,7 @@ dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out) {
1631
1631
  dump_cstr(buf, strlen(attr) + 1, 0, 0, out);
1632
1632
  }
1633
1633
  *out->cur++ = ':';
1634
- dump_val(rb_ivar_get(obj, vid), d2, out);
1634
+ dump_val(rb_ivar_get(obj, vid), d2, out, 0, 0);
1635
1635
  if (out->end - out->cur <= 2) {
1636
1636
  grow(out, 2);
1637
1637
  }
@@ -1652,7 +1652,7 @@ dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out) {
1652
1652
  dump_cstr("~mesg", 5, 0, 0, out);
1653
1653
  *out->cur++ = ':';
1654
1654
  rv = rb_funcall2(obj, rb_intern("message"), 0, 0);
1655
- dump_val(rv, d2, out);
1655
+ dump_val(rv, d2, out, 0, 0);
1656
1656
  if (out->end - out->cur <= 2) {
1657
1657
  grow(out, 2);
1658
1658
  }
@@ -1665,7 +1665,7 @@ dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out) {
1665
1665
  dump_cstr("~bt", 3, 0, 0, out);
1666
1666
  *out->cur++ = ':';
1667
1667
  rv = rb_funcall2(obj, rb_intern("backtrace"), 0, 0);
1668
- dump_val(rv, d2, out);
1668
+ dump_val(rv, d2, out, 0, 0);
1669
1669
  if (out->end - out->cur <= 2) {
1670
1670
  grow(out, 2);
1671
1671
  }
@@ -1696,7 +1696,7 @@ dump_struct_comp(VALUE obj, int depth, Out out) {
1696
1696
 
1697
1697
  dump_cstr(rb_string_value_ptr((VALUE*)&rstr), RSTRING_LEN(rstr), 0, 0, out);
1698
1698
  } else {
1699
- dump_val(aj, depth, out);
1699
+ dump_val(aj, depth, out, 0, 0);
1700
1700
  }
1701
1701
  } else if (Yes == out->opts->to_json && rb_respond_to(obj, oj_to_json_id)) {
1702
1702
  volatile VALUE rs = rb_funcall(obj, oj_to_json_id, 0);
@@ -1778,7 +1778,7 @@ dump_struct_obj(VALUE obj, int depth, Out out) {
1778
1778
  grow(out, size);
1779
1779
  }
1780
1780
  fill_indent(out, d3);
1781
- dump_val(*vp, d3, out);
1781
+ dump_val(*vp, d3, out, 0, 0);
1782
1782
  *out->cur++ = ',';
1783
1783
  }
1784
1784
  }
@@ -1793,7 +1793,7 @@ dump_struct_obj(VALUE obj, int depth, Out out) {
1793
1793
  grow(out, size);
1794
1794
  }
1795
1795
  fill_indent(out, d3);
1796
- dump_val(rb_struct_aref(obj, INT2FIX(i)), d3, out);
1796
+ dump_val(rb_struct_aref(obj, INT2FIX(i)), d3, out, 0, 0);
1797
1797
  *out->cur++ = ',';
1798
1798
  }
1799
1799
  }
@@ -1876,7 +1876,7 @@ dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
1876
1876
  fill_indent(out, d2);
1877
1877
  dump_cstr(name, nlen, 0, 0, out);
1878
1878
  *out->cur++ = ':';
1879
- dump_val(v, d2, out);
1879
+ dump_val(v, d2, out, 0, 0);
1880
1880
  if (out->end - out->cur <= 2) {
1881
1881
  grow(out, 2);
1882
1882
  }
@@ -1893,7 +1893,7 @@ raise_strict(VALUE obj) {
1893
1893
  }
1894
1894
 
1895
1895
  static void
1896
- dump_val(VALUE obj, int depth, Out out) {
1896
+ dump_val(VALUE obj, int depth, Out out, int argc, VALUE *argv) {
1897
1897
  int type = rb_type(obj);
1898
1898
 
1899
1899
  if (MAX_DEPTH < depth) {
@@ -1965,7 +1965,7 @@ dump_val(VALUE obj, int depth, Out out) {
1965
1965
  switch (out->opts->mode) {
1966
1966
  case StrictMode: dump_data_strict(obj, out); break;
1967
1967
  case NullMode: dump_data_null(obj, out); break;
1968
- case CompatMode: dump_obj_comp(obj, depth, out); break;
1968
+ case CompatMode: dump_obj_comp(obj, depth, out, argc, argv); break;
1969
1969
  case ObjectMode:
1970
1970
  default: dump_obj_obj(obj, depth, out); break;
1971
1971
  }
@@ -1988,7 +1988,7 @@ dump_val(VALUE obj, int depth, Out out) {
1988
1988
  case NullMode: dump_nil(out); break;
1989
1989
  case CompatMode:
1990
1990
  case ObjectMode:
1991
- default: dump_obj_comp(obj, depth, out); break;
1991
+ default: dump_obj_comp(obj, depth, out, argc, argv); break;
1992
1992
  }
1993
1993
  break;
1994
1994
  default:
@@ -2002,6 +2002,11 @@ dump_val(VALUE obj, int depth, Out out) {
2002
2002
 
2003
2003
  void
2004
2004
  oj_dump_obj_to_json(VALUE obj, Options copts, Out out) {
2005
+ oj_dump_obj_to_json_using_params(obj, copts, out, 0, 0);
2006
+ }
2007
+
2008
+ void
2009
+ oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int argc, VALUE *argv) {
2005
2010
  if (0 == out->buf) {
2006
2011
  out->buf = ALLOC_N(char, 4096);
2007
2012
  out->end = out->buf + 4095 - BUFFER_EXTRA; // 1 less than end plus extra for possible errors
@@ -2015,7 +2020,7 @@ oj_dump_obj_to_json(VALUE obj, Options copts, Out out) {
2015
2020
  oj_cache8_new(&out->circ_cache);
2016
2021
  }
2017
2022
  out->indent = copts->indent;
2018
- dump_val(obj, 0, out);
2023
+ dump_val(obj, 0, out, argc, argv);
2019
2024
  if (0 < out->indent) {
2020
2025
  switch (*(out->cur - 1)) {
2021
2026
  case ']':
@@ -2462,7 +2467,7 @@ oj_str_writer_push_value(StrWriter sw, VALUE val, const char *key) {
2462
2467
  *sw->out.cur++ = ':';
2463
2468
  }
2464
2469
  }
2465
- dump_val(val, sw->depth, &sw->out);
2470
+ dump_val(val, sw->depth, &sw->out, 0, 0);
2466
2471
  }
2467
2472
 
2468
2473
  void
@@ -1821,7 +1821,7 @@ mimic_object_to_json(int argc, VALUE *argv, VALUE self) {
1821
1821
  // To be strict the mimic_object_to_json_options should be used but people
1822
1822
  // seem to prefer the option of changing that.
1823
1823
  //oj_dump_obj_to_json(self, &mimic_object_to_json_options, &out);
1824
- oj_dump_obj_to_json(self, &copts, &out);
1824
+ oj_dump_obj_to_json_using_params(self, &copts, &out, argc, argv);
1825
1825
  if (0 == out.buf) {
1826
1826
  rb_raise(rb_eNoMemError, "Not enough memory.");
1827
1827
  }
@@ -216,6 +216,7 @@ extern VALUE oj_object_parse_cstr(int argc, VALUE *argv, char *json, size_t len)
216
216
  extern void oj_parse_options(VALUE ropts, Options copts);
217
217
 
218
218
  extern void oj_dump_obj_to_json(VALUE obj, Options copts, Out out);
219
+ extern void oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int argc, VALUE *argv);
219
220
  extern void oj_write_obj_to_file(VALUE obj, const char *path, Options copts);
220
221
  extern void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts);
221
222
  extern void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out);
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '2.12.11'
4
+ VERSION = '2.12.12'
5
5
  end
@@ -1,3 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
1
3
  #!/usr/bin/env ruby
2
4
  # encoding: UTF-8
3
5
 
@@ -5,60 +7,47 @@ $: << File.dirname(__FILE__)
5
7
 
6
8
  require 'helper'
7
9
 
8
- require 'oj'
9
- require 'securerandom'
10
-
11
10
  class Handler
12
- def hash_start() {} end
13
- def hash_set(h,k,v) h.store(k,v) end
14
- def array_start() [] end
15
- def array_append(a,v) a << v end
16
- def error(message, line, column)
17
- raise Exception.new(message, line, column)
11
+ def initialize
12
+ @state = []
13
+ end
14
+
15
+ def hash_start
16
+ @state << {}
17
+ @state.last
18
+ end
19
+
20
+ def hash_end
21
+ @state.pop
22
+ end
23
+
24
+ def hash_set(h,k,v)
25
+ h.store(k,v)
18
26
  end
19
- end
20
27
 
21
- json = Oj.dump({"this"=>"object"})
22
-
23
- if true
24
- name = "/tmp/#{SecureRandom.uuid}"
25
- `mkfifo #{name}`
26
- if fork
27
- open(name, 'r+') do |read_io|
28
- p "start reading #{read_io.stat.ftype}"
29
- Oj.sc_parse(Handler.new, read_io) {|v| p v}
30
- p "stop reading"
31
- end
32
- else
33
- open(name, 'w+') do |write_io|
34
- p "start writing #{write_io.stat.ftype} autoclose: #{write_io.autoclose?}"
35
- write_io.write json
36
- write_io.write json
37
- p "stop writing"
38
- end
39
- sleep(1) # make it obvious that there are two threads
40
- open(name, 'w+') do |write_io|
41
- p "start writing #{write_io.stat.ftype}"
42
- write_io.write json
43
- write_io.write json
44
- p "stop writing"
45
- end
28
+ def array_start
29
+ @state << []
30
+ @state.last
46
31
  end
47
- else
48
- IO.pipe do |read_io, write_io|
49
- if fork
50
- write_io.close
51
- p "start reading #{read_io.stat.ftype}"
52
- Oj.sc_parse(Handler.new, read_io) {|v| p v}
53
- p "stop reading"
54
- read_io.close
55
- else
56
- read_io.close
57
- p "start writing #{write_io.stat.ftype}"
58
- write_io.write json
59
- write_io.write json
60
- p "stop writing"
61
- write_io.close
62
- end
32
+
33
+
34
+ def array_end
35
+ @state.pop
36
+ end
37
+
38
+ def array_append(a,v)
39
+ a << v
40
+ end
41
+
42
+ def add_value(v)
43
+ p v
63
44
  end
45
+
46
+ def error(message, line, column); p "ERROR: #{message}" end
64
47
  end
48
+
49
+ $handler = Handler.new
50
+
51
+ IO.popen("cat tst") { |p| puts Oj.sc_parse($handler, p) }
52
+
53
+ #File.open('tst', 'r') { |file| Oj.sc_parse($handler, file) }
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #!/usr/bin/env ruby
4
+ # encoding: UTF-8
5
+
6
+ $: << File.dirname(__FILE__)
7
+
8
+ require 'helper'
9
+
10
+
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #!/usr/bin/env ruby
4
+ # encoding: UTF-8
5
+
6
+ $: << File.dirname(__FILE__)
7
+ %w(lib ext test).each do |dir|
8
+ $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
9
+ end
10
+
11
+ require 'oj'
12
+ require 'stringio'
13
+
14
+ class Parser < Oj::Saj
15
+
16
+ def parse(json)
17
+ Oj.saj_parse(self, StringIO.new(json))
18
+ end
19
+
20
+ def hash_start(key)
21
+ puts "START: #{key}"
22
+ end
23
+
24
+ def error(message, line, column)
25
+ puts "Error callback: #{message}"
26
+ end
27
+
28
+ end
29
+
30
+ parser = Parser.new
31
+
32
+ begin
33
+ # truncated JSON, Oj.saj_parse raises, #error not called
34
+ parser.parse('{"foo{"bar":')
35
+ rescue Exception => e
36
+ puts "*** #{e.class}: #{e.message}"
37
+ end
38
+
39
+ puts "\n\n"
40
+
41
+ begin
42
+ # invalid JSON, doesn't raise an error
43
+ parser.parse('{"foo":{"bar":}')
44
+ rescue Exception => e
45
+ puts "*** #{e.class}: #{e.message}"
46
+ end
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ %w(lib ext test).each do |dir|
5
+ $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
6
+ end
7
+
8
+ require 'oj'
9
+
10
+ def create_item(doc)
11
+ #puts "#{doc.fetch('/id')}: #{doc.fetch('/labels/it/value')} - #{doc.fetch('/descriptions/it/value')}"
12
+ doc.fetch('/id')
13
+ doc.fetch('/labels/it/value')
14
+ doc.fetch('/descriptions/it/value')
15
+ end
16
+
17
+ 100.times { |i|
18
+ File.open('dump_10k.json') { |f|
19
+ f.each { |line|
20
+ #Oj::Doc.open(line) { |doc|
21
+ doc = Oj::Doc.open(line)
22
+ begin
23
+ create_item(doc) if doc.fetch('/type') == 'item'
24
+ rescue Exception => e
25
+ puts "*** #{e.class}: #{e.message}"
26
+ end
27
+ doc.close
28
+ #}
29
+ }
30
+ }
31
+ puts i
32
+ }