123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- package org.json.simple;
- import java.io.IOException;
- import java.io.StringWriter;
- import junit.framework.TestCase;
- public class JSONValueTest extends TestCase {
- public void testByteArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((byte[])null));
- assertEquals("[]", JSONValue.toJSONString(new byte[0]));
- assertEquals("[12]", JSONValue.toJSONString(new byte[] { 12 }));
- assertEquals("[-7,22,86,-99]", JSONValue.toJSONString(new byte[] { -7, 22, 86, -99 }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((byte[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new byte[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new byte[] { 12 }, writer);
- assertEquals("[12]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new byte[] { -7, 22, 86, -99 }, writer);
- assertEquals("[-7,22,86,-99]", writer.toString());
- }
-
- public void testShortArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((short[])null));
- assertEquals("[]", JSONValue.toJSONString(new short[0]));
- assertEquals("[12]", JSONValue.toJSONString(new short[] { 12 }));
- assertEquals("[-7,22,86,-99]", JSONValue.toJSONString(new short[] { -7, 22, 86, -99 }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((short[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new short[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new short[] { 12 }, writer);
- assertEquals("[12]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new short[] { -7, 22, 86, -99 }, writer);
- assertEquals("[-7,22,86,-99]", writer.toString());
- }
-
- public void testIntArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((int[])null));
- assertEquals("[]", JSONValue.toJSONString(new int[0]));
- assertEquals("[12]", JSONValue.toJSONString(new int[] { 12 }));
- assertEquals("[-7,22,86,-99]", JSONValue.toJSONString(new int[] { -7, 22, 86, -99 }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((int[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new int[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new int[] { 12 }, writer);
- assertEquals("[12]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new int[] { -7, 22, 86, -99 }, writer);
- assertEquals("[-7,22,86,-99]", writer.toString());
- }
-
- public void testLongArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((long[])null));
- assertEquals("[]", JSONValue.toJSONString(new long[0]));
- assertEquals("[12]", JSONValue.toJSONString(new long[] { 12 }));
- assertEquals("[-7,22,9223372036854775807,-99]", JSONValue.toJSONString(new long[] { -7, 22, 9223372036854775807L, -99 }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((long[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new long[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new long[] { 12 }, writer);
- assertEquals("[12]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new long[] { -7, 22, 86, -99 }, writer);
- assertEquals("[-7,22,86,-99]", writer.toString());
- }
-
- public void testFloatArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((float[])null));
- assertEquals("[]", JSONValue.toJSONString(new float[0]));
- assertEquals("[12.8]", JSONValue.toJSONString(new float[] { 12.8f }));
- assertEquals("[-7.1,22.234,86.7,-99.02]", JSONValue.toJSONString(new float[] { -7.1f, 22.234f, 86.7f, -99.02f }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((float[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new float[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new float[] { 12.8f }, writer);
- assertEquals("[12.8]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new float[] { -7.1f, 22.234f, 86.7f, -99.02f }, writer);
- assertEquals("[-7.1,22.234,86.7,-99.02]", writer.toString());
- }
-
- public void testDoubleArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((double[])null));
- assertEquals("[]", JSONValue.toJSONString(new double[0]));
- assertEquals("[12.8]", JSONValue.toJSONString(new double[] { 12.8 }));
- assertEquals("[-7.1,22.234,86.7,-99.02]", JSONValue.toJSONString(new double[] { -7.1, 22.234, 86.7, -99.02 }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((double[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new double[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new double[] { 12.8 }, writer);
- assertEquals("[12.8]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new double[] { -7.1, 22.234, 86.7, -99.02 }, writer);
- assertEquals("[-7.1,22.234,86.7,-99.02]", writer.toString());
- }
-
- public void testBooleanArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((boolean[])null));
- assertEquals("[]", JSONValue.toJSONString(new boolean[0]));
- assertEquals("[true]", JSONValue.toJSONString(new boolean[] { true }));
- assertEquals("[true,false,true]", JSONValue.toJSONString(new boolean[] { true, false, true }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((boolean[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new boolean[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new boolean[] { true }, writer);
- assertEquals("[true]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new boolean[] { true, false, true }, writer);
- assertEquals("[true,false,true]", writer.toString());
- }
-
- public void testCharArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((char[])null));
- assertEquals("[]", JSONValue.toJSONString(new char[0]));
- assertEquals("[\"a\"]", JSONValue.toJSONString(new char[] { 'a' }));
- assertEquals("[\"a\",\"b\",\"c\"]", JSONValue.toJSONString(new char[] { 'a', 'b', 'c' }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((char[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new char[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new char[] { 'a' }, writer);
- assertEquals("[\"a\"]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new char[] { 'a', 'b', 'c' }, writer);
- assertEquals("[\"a\",\"b\",\"c\"]", writer.toString());
- }
-
- public void testObjectArrayToString() throws IOException {
- assertEquals("null", JSONValue.toJSONString((Object[])null));
- assertEquals("[]", JSONValue.toJSONString(new Object[0]));
- assertEquals("[\"Hello\"]", JSONValue.toJSONString(new Object[] { "Hello" }));
- assertEquals("[\"Hello\",12,[1,2,3]]", JSONValue.toJSONString(new Object[] { "Hello", new Integer(12), new int[] { 1, 2, 3 } }));
-
- StringWriter writer;
-
- writer = new StringWriter();
- JSONValue.writeJSONString((Object[])null, writer);
- assertEquals("null", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new Object[0], writer);
- assertEquals("[]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new Object[] { "Hello" }, writer);
- assertEquals("[\"Hello\"]", writer.toString());
-
- writer = new StringWriter();
- JSONValue.writeJSONString(new Object[] { "Hello", new Integer(12), new int[] { 1, 2, 3} }, writer);
- assertEquals("[\"Hello\",12,[1,2,3]]", writer.toString());
- }
-
- public void testArraysOfArrays() throws IOException {
-
- StringWriter writer;
-
- final int[][][] nestedIntArray = new int[][][]{{{1}, {5}}, {{2}, {6}}};
- final String expectedNestedIntString = "[[[1],[5]],[[2],[6]]]";
-
- assertEquals(expectedNestedIntString, JSONValue.toJSONString(nestedIntArray));
-
- writer = new StringWriter();
- JSONValue.writeJSONString(nestedIntArray, writer);
- assertEquals(expectedNestedIntString, writer.toString());
- final String[][] nestedStringArray = new String[][]{{"a", "b"}, {"c", "d"}};
- final String expectedNestedStringString = "[[\"a\",\"b\"],[\"c\",\"d\"]]";
-
- assertEquals(expectedNestedStringString, JSONValue.toJSONString(nestedStringArray));
-
- writer = new StringWriter();
- JSONValue.writeJSONString(nestedStringArray, writer);
- assertEquals(expectedNestedStringString, writer.toString());
- }
- }
|