static JSONObject parametersToJsonObject(Map<String, String> params) throws JSONException { JSONObject root = new JSONObject(); for (Map.Entry<String, String> entry : params.entrySet()) { String[] path = StringUtils.splitPreserveAllTokens(entry.getKey(), '.'); JSONObject holder = buildHolder(root, path, 0); if (path.length > 1) { holder.put(path[path.length - 1], convertToJsonValue(entry.getValue())); } else { holder.put(path[0], convertToJsonValue(entry.getValue())); } } return root; }
result.put(prefix + '.' + key, "null"); } else { collect(json.get(key), prefix + '.' + key, result); if (isAllLiterals(jsonArr)) { result.put(prefix + '(' + i + ')', "null"); } else { collect(jsonArr.get(i), prefix + '(' + i + ')', result);
return buildHolder(newStep, steps, ++currentStep); } else { JSONObject newStep; root.put(steps[currentStep], newStep); return buildHolder(newStep, steps, ++currentStep);
@Test public void testJsonFromRequest() throws Exception { HttpServletRequest fakeRequest; for (String badParms : ImmutableList.of("x=1", "x=1&callback=")) { fakeRequest = new FakeHttpServletRequest("http://foo.com/gadgets/rpc?" + badParms); assertNull(JsonConversionUtil.fromRequest(fakeRequest)); } }
@Test public void testValueToJsonParsing() throws Exception { String longNumber = "108502345354398668456"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longNumber), longNumber); String longDoubleOverflow = "108502345354398668456.1234"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longDoubleOverflow), longDoubleOverflow); String longDoubleFractionPart = "1.108502345354398668456108502345354398668456"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longDoubleFractionPart), longDoubleFractionPart); assertJsonEquals(JsonConversionUtil.convertToJsonValue("12345"), 12345); assertJsonEquals(JsonConversionUtil.convertToJsonValue("12.345"), 12.345); assertJsonEquals(JsonConversionUtil.convertToJsonValue("abc"), "abc"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("\"a,b,c\""), "a,b,c"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("true"), true); assertJsonEquals(JsonConversionUtil.convertToJsonValue("false"), false); assertJsonEquals(JsonConversionUtil.convertToJsonValue("null"), JSONObject.NULL); assertJsonEquals(JsonConversionUtil.convertToJsonValue("'abc'"), "abc"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("a,b,c"), new JSONArray(Lists.newArrayList("a", "b", "c"))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("1,2,3,true,false,null"), new JSONArray(Lists.<Object>newArrayList(1, 2, 3, true, false, null))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("(1)"), new JSONArray(Lists.newArrayList(1))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("(true)"), new JSONArray(Lists.newArrayList(true))); }
@Test public void testJSONToParameterMapParsing() throws Exception { Map<String, String> resultMap = JsonConversionUtil .fromJson(new JSONObject("{a:{b:[{c:\"hello\"},{c:\"hello\"}]}}")); assertEquals(2, resultMap.size()); assertEquals("hello", resultMap.get(".a.b(0).c")); assertEquals("hello", resultMap.get(".a.b(1).c")); }
public static Map<String, String> fromJson(JSONObject obj) throws JSONException { Map<String, String> result = Maps.newHashMap(); collect(obj, "", result); return result; }
@Test public void testParameterMapToJsonParsing() throws Exception { assertJsonEquals(JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "1")), new JSONObject("{a:{b:{c:1}}}")); assertJsonEquals( JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "\"1\"")), new JSONObject("{a:{b:{c:\"1\"}}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "true")), new JSONObject("{a:{b:{c:true}}}")); assertJsonEquals( JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "false")), new JSONObject("{a:{b:{c:false}}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "null")), new JSONObject("{a:{b:{c:null}}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject( ImmutableMap.of("a.b(0).c", "hello", "a.b(1).c", "hello")), new JSONObject("{a:{b:[{c:\"hello\"},{c:\"hello\"}]}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject( ImmutableMap.of("a.b.c", "hello, true, false, null, 1,2, \"null\", \"()\"")), new JSONObject("{a:{b:{c:[\"hello\",true,false,null,1,2,\"null\",\"()\"]}}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject( ImmutableMap.of("a.b.c", "\"hello, true, false, null, 1,2\"")), new JSONObject("{a:{b:{c:\"hello, true, false, null, 1,2\"}}}")); }
return buildHolder(newStep, steps, ++currentStep); } else { JSONObject newStep; root.put(steps[currentStep], newStep); return buildHolder(newStep, steps, ++currentStep);
@Test public void testJsonFromRequest() throws Exception { HttpServletRequest fakeRequest; for (String badParms : ImmutableList.of("x=1", "x=1&callback=")) { fakeRequest = new FakeHttpServletRequest("http://foo.com/gadgets/rpc?" + badParms); assertNull(JsonConversionUtil.fromRequest(fakeRequest)); } }
@Test public void testValueToJsonParsing() throws Exception { String longNumber = "108502345354398668456"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longNumber), longNumber); String longDoubleOverflow = "108502345354398668456.1234"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longDoubleOverflow), longDoubleOverflow); String longDoubleFractionPart = "1.108502345354398668456108502345354398668456"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longDoubleFractionPart), longDoubleFractionPart); assertJsonEquals(JsonConversionUtil.convertToJsonValue("12345"), 12345); assertJsonEquals(JsonConversionUtil.convertToJsonValue("12.345"), 12.345); assertJsonEquals(JsonConversionUtil.convertToJsonValue("abc"), "abc"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("\"a,b,c\""), "a,b,c"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("true"), true); assertJsonEquals(JsonConversionUtil.convertToJsonValue("false"), false); assertJsonEquals(JsonConversionUtil.convertToJsonValue("null"), JSONObject.NULL); assertJsonEquals(JsonConversionUtil.convertToJsonValue("'abc'"), "abc"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("a,b,c"), new JSONArray(Lists.newArrayList("a", "b", "c"))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("1,2,3,true,false,null"), new JSONArray(Lists.<Object>newArrayList(1, 2, 3, true, false, null))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("(1)"), new JSONArray(Lists.newArrayList(1))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("(true)"), new JSONArray(Lists.newArrayList(true))); }
@Test public void testJSONToParameterMapParsing() throws Exception { Map<String, String> resultMap = JsonConversionUtil .fromJson(new JSONObject("{a:{b:[{c:\"hello\"},{c:\"hello\"}]}}")); assertEquals(2, resultMap.size()); assertEquals("hello", resultMap.get(".a.b(0).c")); assertEquals("hello", resultMap.get(".a.b(1).c")); }
public static Map<String, String> fromJson(JSONObject obj) throws JSONException { Map<String, String> result = Maps.newHashMap(); collect(obj, "", result); return result; }
@Test public void testParameterMapToJsonParsing() throws Exception { assertJsonEquals(JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "1")), new JSONObject("{a:{b:{c:1}}}")); assertJsonEquals( JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "\"1\"")), new JSONObject("{a:{b:{c:\"1\"}}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "true")), new JSONObject("{a:{b:{c:true}}}")); assertJsonEquals( JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "false")), new JSONObject("{a:{b:{c:false}}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject(ImmutableMap.of("a.b.c", "null")), new JSONObject("{a:{b:{c:null}}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject( ImmutableMap.of("a.b(0).c", "hello", "a.b(1).c", "hello")), new JSONObject("{a:{b:[{c:\"hello\"},{c:\"hello\"}]}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject( ImmutableMap.of("a.b.c", "hello, true, false, null, 1,2, \"null\", \"()\"")), new JSONObject("{a:{b:{c:[\"hello\",true,false,null,1,2,\"null\",\"()\"]}}}")); assertJsonEquals(JsonConversionUtil.parametersToJsonObject( ImmutableMap.of("a.b.c", "\"hello, true, false, null, 1,2\"")), new JSONObject("{a:{b:{c:\"hello, true, false, null, 1,2\"}}}")); }
static JSONObject parametersToJsonObject(Map<String, String> params) throws JSONException { JSONObject root = new JSONObject(); for (Map.Entry<String, String> entry : params.entrySet()) { String[] path = StringUtils.splitPreserveAllTokens(entry.getKey(), '.'); JSONObject holder = buildHolder(root, path, 0); if (path.length > 1) { holder.put(path[path.length - 1], convertToJsonValue(entry.getValue())); } else { holder.put(path[0], convertToJsonValue(entry.getValue())); } } return root; }
return buildHolder(newStep, steps, ++currentStep); } else { JSONObject newStep; root.put(steps[currentStep], newStep); return buildHolder(newStep, steps, ++currentStep);
@Test public void testJsonFromRequest() throws Exception { HttpServletRequest fakeRequest; for (String badParms : ImmutableList.of("x=1", "x=1&callback=")) { fakeRequest = new FakeHttpServletRequest("http://foo.com/gadgets/rpc?" + badParms); assertNull(JsonConversionUtil.fromRequest(fakeRequest)); } }
@Test public void testValueToJsonParsing() throws Exception { String longNumber = "108502345354398668456"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longNumber), longNumber); String longDoubleOverflow = "108502345354398668456.1234"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longDoubleOverflow), longDoubleOverflow); String longDoubleFractionPart = "1.108502345354398668456108502345354398668456"; assertJsonEquals(JsonConversionUtil.convertToJsonValue(longDoubleFractionPart), longDoubleFractionPart); assertJsonEquals(JsonConversionUtil.convertToJsonValue("12345"), 12345); assertJsonEquals(JsonConversionUtil.convertToJsonValue("12.345"), 12.345); assertJsonEquals(JsonConversionUtil.convertToJsonValue("abc"), "abc"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("\"a,b,c\""), "a,b,c"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("true"), true); assertJsonEquals(JsonConversionUtil.convertToJsonValue("false"), false); assertJsonEquals(JsonConversionUtil.convertToJsonValue("null"), JSONObject.NULL); assertJsonEquals(JsonConversionUtil.convertToJsonValue("'abc'"), "abc"); assertJsonEquals(JsonConversionUtil.convertToJsonValue("a,b,c"), new JSONArray(Lists.newArrayList("a", "b", "c"))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("1,2,3,true,false,null"), new JSONArray(Lists.<Object>newArrayList(1, 2, 3, true, false, null))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("(1)"), new JSONArray(Lists.newArrayList(1))); assertJsonEquals(JsonConversionUtil.convertToJsonValue("(true)"), new JSONArray(Lists.newArrayList(true))); }
result.put(prefix + '.' + key, "null"); } else { collect(json.get(key), prefix + '.' + key, result); if (isAllLiterals(jsonArr)) { result.put(prefix + '(' + i + ')', "null"); } else { collect(jsonArr.get(i), prefix + '(' + i + ')', result);
@Test public void testJSONToParameterMapParsing() throws Exception { Map<String, String> resultMap = JsonConversionUtil .fromJson(new JSONObject("{a:{b:[{c:\"hello\"},{c:\"hello\"}]}}")); assertEquals(2, resultMap.size()); assertEquals("hello", resultMap.get(".a.b(0).c")); assertEquals("hello", resultMap.get(".a.b(1).c")); }