JsonSliceUtil { public static String slicesToString(List<JsonSlice> slices) throws JsonFormatException, IOException { if (slices == null || slices.size() == 0) { throw new JsonFormatException("slices is null or empty."); } StringWriter writer = new StringWriter(); int cnt = slices.size(); for (int i = 0; i < cnt; i++) { JsonSlice slice = slices.get(i); if (slice == null) { throw new JsonFormatException("slice is null. i=" + i); } switch (slice.getState()) { case START_ARRAY: i = slicesToStringInArray(slices, i, writer); break; case START_HASH: i = slicesToStringInHash(slices, i, writer); break; default: throw new JsonFormatException("invalid state. i=" + i + " is " + slice.getState()); } } return writer.toString(); }  static String slicesToString(List<JsonSlice> slices);  }
@Test(expected = JsonFormatException.class) public void slicesToString_null() throws JsonFormatException, IOException { JsonSliceUtil.slicesToString(null); }
@Test(expected = JsonFormatException.class) public void slicesToString_empty() throws JsonFormatException, IOException { JsonSliceUtil.slicesToString(new ArrayList<JsonSlice>()); }
@Test public void slicesToString_array() throws JsonFormatException, IOException { List<JsonSlice> slices; String json; slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.END_ARRAY)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("[]")); slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.END_HASH)); slices.add(new JsonSlice(State.END_ARRAY)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("[{}]")); slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.KEY, "hoge")); slices.add(new JsonSlice(State.VALUE_BOOLEAN, true)); slices.add(new JsonSlice(State.END_HASH)); slices.add(new JsonSlice(State.END_ARRAY)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("[{\"hoge\":true}]")); slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.KEY, "hoge")); slices.add(new JsonSlice(State.VALUE_BOOLEAN, true)); slices.add(new JsonSlice(State.KEY, "fuga")); slices.add(new JsonSlice(State.VALUE_NULL)); slices.add(new JsonSlice(State.END_HASH)); slices.add(new JsonSlice(State.END_ARRAY)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("[{\"hoge\":true,\"fuga\":null}]")); slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.KEY, "hoge")); slices.add(new JsonSlice(State.VALUE_BOOLEAN, true)); slices.add(new JsonSlice(State.KEY, "fuga")); slices.add(new JsonSlice(State.VALUE_NULL)); slices.add(new JsonSlice(State.END_HASH)); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.END_ARRAY)); slices.add(new JsonSlice(State.END_ARRAY)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("[{\"hoge\":true,\"fuga\":null},[]]")); slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.VALUE_BOOLEAN, true)); slices.add(new JsonSlice(State.VALUE_LONG, 1)); slices.add(new JsonSlice(State.VALUE_DOUBLE, 2.2)); slices.add(new JsonSlice(State.VALUE_STRING, "str")); slices.add(new JsonSlice(State.VALUE_NULL)); slices.add(new JsonSlice(State.END_ARRAY)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("[true,1,2.2,\"str\",null]")); }
@Test public void slicesToString_hash() throws JsonFormatException, IOException { List<JsonSlice> slices; String json; slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.END_HASH)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("{}")); slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.KEY, "hoge")); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.END_ARRAY)); slices.add(new JsonSlice(State.END_HASH)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("{\"hoge\":[]}")); slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.KEY, "hoge")); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.END_ARRAY)); slices.add(new JsonSlice(State.KEY, "fuga")); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.VALUE_BOOLEAN, false)); slices.add(new JsonSlice(State.END_ARRAY)); slices.add(new JsonSlice(State.END_HASH)); json = JsonSliceUtil.slicesToString(slices); assertThat(json, is("{\"hoge\":[],\"fuga\":[false]}")); slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.KEY, "ary")); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.END_ARRAY)); slices.add(new JsonSlice(State.KEY, "hash")); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.END_HASH)); slices.add(new JsonSlice(State.KEY, "b")); slices.add(new JsonSlice(State.VALUE_BOOLEAN, true)); slices.add(new JsonSlice(State.KEY, "l")); slices.add(new JsonSlice(State.VALUE_LONG, 1)); slices.add(new JsonSlice(State.KEY, "d")); slices.add(new JsonSlice(State.VALUE_DOUBLE, 2.2)); slices.add(new JsonSlice(State.KEY, "str")); slices.add(new JsonSlice(State.VALUE_STRING, "str")); slices.add(new JsonSlice(State.KEY, "null")); slices.add(new JsonSlice(State.VALUE_NULL)); slices.add(new JsonSlice(State.END_HASH)); json = JsonSliceUtil.slicesToString(slices); assertThat( json, is("{\"ary\":[],\"hash\":{},\"b\":true,\"l\":1,\"d\":2.2,\"str\":\"str\",\"null\":null}")); }
@Test(expected = JsonFormatException.class) public void slicesToString_invalid1() throws JsonFormatException, IOException { List<JsonSlice> slices; slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_HASH)); slices.add(new JsonSlice(State.START_ARRAY)); slices.add(new JsonSlice(State.END_ARRAY)); slices.add(new JsonSlice(State.END_HASH)); JsonSliceUtil.slicesToString(slices); }
@Test(expected = JsonFormatException.class) public void slicesToString_invalid2() throws JsonFormatException, IOException { List<JsonSlice> slices; slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_HASH)); JsonSliceUtil.slicesToString(slices); }
@Test(expected = JsonFormatException.class) public void slicesToString_invalid3() throws JsonFormatException, IOException { List<JsonSlice> slices; slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.START_ARRAY)); JsonSliceUtil.slicesToString(slices); }
@Test(expected = JsonFormatException.class) public void slicesToString_invalid4() throws JsonFormatException, IOException { List<JsonSlice> slices; slices = new ArrayList<JsonSlice>(); slices.add(new JsonSlice(State.VALUE_NULL)); JsonSliceUtil.slicesToString(slices); }