gson 0.5.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rbenv-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE +201 -0
- data/README.md +104 -0
- data/Rakefile +22 -0
- data/benchmark/README.markdown +28 -0
- data/benchmark/benchmark.rb +97 -0
- data/benchmark/profile.rb +46 -0
- data/benchmark/subjects/item.json +1 -0
- data/benchmark/subjects/ohai.json +1216 -0
- data/benchmark/subjects/twitter_search.json +1 -0
- data/benchmark/subjects/twitter_stream.json +430 -0
- data/benchmark/subjects/unicode.json +1 -0
- data/ext/gson_ext/Decoder.java +222 -0
- data/ext/gson_ext/Encoder.java +181 -0
- data/ext/gson_ext/GsonExtService.java +50 -0
- data/gson.gemspec +38 -0
- data/lib/gson-2.2.2.jar +0 -0
- data/lib/gson.rb +33 -0
- data/lib/gson/version.rb +20 -0
- data/tasks/compile.rake +33 -0
- data/tasks/test.rake +25 -0
- data/tasks/util.rake +21 -0
- data/test/okjson/README.markdown +18 -0
- data/test/okjson/t/decode-bignum-e.json +1 -0
- data/test/okjson/t/decode-bignum-e.json.exp +1 -0
- data/test/okjson/t/decode-bignum.json +1 -0
- data/test/okjson/t/decode-bignum.json.exp +1 -0
- data/test/okjson/t/decode-invalid15.json +1 -0
- data/test/okjson/t/decode-invalid15.json.exp +1 -0
- data/test/okjson/t/decode-invalid16.json +1 -0
- data/test/okjson/t/decode-invalid16.json.exp +1 -0
- data/test/okjson/t/decode-invalid17.json +1 -0
- data/test/okjson/t/decode-invalid17.json.exp +1 -0
- data/test/okjson/t/decode-invalid25.json +1 -0
- data/test/okjson/t/decode-invalid25.json.exp +1 -0
- data/test/okjson/t/decode-invalid26.json +1 -0
- data/test/okjson/t/decode-invalid26.json.exp +1 -0
- data/test/okjson/t/decode-invalid27.json +2 -0
- data/test/okjson/t/decode-invalid27.json.exp +1 -0
- data/test/okjson/t/decode-invalid28.json +2 -0
- data/test/okjson/t/decode-invalid28.json.exp +1 -0
- data/test/okjson/t/encode-badutf8.rb +1 -0
- data/test/okjson/t/encode-badutf8.rb.exp +1 -0
- data/test/okjson/t/encode-bignum.json +1 -0
- data/test/okjson/t/encode-bignum.json.exp +1 -0
- data/test/okjson/t/encode-int.rb +1 -0
- data/test/okjson/t/encode-int.rb.exp +1 -0
- data/test/okjson/t/encode-obj.rb +1 -0
- data/test/okjson/t/encode-obj.rb.exp +1 -0
- data/test/okjson/t/encode-utf8.rb +1 -0
- data/test/okjson/t/encode-utf8.rb.exp +1 -0
- data/test/okjson/t/err-key.rb +1 -0
- data/test/okjson/t/err-naninf.rb +1 -0
- data/test/okjson/t/err-other.rb +1 -0
- data/test/okjson/t/err-root.rb +1 -0
- data/test/okjson/t/err-symbol.rb +1 -0
- data/test/okjson/t/invalid-key.json +1 -0
- data/test/okjson/t/invalid-uescape.json +1 -0
- data/test/okjson/t/invalid1.json +1 -0
- data/test/okjson/t/invalid10.json +1 -0
- data/test/okjson/t/invalid11.json +1 -0
- data/test/okjson/t/invalid12.json +1 -0
- data/test/okjson/t/invalid13.json +1 -0
- data/test/okjson/t/invalid14.json +1 -0
- data/test/okjson/t/invalid19.json +1 -0
- data/test/okjson/t/invalid2.json +1 -0
- data/test/okjson/t/invalid20.json +1 -0
- data/test/okjson/t/invalid21.json +1 -0
- data/test/okjson/t/invalid22.json +1 -0
- data/test/okjson/t/invalid23.json +1 -0
- data/test/okjson/t/invalid24.json +1 -0
- data/test/okjson/t/invalid3.json +1 -0
- data/test/okjson/t/invalid4.json +1 -0
- data/test/okjson/t/invalid5.json +1 -0
- data/test/okjson/t/invalid6.json +1 -0
- data/test/okjson/t/invalid7.json +1 -0
- data/test/okjson/t/invalid8.json +1 -0
- data/test/okjson/t/invalid9.json +1 -0
- data/test/okjson/t/valid-0.json +1 -0
- data/test/okjson/t/valid-0.json.exp +1 -0
- data/test/okjson/t/valid-1.json +1 -0
- data/test/okjson/t/valid-1.json.exp +1 -0
- data/test/okjson/t/valid-alnum.json +1 -0
- data/test/okjson/t/valid-alnum.json.exp +1 -0
- data/test/okjson/t/valid-array-deep.json +1 -0
- data/test/okjson/t/valid-array-deep.json.exp +1 -0
- data/test/okjson/t/valid-array-empty.json +1 -0
- data/test/okjson/t/valid-array-empty.json.exp +1 -0
- data/test/okjson/t/valid-array-single.json +1 -0
- data/test/okjson/t/valid-array-single.json.exp +1 -0
- data/test/okjson/t/valid-backslash.json +1 -0
- data/test/okjson/t/valid-backslash.json.exp +1 -0
- data/test/okjson/t/valid-comment.json +1 -0
- data/test/okjson/t/valid-comment.json.exp +1 -0
- data/test/okjson/t/valid-comment2.json +1 -0
- data/test/okjson/t/valid-comment2.json.exp +1 -0
- data/test/okjson/t/valid-compact.json +1 -0
- data/test/okjson/t/valid-compact.json.exp +1 -0
- data/test/okjson/t/valid-escape.json +1 -0
- data/test/okjson/t/valid-escape.json.exp +1 -0
- data/test/okjson/t/valid-false.json +1 -0
- data/test/okjson/t/valid-false.json.exp +1 -0
- data/test/okjson/t/valid-frac-E-minus.json +1 -0
- data/test/okjson/t/valid-frac-E-minus.json.exp +1 -0
- data/test/okjson/t/valid-frac-E-plus.json +1 -0
- data/test/okjson/t/valid-frac-E-plus.json.exp +1 -0
- data/test/okjson/t/valid-frac.json +1 -0
- data/test/okjson/t/valid-frac.json.exp +1 -0
- data/test/okjson/t/valid-hex.json +1 -0
- data/test/okjson/t/valid-hex.json.exp +1 -0
- data/test/okjson/t/valid-integer.json +1 -0
- data/test/okjson/t/valid-integer.json.exp +1 -0
- data/test/okjson/t/valid-json.json +1 -0
- data/test/okjson/t/valid-json.json.exp +1 -0
- data/test/okjson/t/valid-key.json +1 -0
- data/test/okjson/t/valid-key.json.exp +1 -0
- data/test/okjson/t/valid-null.json +1 -0
- data/test/okjson/t/valid-null.json.exp +1 -0
- data/test/okjson/t/valid-object-empty.json +1 -0
- data/test/okjson/t/valid-object-empty.json.exp +1 -0
- data/test/okjson/t/valid-object-single.json +1 -0
- data/test/okjson/t/valid-object-single.json.exp +1 -0
- data/test/okjson/t/valid-punc.json +1 -0
- data/test/okjson/t/valid-punc.json.exp +1 -0
- data/test/okjson/t/valid-quote.json +1 -0
- data/test/okjson/t/valid-quote.json.exp +1 -0
- data/test/okjson/t/valid-quotes.json +1 -0
- data/test/okjson/t/valid-quotes.json.exp +1 -0
- data/test/okjson/t/valid-slash.json +1 -0
- data/test/okjson/t/valid-slash.json.exp +1 -0
- data/test/okjson/t/valid-space.json +1 -0
- data/test/okjson/t/valid-space.json.exp +1 -0
- data/test/okjson/t/valid-spaced.json +5 -0
- data/test/okjson/t/valid-spaced.json.exp +1 -0
- data/test/okjson/t/valid-spaced2.json +13 -0
- data/test/okjson/t/valid-spaced2.json.exp +1 -0
- data/test/okjson/t/valid-true.json +1 -0
- data/test/okjson/t/valid-true.json.exp +1 -0
- data/test/okjson/t/valid-url.json +1 -0
- data/test/okjson/t/valid-url.json.exp +1 -0
- data/test/test_decoder.rb +88 -0
- data/test/test_encoder.rb +102 -0
- data/test/test_okjson.rb +57 -0
- metadata +329 -0
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: x15"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: \'"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: '"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: \017"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: 017"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["tab character in string "]
|
@@ -0,0 +1 @@
|
|
1
|
+
["tab\tcharacter\tin\tstring\t"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["tab\ character\ in\ string\ "]
|
@@ -0,0 +1 @@
|
|
1
|
+
["tab\tcharacter\tin\tstring\t"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["line\nbreak"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["line\nbreak"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\x82\xAC\xEF"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["���"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[23456789012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]
|
@@ -0,0 +1 @@
|
|
1
|
+
[23456789012000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]
|
@@ -0,0 +1 @@
|
|
1
|
+
[123]
|
@@ -0,0 +1 @@
|
|
1
|
+
[123]
|
@@ -0,0 +1 @@
|
|
1
|
+
{"a" => 1}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"a":1}
|
@@ -0,0 +1 @@
|
|
1
|
+
["á$¢€園𝄞"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["á$¢€園𝄞"]
|
@@ -0,0 +1 @@
|
|
1
|
+
{1 => 1}
|
@@ -0,0 +1 @@
|
|
1
|
+
[1.0/0,0.0/0,-1.0/0]
|
@@ -0,0 +1 @@
|
|
1
|
+
[//]
|
@@ -0,0 +1 @@
|
|
1
|
+
"a"
|
@@ -0,0 +1 @@
|
|
1
|
+
[:s]
|
@@ -0,0 +1 @@
|
|
1
|
+
{3: "invalid key"}
|
@@ -0,0 +1 @@
|
|
1
|
+
["\u", "\u1", "\u01", "\u001", "\u0001", "\uxxxx"]
|
@@ -0,0 +1 @@
|
|
1
|
+
"A JSON payload should be an object or array, not a string."
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Extra value after close": true} "misplaced quoted value"
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Illegal expression": 1 + 2}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Illegal invocation": alert()}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Numbers cannot have leading zeroes": 013}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Numbers cannot be hex": 0x14}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Missing colon" null}
|
@@ -0,0 +1 @@
|
|
1
|
+
["Unclosed array"
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Double colon":: null}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Comma instead of colon", null}
|
@@ -0,0 +1 @@
|
|
1
|
+
["Colon instead of comma": false]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Bad value", truth]
|
@@ -0,0 +1 @@
|
|
1
|
+
['single quote']
|
@@ -0,0 +1 @@
|
|
1
|
+
{unquoted_key: "keys must be quoted"}
|
@@ -0,0 +1 @@
|
|
1
|
+
["extra comma",]
|
@@ -0,0 +1 @@
|
|
1
|
+
["double extra comma",,]
|
@@ -0,0 +1 @@
|
|
1
|
+
[ , "<-- missing value"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Comma after the close"],
|
@@ -0,0 +1 @@
|
|
1
|
+
["Extra close"]]
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Extra comma": true,}
|
@@ -0,0 +1 @@
|
|
1
|
+
[0]
|
@@ -0,0 +1 @@
|
|
1
|
+
[0]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1]
|
@@ -0,0 +1 @@
|
|
1
|
+
["abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWYZ0123456789"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWYZ0123456789"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
@@ -0,0 +1 @@
|
|
1
|
+
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
@@ -0,0 +1 @@
|
|
1
|
+
[]
|
@@ -0,0 +1 @@
|
|
1
|
+
[]
|
@@ -0,0 +1 @@
|
|
1
|
+
["a"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["a"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\\"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\\"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["// /* <!-- --"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["// /* <!-- --"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["# -- --> */"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["# -- --> */"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1,2,3,4,5,6,7]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1,2,3,4,5,6,7]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\b\f\n\r\t"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\b\f\n\r\t"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[false]
|
@@ -0,0 +1 @@
|
|
1
|
+
[false]
|
@@ -0,0 +1 @@
|
|
1
|
+
[0.123456789E-12]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1.23456789E-13]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1.23456789E34]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1.23456789E34]
|
@@ -0,0 +1 @@
|
|
1
|
+
[-9876.543210]
|
@@ -0,0 +1 @@
|
|
1
|
+
[-9876.54321]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\u0024\u00a2\u20ac\u5712\uD834\uDD1E"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["$¢€園𝄞"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1234567890]
|
@@ -0,0 +1 @@
|
|
1
|
+
[1234567890]
|
@@ -0,0 +1 @@
|
|
1
|
+
["{\"object with 1 member\":[\"array with 1 element\"]}"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["{\"object with 1 member\":[\"array with 1 element\"]}"]
|
@@ -0,0 +1 @@
|
|
1
|
+
{"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?":"A key can be any string"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"/\\\"쫾몾ꮘﳞ볚\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?":"A key can be any string"}
|
@@ -0,0 +1 @@
|
|
1
|
+
[null]
|
@@ -0,0 +1 @@
|
|
1
|
+
[null]
|
@@ -0,0 +1 @@
|
|
1
|
+
{}
|
@@ -0,0 +1 @@
|
|
1
|
+
{}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"a":"b"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"a":"b"}
|
@@ -0,0 +1 @@
|
|
1
|
+
["`1~!@#$%^&*()_+-={':[,]}|;.</>?"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["`1~!@#$%^&*()_+-={':[,]}|;.</>?"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\""]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\""]
|
@@ -0,0 +1 @@
|
|
1
|
+
["" \u0022 %22 0x22 034 ""]
|
@@ -0,0 +1 @@
|
|
1
|
+
["" \" %22 0x22 034 ""]
|
@@ -0,0 +1 @@
|
|
1
|
+
["\/"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["/"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[" "]
|
@@ -0,0 +1 @@
|
|
1
|
+
[" "]
|
@@ -0,0 +1 @@
|
|
1
|
+
{" s p a c e d ":[1,2,3,4,5,6,7]}
|
@@ -0,0 +1 @@
|
|
1
|
+
[{"a":"b"},0.5,98.6,99.44,1066,"rosebud"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[true]
|
@@ -0,0 +1 @@
|
|
1
|
+
[true]
|
@@ -0,0 +1 @@
|
|
1
|
+
["http://www.JSON.org/"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["http://www.JSON.org/"]
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'gson'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class TestDecoder < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
def test_it_lenient_by_default
|
8
|
+
decoder = Gson::Decoder.new
|
9
|
+
assert decoder.lenient?
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_it_doesnt_symbolize_keys_by_default
|
13
|
+
decoder = Gson::Decoder.new
|
14
|
+
refute decoder.symbolize_keys?
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_it_symbolizes_keys_if_required
|
18
|
+
decoder = Gson::Decoder.new(:symbolize_keys => true)
|
19
|
+
expected = {:foo => "bar"}
|
20
|
+
assert_equal expected, decoder.decode('{"foo":"bar"}')
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_it_accepts_io_objects
|
24
|
+
path = File.expand_path("../okjson/t/valid-object-single.json", __FILE__)
|
25
|
+
File.open(path) do |io|
|
26
|
+
decoder = Gson::Decoder.new
|
27
|
+
expected = {"a" => "b"}
|
28
|
+
assert_equal expected, decoder.decode(io)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_it_accepts_stringio_objects
|
33
|
+
io = StringIO.new('{"a":"b"}')
|
34
|
+
decoder = Gson::Decoder.new
|
35
|
+
expected = {"a" => "b"}
|
36
|
+
assert_equal expected, decoder.decode(io)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_in_lenient_mode_it_allows_top_level_value_of_any_type
|
40
|
+
assert_valid_in_lenient_mode(1, "1")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_in_lenient_mode_it_allows_source_to_start_with_non_execute_prefix
|
44
|
+
nep = %Q|)]}'\n|
|
45
|
+
assert_valid_in_lenient_mode({"foo"=>"bar"}, nep + '{"foo":"bar"}')
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_in_lenient_mode_it_allows_source_to_include_multiple_top_level_values
|
49
|
+
assert_valid_in_lenient_mode([1, 2, 3], '1 2 3')
|
50
|
+
assert_valid_in_lenient_mode([{"foo"=>"bar"}, {"bar"=>"foo"}],
|
51
|
+
'{"foo":"bar"}{"bar":"foo"}')
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_in_lenient_mode_it_allows_names_to_be_unquoted
|
55
|
+
assert_valid_in_lenient_mode({"foo"=>"bar"}, '{foo:"bar"}')
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_in_lenient_mode_it_allows_names_to_be_single_quoted
|
59
|
+
assert_valid_in_lenient_mode({"foo"=>"bar"}, %Q({'foo':"bar"}))
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_in_lenient_mode_it_allows_strings_to_be_unquoted
|
63
|
+
assert_valid_in_lenient_mode({"foo"=>"bar"}, '{"foo":bar}')
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_in_lenient_mode_it_allows_strings_to_be_single_quoted
|
67
|
+
assert_valid_in_lenient_mode({"foo"=>"bar"}, %Q({"foo":'bar'}))
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_in_lenient_mode_it_allows_semicolons_as_array_separators
|
71
|
+
assert_valid_in_lenient_mode({"foo"=>[1,2,3,4]}, '{"foo":[1;2;3;4]}')
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_in_lenient_mode_it_allows_hashrockets_as_name_value_separators
|
75
|
+
assert_valid_in_lenient_mode({"foo" => "bar"}, '{"foo"=>"bar"}')
|
76
|
+
end
|
77
|
+
|
78
|
+
def assert_valid_in_lenient_mode(expected, source)
|
79
|
+
decoder = Gson::Decoder.new(:lenient => true)
|
80
|
+
assert_equal expected, decoder.decode(source)
|
81
|
+
|
82
|
+
decoder = Gson::Decoder.new(:lenient => false)
|
83
|
+
assert_raises Gson::DecodeError do
|
84
|
+
decoder.decode(source)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'gson'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class TestEncoder < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
def test_it_lenient_by_default
|
8
|
+
encoder = Gson::Encoder.new
|
9
|
+
assert encoder.lenient?
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_it_serializes_nils_by_default
|
13
|
+
encoder = Gson::Encoder.new
|
14
|
+
assert encoder.serialize_nils?
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_it_isnt_html_safe_by_default
|
18
|
+
encoder = Gson::Encoder.new
|
19
|
+
refute encoder.html_safe?
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_it_doesnt_prettify_output_by_default
|
23
|
+
encoder = Gson::Encoder.new
|
24
|
+
assert_equal "", encoder.indent
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_it_generates_html_safe_content_if_needed
|
28
|
+
source = {:avatar => '<img src="http://example.com/avatar.png">'}
|
29
|
+
|
30
|
+
encoder = Gson::Encoder.new(:html_safe => true)
|
31
|
+
expected = '{"avatar":"\u003cimg src\u003d\"http://example.com/avatar.png\"\u003e"}'
|
32
|
+
assert_equal expected, encoder.encode(source)
|
33
|
+
|
34
|
+
encoder = Gson::Encoder.new(:html_safe => false)
|
35
|
+
expected = '{"avatar":"<img src=\"http://example.com/avatar.png\">"}'
|
36
|
+
assert_equal expected, encoder.encode(source)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_it_might_be_configured_to_skip_nils
|
40
|
+
source = {"foo" => "bar", "baz" => nil}
|
41
|
+
|
42
|
+
encoder = Gson::Encoder.new(:serialize_nils => true)
|
43
|
+
expected = '{"foo":"bar","baz":null}'
|
44
|
+
assert_equal expected, encoder.encode(source)
|
45
|
+
|
46
|
+
encoder = Gson::Encoder.new(:serialize_nils => false)
|
47
|
+
expected = '{"foo":"bar"}'
|
48
|
+
assert_equal expected, encoder.encode(source)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_it_can_prettify_the_output
|
52
|
+
source = {"foo" => "bar", "ary" => [1, 2, 3]}
|
53
|
+
|
54
|
+
encoder = Gson::Encoder.new(:indent => " ")
|
55
|
+
expected = <<EOJ.chomp
|
56
|
+
{
|
57
|
+
"foo": "bar",
|
58
|
+
"ary": [
|
59
|
+
1,
|
60
|
+
2,
|
61
|
+
3
|
62
|
+
]
|
63
|
+
}
|
64
|
+
EOJ
|
65
|
+
assert_equal expected, encoder.encode(source)
|
66
|
+
|
67
|
+
encoder = Gson::Encoder.new(:indent => nil)
|
68
|
+
expected = '{"foo":"bar","ary":[1,2,3]}'
|
69
|
+
assert_equal expected, encoder.encode(source)
|
70
|
+
|
71
|
+
encoder = Gson::Encoder.new(:indent => "")
|
72
|
+
expected = '{"foo":"bar","ary":[1,2,3]}'
|
73
|
+
assert_equal expected, encoder.encode(source)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_in_lenient_mode_it_allows_primitives_as_top_level_value
|
77
|
+
source = 1
|
78
|
+
|
79
|
+
encoder = Gson::Encoder.new(:lenient => true)
|
80
|
+
expected = '1'
|
81
|
+
assert_equal expected, encoder.encode(source)
|
82
|
+
|
83
|
+
encoder = Gson::Encoder.new(:lenient => false)
|
84
|
+
assert_raises Gson::EncodeError do
|
85
|
+
encoder.encode(source)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_in_lenient_mode_it_allows_nans_to_be_serialized
|
90
|
+
source = {"foo" => 1/0.0}
|
91
|
+
|
92
|
+
encoder = Gson::Encoder.new(:lenient => true)
|
93
|
+
expected = '{"foo":Infinity}'
|
94
|
+
assert_equal expected, encoder.encode(source)
|
95
|
+
|
96
|
+
encoder = Gson::Encoder.new(:lenient => false)
|
97
|
+
assert_raises Gson::EncodeError do
|
98
|
+
encoder.encode(source)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|