### Question:
JsonUtils { public static Map<String, Object> toMap(String json) { return getGson().fromJson(json, new TypeToken<HashMap<String, Object>>() { }.getType()); } private  JsonUtils(); static String stripComment(String source); static String toJson(Object source); static Map<String, Object> toMap(String json); static Object[] toArray(String json); static Collection<T> toCollection(String json);  }### Answer:
@Test(expected = JsonParseException.class) public void toMap_Exception_String() { JsonUtils.toMap("name"); }
@Test(expected = JsonParseException.class) public void toMap_Exception_Array() { JsonUtils.toMap("[\"name\"]"); }
@Test public void toMap() { String str = "{\"str\":\"str\",\"int\":1,\"bool\":true}"; Map<String, Object> map = JsonUtils.toMap(str); Assert.assertEquals("str", map.get("str")); Assert.assertEquals(String.class, map.get("str").getClass()); Assert.assertEquals(new Integer(1), map.get("int")); Assert.assertEquals(Integer.class, map.get("int").getClass()); Assert.assertEquals(new Boolean(true), map.get("bool")); Assert.assertEquals(Boolean.class, map.get("bool").getClass()); }
@Test public void toMap_NestedMap() { String str = "{\"str\":\"str\",\"int\":1,\"bool\":true,\"sub\":{\"str\":\"str\",\"int\":1,\"bool\":true}}"; Map<String, Object> map = JsonUtils.toMap(str); Assert.assertEquals("str", map.get("str")); Assert.assertEquals(String.class, map.get("str").getClass()); Assert.assertEquals(new Integer(1), map.get("int")); Assert.assertEquals(Integer.class, map.get("int").getClass()); Assert.assertEquals(new Boolean(true), map.get("bool")); Assert.assertEquals(Boolean.class, map.get("bool").getClass()); Assert.assertTrue(map.get("sub") instanceof Map); @SuppressWarnings({"unchecked"}) Map<String, Object> sub = (Map<String, Object>) map.get("sub"); Assert.assertEquals("str", sub.get("str")); Assert.assertEquals(String.class, sub.get("str").getClass()); Assert.assertEquals(new Integer(1), sub.get("int")); Assert.assertEquals(Integer.class, sub.get("int").getClass()); Assert.assertEquals(new Boolean(true), sub.get("bool")); Assert.assertEquals(Boolean.class, sub.get("bool").getClass()); }
@Test public void toMap_Nested_Array_Simple() { String str = "{\"str\":\"str\",\"int\":1,\"bool\":true,\"array\":[\"a1\",1,true]}"; Map<String, Object> map = JsonUtils.toMap(str); Assert.assertEquals("str", map.get("str")); Assert.assertEquals(String.class, map.get("str").getClass()); Assert.assertEquals(new Integer(1), map.get("int")); Assert.assertEquals(Integer.class, map.get("int").getClass()); Assert.assertEquals(new Boolean(true), map.get("bool")); Assert.assertEquals(Boolean.class, map.get("bool").getClass()); Assert.assertEquals(Object[].class, map.get("array").getClass()); Object[] array = (Object[]) map.get("array"); Assert.assertEquals(String.class, array[0].getClass()); Assert.assertEquals("a1", array[0]); Assert.assertEquals(Integer.class, array[1].getClass()); Assert.assertEquals(1, array[1]); Assert.assertEquals(Boolean.class, array[2].getClass()); Assert.assertEquals(new Boolean(true), array[2]); }