gson 0.5.0-java → 0.6.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -75,8 +75,8 @@ public class Encoder extends RubyObject {
75
75
  @JRubyMethod(optional = 1)
76
76
  public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
77
77
  Ruby ruby = context.getRuntime();
78
- this.options = context.nil;
79
- if (args.length < 1) {
78
+ if (args.length < 1 || args[0].isNil()) {
79
+ this.options = RubyHash.newHash(ruby);
80
80
  return context.nil;
81
81
  }
82
82
  if (!(args[0] instanceof RubyHash)) {
@@ -173,8 +173,12 @@ public class Encoder extends RubyObject {
173
173
  writer.value((Double)((RubyFloat)val).getDoubleValue());
174
174
  } else if (val instanceof RubyBoolean) {
175
175
  writer.value(val.isTrue());
176
- } else if (val.respondsTo("to_json")) {
177
- encodeValue(writer, context, val.callMethod(context, "to_json", this.options));
176
+ } else if (val.respondsTo("as_json")) {
177
+ encodeValue(writer, context, val.callMethod(context, "as_json", this.options));
178
+ } else if (val.respondsTo("to_s")) {
179
+ writer.value(val.toString());
180
+ } else {
181
+ writer.value(val.anyToString().toString());
178
182
  }
179
183
  }
180
184
 
data/lib/gson/version.rb CHANGED
@@ -16,5 +16,5 @@
16
16
  #
17
17
 
18
18
  module Gson
19
- VERSION = "0.5.0"
19
+ VERSION = "0.6.0"
20
20
  end
data/test/test_encoder.rb CHANGED
@@ -99,4 +99,44 @@ EOJ
99
99
  end
100
100
  end
101
101
 
102
+ class Custom
103
+ attr_accessor :foo, :bar
104
+
105
+ def initialize(foo, bar)
106
+ @foo = foo
107
+ @bar = bar
108
+ end
109
+
110
+ def as_json(options = {})
111
+ {:foo => @foo, :bar => @bar}
112
+ end
113
+ end
114
+
115
+ def test_it_dumps_custom_objects_wich_implement_as_json
116
+ encoder = Gson::Encoder.new
117
+ expected = '{"foo":1,"bar":2}'
118
+ assert_equal expected, encoder.encode(Custom.new(1, 2))
119
+ end
120
+
121
+ def test_it_converts_unknown_objects_to_string
122
+ time = Time.at(1355218745).utc
123
+
124
+ # time does not respond to to_json method
125
+ def time.respond_to?(method, *args)
126
+ return false if method == :to_json
127
+ super
128
+ end
129
+
130
+ encoder = Gson::Encoder.new
131
+ decoder = Gson::Decoder.new
132
+
133
+ dumped_json = encoder.encode(time)
134
+ expected = if RUBY_VERSION > '1.9'
135
+ '2012-12-11 09:39:05 UTC'
136
+ else
137
+ 'Tue Dec 11 09:39:05 UTC 2012'
138
+ end
139
+ assert_equal(expected, decoder.decode(dumped_json))
140
+ end
141
+
102
142
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: gson
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.0
5
+ version: 0.6.0
6
6
  platform: java
7
7
  authors:
8
8
  - Sergey Avseyev
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-29 00:00:00.000000000 Z
12
+ date: 2013-01-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler