gson 0.5.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/.gitignore +5 -0
  2. data/.rbenv-version +1 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +201 -0
  5. data/README.md +104 -0
  6. data/Rakefile +22 -0
  7. data/benchmark/README.markdown +28 -0
  8. data/benchmark/benchmark.rb +97 -0
  9. data/benchmark/profile.rb +46 -0
  10. data/benchmark/subjects/item.json +1 -0
  11. data/benchmark/subjects/ohai.json +1216 -0
  12. data/benchmark/subjects/twitter_search.json +1 -0
  13. data/benchmark/subjects/twitter_stream.json +430 -0
  14. data/benchmark/subjects/unicode.json +1 -0
  15. data/ext/gson_ext/Decoder.java +222 -0
  16. data/ext/gson_ext/Encoder.java +181 -0
  17. data/ext/gson_ext/GsonExtService.java +50 -0
  18. data/gson.gemspec +38 -0
  19. data/lib/gson-2.2.2.jar +0 -0
  20. data/lib/gson.rb +33 -0
  21. data/lib/gson/version.rb +20 -0
  22. data/tasks/compile.rake +33 -0
  23. data/tasks/test.rake +25 -0
  24. data/tasks/util.rake +21 -0
  25. data/test/okjson/README.markdown +18 -0
  26. data/test/okjson/t/decode-bignum-e.json +1 -0
  27. data/test/okjson/t/decode-bignum-e.json.exp +1 -0
  28. data/test/okjson/t/decode-bignum.json +1 -0
  29. data/test/okjson/t/decode-bignum.json.exp +1 -0
  30. data/test/okjson/t/decode-invalid15.json +1 -0
  31. data/test/okjson/t/decode-invalid15.json.exp +1 -0
  32. data/test/okjson/t/decode-invalid16.json +1 -0
  33. data/test/okjson/t/decode-invalid16.json.exp +1 -0
  34. data/test/okjson/t/decode-invalid17.json +1 -0
  35. data/test/okjson/t/decode-invalid17.json.exp +1 -0
  36. data/test/okjson/t/decode-invalid25.json +1 -0
  37. data/test/okjson/t/decode-invalid25.json.exp +1 -0
  38. data/test/okjson/t/decode-invalid26.json +1 -0
  39. data/test/okjson/t/decode-invalid26.json.exp +1 -0
  40. data/test/okjson/t/decode-invalid27.json +2 -0
  41. data/test/okjson/t/decode-invalid27.json.exp +1 -0
  42. data/test/okjson/t/decode-invalid28.json +2 -0
  43. data/test/okjson/t/decode-invalid28.json.exp +1 -0
  44. data/test/okjson/t/encode-badutf8.rb +1 -0
  45. data/test/okjson/t/encode-badutf8.rb.exp +1 -0
  46. data/test/okjson/t/encode-bignum.json +1 -0
  47. data/test/okjson/t/encode-bignum.json.exp +1 -0
  48. data/test/okjson/t/encode-int.rb +1 -0
  49. data/test/okjson/t/encode-int.rb.exp +1 -0
  50. data/test/okjson/t/encode-obj.rb +1 -0
  51. data/test/okjson/t/encode-obj.rb.exp +1 -0
  52. data/test/okjson/t/encode-utf8.rb +1 -0
  53. data/test/okjson/t/encode-utf8.rb.exp +1 -0
  54. data/test/okjson/t/err-key.rb +1 -0
  55. data/test/okjson/t/err-naninf.rb +1 -0
  56. data/test/okjson/t/err-other.rb +1 -0
  57. data/test/okjson/t/err-root.rb +1 -0
  58. data/test/okjson/t/err-symbol.rb +1 -0
  59. data/test/okjson/t/invalid-key.json +1 -0
  60. data/test/okjson/t/invalid-uescape.json +1 -0
  61. data/test/okjson/t/invalid1.json +1 -0
  62. data/test/okjson/t/invalid10.json +1 -0
  63. data/test/okjson/t/invalid11.json +1 -0
  64. data/test/okjson/t/invalid12.json +1 -0
  65. data/test/okjson/t/invalid13.json +1 -0
  66. data/test/okjson/t/invalid14.json +1 -0
  67. data/test/okjson/t/invalid19.json +1 -0
  68. data/test/okjson/t/invalid2.json +1 -0
  69. data/test/okjson/t/invalid20.json +1 -0
  70. data/test/okjson/t/invalid21.json +1 -0
  71. data/test/okjson/t/invalid22.json +1 -0
  72. data/test/okjson/t/invalid23.json +1 -0
  73. data/test/okjson/t/invalid24.json +1 -0
  74. data/test/okjson/t/invalid3.json +1 -0
  75. data/test/okjson/t/invalid4.json +1 -0
  76. data/test/okjson/t/invalid5.json +1 -0
  77. data/test/okjson/t/invalid6.json +1 -0
  78. data/test/okjson/t/invalid7.json +1 -0
  79. data/test/okjson/t/invalid8.json +1 -0
  80. data/test/okjson/t/invalid9.json +1 -0
  81. data/test/okjson/t/valid-0.json +1 -0
  82. data/test/okjson/t/valid-0.json.exp +1 -0
  83. data/test/okjson/t/valid-1.json +1 -0
  84. data/test/okjson/t/valid-1.json.exp +1 -0
  85. data/test/okjson/t/valid-alnum.json +1 -0
  86. data/test/okjson/t/valid-alnum.json.exp +1 -0
  87. data/test/okjson/t/valid-array-deep.json +1 -0
  88. data/test/okjson/t/valid-array-deep.json.exp +1 -0
  89. data/test/okjson/t/valid-array-empty.json +1 -0
  90. data/test/okjson/t/valid-array-empty.json.exp +1 -0
  91. data/test/okjson/t/valid-array-single.json +1 -0
  92. data/test/okjson/t/valid-array-single.json.exp +1 -0
  93. data/test/okjson/t/valid-backslash.json +1 -0
  94. data/test/okjson/t/valid-backslash.json.exp +1 -0
  95. data/test/okjson/t/valid-comment.json +1 -0
  96. data/test/okjson/t/valid-comment.json.exp +1 -0
  97. data/test/okjson/t/valid-comment2.json +1 -0
  98. data/test/okjson/t/valid-comment2.json.exp +1 -0
  99. data/test/okjson/t/valid-compact.json +1 -0
  100. data/test/okjson/t/valid-compact.json.exp +1 -0
  101. data/test/okjson/t/valid-escape.json +1 -0
  102. data/test/okjson/t/valid-escape.json.exp +1 -0
  103. data/test/okjson/t/valid-false.json +1 -0
  104. data/test/okjson/t/valid-false.json.exp +1 -0
  105. data/test/okjson/t/valid-frac-E-minus.json +1 -0
  106. data/test/okjson/t/valid-frac-E-minus.json.exp +1 -0
  107. data/test/okjson/t/valid-frac-E-plus.json +1 -0
  108. data/test/okjson/t/valid-frac-E-plus.json.exp +1 -0
  109. data/test/okjson/t/valid-frac.json +1 -0
  110. data/test/okjson/t/valid-frac.json.exp +1 -0
  111. data/test/okjson/t/valid-hex.json +1 -0
  112. data/test/okjson/t/valid-hex.json.exp +1 -0
  113. data/test/okjson/t/valid-integer.json +1 -0
  114. data/test/okjson/t/valid-integer.json.exp +1 -0
  115. data/test/okjson/t/valid-json.json +1 -0
  116. data/test/okjson/t/valid-json.json.exp +1 -0
  117. data/test/okjson/t/valid-key.json +1 -0
  118. data/test/okjson/t/valid-key.json.exp +1 -0
  119. data/test/okjson/t/valid-null.json +1 -0
  120. data/test/okjson/t/valid-null.json.exp +1 -0
  121. data/test/okjson/t/valid-object-empty.json +1 -0
  122. data/test/okjson/t/valid-object-empty.json.exp +1 -0
  123. data/test/okjson/t/valid-object-single.json +1 -0
  124. data/test/okjson/t/valid-object-single.json.exp +1 -0
  125. data/test/okjson/t/valid-punc.json +1 -0
  126. data/test/okjson/t/valid-punc.json.exp +1 -0
  127. data/test/okjson/t/valid-quote.json +1 -0
  128. data/test/okjson/t/valid-quote.json.exp +1 -0
  129. data/test/okjson/t/valid-quotes.json +1 -0
  130. data/test/okjson/t/valid-quotes.json.exp +1 -0
  131. data/test/okjson/t/valid-slash.json +1 -0
  132. data/test/okjson/t/valid-slash.json.exp +1 -0
  133. data/test/okjson/t/valid-space.json +1 -0
  134. data/test/okjson/t/valid-space.json.exp +1 -0
  135. data/test/okjson/t/valid-spaced.json +5 -0
  136. data/test/okjson/t/valid-spaced.json.exp +1 -0
  137. data/test/okjson/t/valid-spaced2.json +13 -0
  138. data/test/okjson/t/valid-spaced2.json.exp +1 -0
  139. data/test/okjson/t/valid-true.json +1 -0
  140. data/test/okjson/t/valid-true.json.exp +1 -0
  141. data/test/okjson/t/valid-url.json +1 -0
  142. data/test/okjson/t/valid-url.json.exp +1 -0
  143. data/test/test_decoder.rb +88 -0
  144. data/test/test_encoder.rb +102 -0
  145. data/test/test_okjson.rb +57 -0
  146. 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,2 @@
1
+ ["line
2
+ break"]
@@ -0,0 +1 @@
1
+ ["line\nbreak"]
@@ -0,0 +1,2 @@
1
+ ["line\
2
+ break"]
@@ -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
+ ["// /* <!-- --"]
@@ -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
+ {"a":"b"}
@@ -0,0 +1 @@
1
+ ["`1~!@#$%^&*()_+-={':[,]}|;.</>?"]
@@ -0,0 +1 @@
1
+ ["`1~!@#$%^&*()_+-={':[,]}|;.</>?"]
@@ -0,0 +1 @@
1
+ ["\""]
@@ -0,0 +1 @@
1
+ ["\""]
@@ -0,0 +1 @@
1
+ ["&#34; \u0022 %22 0x22 034 &#x22;"]
@@ -0,0 +1 @@
1
+ ["&#34; \" %22 0x22 034 &#x22;"]
@@ -0,0 +1 @@
1
+ ["\/"]
@@ -0,0 +1 @@
1
+ ["/"]
@@ -0,0 +1 @@
1
+ [" "]
@@ -0,0 +1 @@
1
+ [" "]
@@ -0,0 +1,5 @@
1
+ {" s p a c e d " :[1,2 , 3
2
+
3
+ ,
4
+
5
+ 4 , 5 , 6 ,7 ]}
@@ -0,0 +1 @@
1
+ {" s p a c e d ":[1,2,3,4,5,6,7]}
@@ -0,0 +1,13 @@
1
+ [{"a"
2
+ :"b"}
3
+
4
+ ,
5
+ 0.5 ,98.6
6
+ ,
7
+ 99.44
8
+ ,
9
+
10
+ 1066
11
+
12
+
13
+ ,"rosebud"]
@@ -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