gson 0.6.0-java → 0.6.1-java

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,6 +23,14 @@ Or install it yourself as:
23
23
  Gson::Encoder.new.encode({"abc" => [123, -456.789]})
24
24
  => "{\"abc\":[123,-456.789]}"
25
25
 
26
+ `Gson::Decoder#decode` also accept optional IO or StringIO object:
27
+
28
+ File.open("/tmp/gson.json", "w+") do |io|
29
+ Gson::Encoder.new.encode({"foo" => "bar"}, io)
30
+ end
31
+ File.read("/tmp/gson.json")
32
+ => "{\"foo\":\"bar\"}"
33
+
26
34
  Additional encoder options:
27
35
 
28
36
  * `:html_safe`, default `false`, force encoder to wrte JSON that is
@@ -19,8 +19,8 @@ package gson_ext;
19
19
  import com.google.gson.stream.JsonReader;
20
20
  import com.google.gson.stream.JsonToken;
21
21
  import java.io.IOException;
22
- import java.io.InputStreamReader;
23
22
  import java.io.InputStream;
23
+ import java.io.InputStreamReader;
24
24
  import java.io.Reader;
25
25
  import java.io.StringReader;
26
26
  import java.util.LinkedList;
@@ -32,7 +32,6 @@ import org.jruby.RubyClass;
32
32
  import org.jruby.RubyException;
33
33
  import org.jruby.RubyHash;
34
34
  import org.jruby.RubyIO;
35
- import org.jruby.ext.stringio.RubyStringIO;
36
35
  import org.jruby.RubyNumeric;
37
36
  import org.jruby.RubyObject;
38
37
  import org.jruby.RubyString;
@@ -40,9 +39,10 @@ import org.jruby.RubySymbol;
40
39
  import org.jruby.anno.JRubyClass;
41
40
  import org.jruby.anno.JRubyMethod;
42
41
  import org.jruby.exceptions.RaiseException;
42
+ import org.jruby.ext.stringio.RubyStringIO;
43
+ import org.jruby.java.addons.IOJavaAddons;
43
44
  import org.jruby.runtime.ThreadContext;
44
45
  import org.jruby.runtime.builtin.IRubyObject;
45
- import org.jruby.java.addons.IOJavaAddons;
46
46
 
47
47
  @JRubyClass(name = "Gson::Decoder")
48
48
  public class Decoder extends RubyObject {
@@ -17,26 +17,32 @@
17
17
  package gson_ext;
18
18
 
19
19
  import com.google.gson.stream.JsonWriter;
20
+ import java.io.IOException;
21
+ import java.io.OutputStream;
22
+ import java.io.OutputStreamWriter;
20
23
  import java.io.StringWriter;
21
24
  import java.io.Writer;
22
- import java.io.IOException;
23
25
  import org.jruby.Ruby;
24
26
  import org.jruby.RubyArray;
25
27
  import org.jruby.RubyBoolean;
26
28
  import org.jruby.RubyClass;
27
29
  import org.jruby.RubyException;
28
- import org.jruby.RubyHash;
29
- import org.jruby.RubyNumeric;
30
30
  import org.jruby.RubyFloat;
31
+ import org.jruby.RubyHash;
32
+ import org.jruby.RubyIO;
31
33
  import org.jruby.RubyInteger;
34
+ import org.jruby.RubyNumeric;
32
35
  import org.jruby.RubyObject;
33
36
  import org.jruby.RubyString;
34
37
  import org.jruby.RubySymbol;
35
38
  import org.jruby.anno.JRubyClass;
36
39
  import org.jruby.anno.JRubyMethod;
37
40
  import org.jruby.exceptions.RaiseException;
41
+ import org.jruby.ext.stringio.RubyStringIO;
42
+ import org.jruby.java.addons.IOJavaAddons;
38
43
  import org.jruby.runtime.ThreadContext;
39
44
  import org.jruby.runtime.builtin.IRubyObject;
45
+ import org.jruby.util.IOOutputStream;
40
46
 
41
47
  @JRubyClass(name = "Gson::Encoder")
42
48
  public class Encoder extends RubyObject {
@@ -118,23 +124,38 @@ public class Encoder extends RubyObject {
118
124
 
119
125
  }
120
126
 
121
-
122
- @JRubyMethod
123
- public IRubyObject encode(ThreadContext context, IRubyObject arg) {
127
+ @JRubyMethod(required = 1, optional = 1)
128
+ public IRubyObject encode(ThreadContext context, IRubyObject[] args) {
124
129
  Ruby ruby = context.getRuntime();
130
+ Writer out = null;
131
+
132
+ if (args.length < 2 || args[1].isNil()) {
133
+ out = new StringWriter();
134
+ } else {
135
+ IRubyObject io = args[1];
136
+ if ((io instanceof RubyIO) || (io instanceof RubyStringIO)) {
137
+ IRubyObject stream = IOJavaAddons.AnyIO.any_to_outputstream(context, io);
138
+ out = new OutputStreamWriter((OutputStream)stream.toJava(OutputStream.class));
139
+ } else {
140
+ throw ruby.newArgumentError("Unsupported source. This method accepts IO");
141
+ }
142
+ }
125
143
 
126
- StringWriter out = new StringWriter();
127
144
  JsonWriter writer = new JsonWriter(out);
128
145
  writer.setLenient(this.lenient);
129
146
  writer.setHtmlSafe(this.htmlSafe);
130
147
  writer.setIndent(this.indent);
131
148
  writer.setSerializeNulls(this.serializeNulls);
132
149
  try {
133
- encodeValue(writer, context, arg);
150
+ encodeValue(writer, context, args[0]);
134
151
  } catch (Exception ex) {
135
152
  throw EncodeError.newEncodeError(ruby, ex.getMessage());
136
153
  }
137
- return ruby.newString(out.toString());
154
+ if (out instanceof StringWriter) {
155
+ return ruby.newString(out.toString());
156
+ } else {
157
+ return context.nil;
158
+ }
138
159
  }
139
160
 
140
161
  private void encodeValue(JsonWriter writer, ThreadContext context, IRubyObject val)
@@ -180,6 +201,7 @@ public class Encoder extends RubyObject {
180
201
  } else {
181
202
  writer.value(val.anyToString().toString());
182
203
  }
204
+ writer.flush();
183
205
  }
184
206
 
185
207
  }
@@ -16,5 +16,5 @@
16
16
  #
17
17
 
18
18
  module Gson
19
- VERSION = "0.6.0"
19
+ VERSION = "0.6.1"
20
20
  end
@@ -139,4 +139,11 @@ EOJ
139
139
  assert_equal(expected, decoder.decode(dumped_json))
140
140
  end
141
141
 
142
+ def test_it_supports_output_streams
143
+ encoder = Gson::Encoder.new
144
+ expected = '{"foo":1,"bar":2}'
145
+ output = StringIO.new
146
+ encoder.encode({:foo => 1, :bar => 2}, output)
147
+ assert_equal expected, output.string
148
+ end
142
149
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: gson
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.0
5
+ version: 0.6.1
6
6
  platform: java
7
7
  authors:
8
8
  - Sergey Avseyev
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-05 00:00:00.000000000 Z
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler