oj 2.1.4 → 2.1.6

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: e9c84fb3930bef0021b22138c0e4ee9b42785b1a
4
- data.tar.gz: cba9e1d4cd91413a80b7ff6521ccb6b4c41927d9
3
+ metadata.gz: df465b98a85257f57cccf95bc93cb68de76fece4
4
+ data.tar.gz: 4092e954c5cb1bf3a1cf7a4d8729f4713bbee8bd
5
5
  SHA512:
6
- metadata.gz: 8a6741639d1ba3a7bd3292980ac7819aa6497c8c14621beefc4a1b151c7b8735f787460a02fd0edc8d4fd8c884cf66a453ac5439be7ecb58bf73f85bea21fe7b
7
- data.tar.gz: 7e822754689d760c7e35b648d601f641e33edc1922e747051046abf2f9785f69a6fd0643b24951de6f2fc0e1352689780d64aeb595375c6181d143080bf18075
6
+ metadata.gz: af5ae926a363a12681930df3dee6718f0ada11e43dbe54eef5916265efe65d99fa8d79ce8cdbb2158c2a6aa2111c236f61e6684dcd9699a680efc9c2be603231
7
+ data.tar.gz: 07fa5429999fbbce344b93d3c2afebbd275241ec0e8e733fa8a7ceb9d0da32b34b90effa57881ed68c99c8c0a8cd3ed25919c1747d2f5dc51625d1740ca7c3b8
data/README.md CHANGED
@@ -20,9 +20,9 @@ Follow [@peterohler on Twitter](http://twitter.com/#!/peterohler) for announceme
20
20
 
21
21
  [![Build Status](https://secure.travis-ci.org/ohler55/oj.png?branch=master)](http://travis-ci.org/ohler55/oj)
22
22
 
23
- ### Current Release 2.1.4
23
+ ### Current Release 2.1.6
24
24
 
25
- - Fixed Linux 32 bit rounding bug.
25
+ - Added Oj.to_stream() for dumping JSON to an IO object.
26
26
 
27
27
  [Older release notes](http://www.ohler.com/dev/oj_misc/release_notes.html).
28
28
 
@@ -35,7 +35,8 @@ than any other Ruby JSON parser and 3 or more times faster writing JSON.
35
35
 
36
36
  Oj has several dump or serialization modes which control how Objects are
37
37
  converted to JSON. These modes are set with the :mode option in either the
38
- default options or as one of the options to the dump() method.
38
+ default options or as one of the options to the dump() method. The default mode
39
+ is the :object mode.
39
40
 
40
41
  - :strict mode will only allow the 7 basic JSON types to be serialized. Any
41
42
  other Object will raise and Exception.
@@ -37,6 +37,8 @@
37
37
  #include <stdio.h>
38
38
  #include <string.h>
39
39
  #include <math.h>
40
+ #include <unistd.h>
41
+ #include <errno.h>
40
42
 
41
43
  #include "oj.h"
42
44
  #include "cache8.h"
@@ -1138,7 +1140,7 @@ dump_data_comp(VALUE obj, int depth, Out out) {
1138
1140
  case RubyTime: dump_ruby_time(obj, out); break;
1139
1141
  case XmlTime: dump_xml_time(obj, out); break;
1140
1142
  case UnixTime:
1141
- default: dump_time(obj, out); break;
1143
+ default: dump_time(obj, out); break;
1142
1144
  }
1143
1145
  } else if (oj_bigdecimal_class == clas) {
1144
1146
  VALUE rstr = rb_funcall(obj, oj_to_s_id, 0);
@@ -1671,6 +1673,7 @@ oj_write_obj_to_file(VALUE obj, const char *path, Options copts) {
1671
1673
  struct _Out out;
1672
1674
  size_t size;
1673
1675
  FILE *f;
1676
+ int ok;
1674
1677
 
1675
1678
  out.buf = buf;
1676
1679
  out.end = buf + sizeof(buf) - 10;
@@ -1678,17 +1681,62 @@ oj_write_obj_to_file(VALUE obj, const char *path, Options copts) {
1678
1681
  oj_dump_obj_to_json(obj, copts, &out);
1679
1682
  size = out.cur - out.buf;
1680
1683
  if (0 == (f = fopen(path, "w"))) {
1684
+ if (out.allocated) {
1685
+ xfree(out.buf);
1686
+ }
1681
1687
  rb_raise(rb_eIOError, "%s\n", strerror(errno));
1682
1688
  }
1683
- if (size != fwrite(out.buf, 1, size, f)) {
1689
+ ok = (size == fwrite(out.buf, 1, size, f));
1690
+ if (out.allocated) {
1691
+ xfree(out.buf);
1692
+ }
1693
+ fclose(f);
1694
+ if (!ok) {
1684
1695
  int err = ferror(f);
1685
1696
 
1686
1697
  rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
1687
1698
  }
1699
+ }
1700
+
1701
+ void
1702
+ oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts) {
1703
+ char buf[4096];
1704
+ struct _Out out;
1705
+ size_t size;
1706
+ VALUE clas = rb_obj_class(stream);
1707
+ VALUE s;
1708
+
1709
+ out.buf = buf;
1710
+ out.end = buf + sizeof(buf) - 10;
1711
+ out.allocated = 0;
1712
+ oj_dump_obj_to_json(obj, copts, &out);
1713
+ size = out.cur - out.buf;
1714
+ if (oj_stringio_class == clas) {
1715
+ rb_funcall(stream, oj_write_id, 1, rb_str_new(out.buf, size));
1716
+ #ifndef JRUBY_RUBY
1717
+ #if !IS_WINDOWS
1718
+ } else if (rb_respond_to(stream, oj_fileno_id) && Qnil != (s = rb_funcall(stream, oj_fileno_id, 0))) {
1719
+ int fd = FIX2INT(s);
1720
+
1721
+ if (size != write(fd, out.buf, size)) {
1722
+ if (out.allocated) {
1723
+ xfree(out.buf);
1724
+ }
1725
+ rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", errno, strerror(errno));
1726
+ }
1727
+ #endif
1728
+ #endif
1729
+ } else if (rb_respond_to(stream, oj_write_id)) {
1730
+ s = rb_funcall(stream, oj_write_id, 1, rb_str_new(out.buf, size));
1731
+ } else {
1732
+ if (out.allocated) {
1733
+ xfree(out.buf);
1734
+ }
1735
+ rb_raise(rb_eArgError, "to_stream() expected an IO Object.");
1736
+ }
1688
1737
  if (out.allocated) {
1689
1738
  xfree(out.buf);
1690
1739
  }
1691
- fclose(f);
1692
1740
  }
1693
1741
 
1694
1742
  // dump leaf functions
@@ -26,7 +26,7 @@ dflags = {
26
26
  'HAS_NANO_TIME' => ('ruby' == type && ('1' == version[0] && '9' == version[1]) || '2' <= version[0]) ? 1 : 0,
27
27
  'HAS_RSTRUCT' => ('ruby' == type || 'ree' == type || 'tcs-ruby' == type) ? 1 : 0,
28
28
  'HAS_IVAR_HELPERS' => ('ruby' == type && !is_windows && (('1' == version[0] && '9' == version[1]) || '2' <= version[0])) ? 1 : 0,
29
- 'HAS_EXCEPTION_MAGIC' => ('ruby' == type && !is_windows && ('1' == version[0] && '9' == version[1])) ? 0 : 1,
29
+ 'HAS_EXCEPTION_MAGIC' => ('ruby' == type && ('1' == version[0] && '9' == version[1])) ? 0 : 1,
30
30
  'HAS_PROC_WITH_BLOCK' => ('ruby' == type && (('1' == version[0] && '9' == version[1]) || '2' <= version[0])) ? 1 : 0,
31
31
  'HAS_GC_GUARD' => ('jruby' != type && 'rubinius' != type) ? 1 : 0,
32
32
  'HAS_TOP_LEVEL_ST_H' => ('ree' == type || ('ruby' == type && '1' == version[0] && '8' == version[1])) ? 1 : 0,
@@ -712,6 +712,27 @@ to_file(int argc, VALUE *argv, VALUE self) {
712
712
  return Qnil;
713
713
  }
714
714
 
715
+ /* call-seq: to_stream(io, obj, options)
716
+ *
717
+ * Dumps an Object to the specified IO stream.
718
+ * @param [IO] io IO stream to write the JSON document to
719
+ * @param [Object] obj Object to serialize as an JSON document String
720
+ * @param [Hash] options formating options
721
+ * @param [Fixnum] :indent format expected
722
+ * @param [true|false] :circular allow circular references, default: false
723
+ */
724
+ static VALUE
725
+ to_stream(int argc, VALUE *argv, VALUE self) {
726
+ struct _Options copts = oj_default_options;
727
+
728
+ if (3 == argc) {
729
+ oj_parse_options(argv[2], &copts);
730
+ }
731
+ oj_write_obj_to_stream(argv[1], *argv, &copts);
732
+
733
+ return Qnil;
734
+ }
735
+
715
736
  // Mimic JSON section
716
737
 
717
738
  static VALUE
@@ -1117,6 +1138,7 @@ void Init_oj() {
1117
1138
 
1118
1139
  rb_define_module_function(Oj, "dump", dump, -1);
1119
1140
  rb_define_module_function(Oj, "to_file", to_file, -1);
1141
+ rb_define_module_function(Oj, "to_stream", to_stream, -1);
1120
1142
 
1121
1143
  rb_define_module_function(Oj, "saj_parse", oj_saj_parse, -1);
1122
1144
  rb_define_module_function(Oj, "sc_parse", oj_sc_parse, -1);
@@ -165,6 +165,7 @@ extern void oj_parse_options(VALUE ropts, Options copts);
165
165
 
166
166
  extern void oj_dump_obj_to_json(VALUE obj, Options copts, Out out);
167
167
  extern void oj_write_obj_to_file(VALUE obj, const char *path, Options copts);
168
+ extern void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts);
168
169
  extern void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out);
169
170
  extern void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts);
170
171
 
@@ -214,6 +215,7 @@ extern ID oj_tv_nsec_id;
214
215
  extern ID oj_tv_sec_id;
215
216
  extern ID oj_tv_usec_id;
216
217
  extern ID oj_utc_offset_id;
218
+ extern ID oj_write_id;
217
219
 
218
220
  #if SAFE_CACHE
219
221
  extern pthread_mutex_t oj_cache_mutex;
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '2.1.4'
4
+ VERSION = '2.1.6'
5
5
  end
@@ -78,7 +78,6 @@ if files.empty?
78
78
  File.open('sample.marshal', 'w') { |f| f.write($mars) }
79
79
  else
80
80
  puts "loading and parsing #{files}\n\n"
81
- # TBD change to allow xml and json
82
81
  data = files.map do |f|
83
82
  $xml = File.read(f)
84
83
  $obj = Ox.load($xml);
@@ -933,29 +933,25 @@ class Juice < ::Test::Unit::TestCase
933
933
 
934
934
  # Stream IO
935
935
  def test_io_string
936
- json = %{{
937
- "x":true,
938
- "y":58,
939
- "z": [1,2,3]
940
- }
941
- }
942
- input = StringIO.new(json)
936
+ src = { 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}
937
+ output = StringIO.open("", "w+")
938
+ Oj.to_stream(output, src)
939
+
940
+ input = StringIO.new(output.string())
943
941
  obj = Oj.load(input, :mode => :strict)
944
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
942
+ assert_equal(src, obj)
945
943
  end
946
944
 
947
945
  def test_io_file
946
+ src = { 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}
948
947
  filename = 'open_file_test.json'
949
- File.open(filename, 'w') { |f| f.write(%{{
950
- "x":true,
951
- "y":58,
952
- "z": [1,2,3]
953
- }
954
- }) }
948
+ File.open(filename, "w") { |f|
949
+ Oj.to_stream(f, src)
950
+ }
955
951
  f = File.new(filename)
956
952
  obj = Oj.load(f, :mode => :strict)
957
953
  f.close()
958
- assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
954
+ assert_equal(src, obj)
959
955
  end
960
956
 
961
957
  # symbol_keys option
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.1.4
4
+ version: 2.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-04 00:00:00.000000000 Z
11
+ date: 2013-10-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'The fastest JSON parser and object serializer. '
14
14
  email: peter@ohler.com
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project: oj
121
- rubygems_version: 2.0.2
121
+ rubygems_version: 2.0.3
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: A fast JSON parser and serializer.