JSONValueTest.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package org.json.simple;
  2. import java.io.IOException;
  3. import java.io.StringWriter;
  4. import junit.framework.TestCase;
  5. public class JSONValueTest extends TestCase {
  6. public void testByteArrayToString() throws IOException {
  7. assertEquals("null", JSONValue.toJSONString((byte[])null));
  8. assertEquals("[]", JSONValue.toJSONString(new byte[0]));
  9. assertEquals("[12]", JSONValue.toJSONString(new byte[] { 12 }));
  10. assertEquals("[-7,22,86,-99]", JSONValue.toJSONString(new byte[] { -7, 22, 86, -99 }));
  11. StringWriter writer;
  12. writer = new StringWriter();
  13. JSONValue.writeJSONString((byte[])null, writer);
  14. assertEquals("null", writer.toString());
  15. writer = new StringWriter();
  16. JSONValue.writeJSONString(new byte[0], writer);
  17. assertEquals("[]", writer.toString());
  18. writer = new StringWriter();
  19. JSONValue.writeJSONString(new byte[] { 12 }, writer);
  20. assertEquals("[12]", writer.toString());
  21. writer = new StringWriter();
  22. JSONValue.writeJSONString(new byte[] { -7, 22, 86, -99 }, writer);
  23. assertEquals("[-7,22,86,-99]", writer.toString());
  24. }
  25. public void testShortArrayToString() throws IOException {
  26. assertEquals("null", JSONValue.toJSONString((short[])null));
  27. assertEquals("[]", JSONValue.toJSONString(new short[0]));
  28. assertEquals("[12]", JSONValue.toJSONString(new short[] { 12 }));
  29. assertEquals("[-7,22,86,-99]", JSONValue.toJSONString(new short[] { -7, 22, 86, -99 }));
  30. StringWriter writer;
  31. writer = new StringWriter();
  32. JSONValue.writeJSONString((short[])null, writer);
  33. assertEquals("null", writer.toString());
  34. writer = new StringWriter();
  35. JSONValue.writeJSONString(new short[0], writer);
  36. assertEquals("[]", writer.toString());
  37. writer = new StringWriter();
  38. JSONValue.writeJSONString(new short[] { 12 }, writer);
  39. assertEquals("[12]", writer.toString());
  40. writer = new StringWriter();
  41. JSONValue.writeJSONString(new short[] { -7, 22, 86, -99 }, writer);
  42. assertEquals("[-7,22,86,-99]", writer.toString());
  43. }
  44. public void testIntArrayToString() throws IOException {
  45. assertEquals("null", JSONValue.toJSONString((int[])null));
  46. assertEquals("[]", JSONValue.toJSONString(new int[0]));
  47. assertEquals("[12]", JSONValue.toJSONString(new int[] { 12 }));
  48. assertEquals("[-7,22,86,-99]", JSONValue.toJSONString(new int[] { -7, 22, 86, -99 }));
  49. StringWriter writer;
  50. writer = new StringWriter();
  51. JSONValue.writeJSONString((int[])null, writer);
  52. assertEquals("null", writer.toString());
  53. writer = new StringWriter();
  54. JSONValue.writeJSONString(new int[0], writer);
  55. assertEquals("[]", writer.toString());
  56. writer = new StringWriter();
  57. JSONValue.writeJSONString(new int[] { 12 }, writer);
  58. assertEquals("[12]", writer.toString());
  59. writer = new StringWriter();
  60. JSONValue.writeJSONString(new int[] { -7, 22, 86, -99 }, writer);
  61. assertEquals("[-7,22,86,-99]", writer.toString());
  62. }
  63. public void testLongArrayToString() throws IOException {
  64. assertEquals("null", JSONValue.toJSONString((long[])null));
  65. assertEquals("[]", JSONValue.toJSONString(new long[0]));
  66. assertEquals("[12]", JSONValue.toJSONString(new long[] { 12 }));
  67. assertEquals("[-7,22,9223372036854775807,-99]", JSONValue.toJSONString(new long[] { -7, 22, 9223372036854775807L, -99 }));
  68. StringWriter writer;
  69. writer = new StringWriter();
  70. JSONValue.writeJSONString((long[])null, writer);
  71. assertEquals("null", writer.toString());
  72. writer = new StringWriter();
  73. JSONValue.writeJSONString(new long[0], writer);
  74. assertEquals("[]", writer.toString());
  75. writer = new StringWriter();
  76. JSONValue.writeJSONString(new long[] { 12 }, writer);
  77. assertEquals("[12]", writer.toString());
  78. writer = new StringWriter();
  79. JSONValue.writeJSONString(new long[] { -7, 22, 86, -99 }, writer);
  80. assertEquals("[-7,22,86,-99]", writer.toString());
  81. }
  82. public void testFloatArrayToString() throws IOException {
  83. assertEquals("null", JSONValue.toJSONString((float[])null));
  84. assertEquals("[]", JSONValue.toJSONString(new float[0]));
  85. assertEquals("[12.8]", JSONValue.toJSONString(new float[] { 12.8f }));
  86. assertEquals("[-7.1,22.234,86.7,-99.02]", JSONValue.toJSONString(new float[] { -7.1f, 22.234f, 86.7f, -99.02f }));
  87. StringWriter writer;
  88. writer = new StringWriter();
  89. JSONValue.writeJSONString((float[])null, writer);
  90. assertEquals("null", writer.toString());
  91. writer = new StringWriter();
  92. JSONValue.writeJSONString(new float[0], writer);
  93. assertEquals("[]", writer.toString());
  94. writer = new StringWriter();
  95. JSONValue.writeJSONString(new float[] { 12.8f }, writer);
  96. assertEquals("[12.8]", writer.toString());
  97. writer = new StringWriter();
  98. JSONValue.writeJSONString(new float[] { -7.1f, 22.234f, 86.7f, -99.02f }, writer);
  99. assertEquals("[-7.1,22.234,86.7,-99.02]", writer.toString());
  100. }
  101. public void testDoubleArrayToString() throws IOException {
  102. assertEquals("null", JSONValue.toJSONString((double[])null));
  103. assertEquals("[]", JSONValue.toJSONString(new double[0]));
  104. assertEquals("[12.8]", JSONValue.toJSONString(new double[] { 12.8 }));
  105. assertEquals("[-7.1,22.234,86.7,-99.02]", JSONValue.toJSONString(new double[] { -7.1, 22.234, 86.7, -99.02 }));
  106. StringWriter writer;
  107. writer = new StringWriter();
  108. JSONValue.writeJSONString((double[])null, writer);
  109. assertEquals("null", writer.toString());
  110. writer = new StringWriter();
  111. JSONValue.writeJSONString(new double[0], writer);
  112. assertEquals("[]", writer.toString());
  113. writer = new StringWriter();
  114. JSONValue.writeJSONString(new double[] { 12.8 }, writer);
  115. assertEquals("[12.8]", writer.toString());
  116. writer = new StringWriter();
  117. JSONValue.writeJSONString(new double[] { -7.1, 22.234, 86.7, -99.02 }, writer);
  118. assertEquals("[-7.1,22.234,86.7,-99.02]", writer.toString());
  119. }
  120. public void testBooleanArrayToString() throws IOException {
  121. assertEquals("null", JSONValue.toJSONString((boolean[])null));
  122. assertEquals("[]", JSONValue.toJSONString(new boolean[0]));
  123. assertEquals("[true]", JSONValue.toJSONString(new boolean[] { true }));
  124. assertEquals("[true,false,true]", JSONValue.toJSONString(new boolean[] { true, false, true }));
  125. StringWriter writer;
  126. writer = new StringWriter();
  127. JSONValue.writeJSONString((boolean[])null, writer);
  128. assertEquals("null", writer.toString());
  129. writer = new StringWriter();
  130. JSONValue.writeJSONString(new boolean[0], writer);
  131. assertEquals("[]", writer.toString());
  132. writer = new StringWriter();
  133. JSONValue.writeJSONString(new boolean[] { true }, writer);
  134. assertEquals("[true]", writer.toString());
  135. writer = new StringWriter();
  136. JSONValue.writeJSONString(new boolean[] { true, false, true }, writer);
  137. assertEquals("[true,false,true]", writer.toString());
  138. }
  139. public void testCharArrayToString() throws IOException {
  140. assertEquals("null", JSONValue.toJSONString((char[])null));
  141. assertEquals("[]", JSONValue.toJSONString(new char[0]));
  142. assertEquals("[\"a\"]", JSONValue.toJSONString(new char[] { 'a' }));
  143. assertEquals("[\"a\",\"b\",\"c\"]", JSONValue.toJSONString(new char[] { 'a', 'b', 'c' }));
  144. StringWriter writer;
  145. writer = new StringWriter();
  146. JSONValue.writeJSONString((char[])null, writer);
  147. assertEquals("null", writer.toString());
  148. writer = new StringWriter();
  149. JSONValue.writeJSONString(new char[0], writer);
  150. assertEquals("[]", writer.toString());
  151. writer = new StringWriter();
  152. JSONValue.writeJSONString(new char[] { 'a' }, writer);
  153. assertEquals("[\"a\"]", writer.toString());
  154. writer = new StringWriter();
  155. JSONValue.writeJSONString(new char[] { 'a', 'b', 'c' }, writer);
  156. assertEquals("[\"a\",\"b\",\"c\"]", writer.toString());
  157. }
  158. public void testObjectArrayToString() throws IOException {
  159. assertEquals("null", JSONValue.toJSONString((Object[])null));
  160. assertEquals("[]", JSONValue.toJSONString(new Object[0]));
  161. assertEquals("[\"Hello\"]", JSONValue.toJSONString(new Object[] { "Hello" }));
  162. assertEquals("[\"Hello\",12,[1,2,3]]", JSONValue.toJSONString(new Object[] { "Hello", new Integer(12), new int[] { 1, 2, 3 } }));
  163. StringWriter writer;
  164. writer = new StringWriter();
  165. JSONValue.writeJSONString((Object[])null, writer);
  166. assertEquals("null", writer.toString());
  167. writer = new StringWriter();
  168. JSONValue.writeJSONString(new Object[0], writer);
  169. assertEquals("[]", writer.toString());
  170. writer = new StringWriter();
  171. JSONValue.writeJSONString(new Object[] { "Hello" }, writer);
  172. assertEquals("[\"Hello\"]", writer.toString());
  173. writer = new StringWriter();
  174. JSONValue.writeJSONString(new Object[] { "Hello", new Integer(12), new int[] { 1, 2, 3} }, writer);
  175. assertEquals("[\"Hello\",12,[1,2,3]]", writer.toString());
  176. }
  177. public void testArraysOfArrays() throws IOException {
  178. StringWriter writer;
  179. final int[][][] nestedIntArray = new int[][][]{{{1}, {5}}, {{2}, {6}}};
  180. final String expectedNestedIntString = "[[[1],[5]],[[2],[6]]]";
  181. assertEquals(expectedNestedIntString, JSONValue.toJSONString(nestedIntArray));
  182. writer = new StringWriter();
  183. JSONValue.writeJSONString(nestedIntArray, writer);
  184. assertEquals(expectedNestedIntString, writer.toString());
  185. final String[][] nestedStringArray = new String[][]{{"a", "b"}, {"c", "d"}};
  186. final String expectedNestedStringString = "[[\"a\",\"b\"],[\"c\",\"d\"]]";
  187. assertEquals(expectedNestedStringString, JSONValue.toJSONString(nestedStringArray));
  188. writer = new StringWriter();
  189. JSONValue.writeJSONString(nestedStringArray, writer);
  190. assertEquals(expectedNestedStringString, writer.toString());
  191. }
  192. }