public void testIsFunction() { assertTrue(JSONUtils.isFunction("function(){ return a; }")); assertTrue(JSONUtils.isFunction("function (){ return a; }")); assertTrue(JSONUtils.isFunction("function() { return a; }")); assertTrue(JSONUtils.isFunction("function () { return a; }")); assertTrue(JSONUtils.isFunction("function(a){ return a; }")); }
@Test public void testParseJson() throws Exception { Map<String, Object> jsonParams = new HashMap<String, Object>(); jsonParams.put("author", "John B. Smith"); jsonParams.put("year", "2000"); String s = JSONUtils.buildJSON(jsonParams); Map<String, Object> map = JSONUtils.parseJSON(s); Assert.assertEquals("John B. Smith", map.get("author")); Assert.assertEquals("2000", map.get("year")); }
public void testNumberToString_null() { try { JSONUtils.numberToString(null); fail("Should have thrown a JSONException"); } catch (JSONException expected) { // ok } }
public String toString(boolean pretty) { try { return JSONUtils.format(this, pretty); } catch (IOException ex) { throw new RuntimeException( ex.getClass().getSimpleName() + " in " + getClass().getSimpleName() + ".toString()", ex); } }
public void testValidity_inifiniteFloat() { try { JSONUtils.testValidity(new Float(Float.POSITIVE_INFINITY)); fail("Should have thrown a JSONException"); } catch (JSONException expected) { // ok } }
public void testValidity_nanFloat() { try { JSONUtils.testValidity(new Float(Float.NaN)); fail("Should have thrown a JSONException"); } catch (JSONException expected) { // ok } }
public void testEstimate() throws Exception { Map<String, Object> obj = new HashMap<String, Object>(); // map: + 20 List<Object> value = new ArrayList<Object>(); // array: + 20 value.add("1234567890"); // string: + 20 + 2 * 10 => +40 value.add(12345); // number: + 20 + 8 => +28 obj.put("key", value); // key(string): +20 + 6 => +26 long size = JSONUtils.estimate(obj); assertEquals(134, size); }
public String execute() throws Exception { try { if (currentPage == null) { currentPage = 1; } if (rows == null) { rows = Constant.DEFAULT_ROWS; } MyPrint.myPrint("in ShowMessageAction!"); List<MessageBean> list = cmg.getMessages(courtId, currentPage, rows); int count = cmg.countMesgByCourtId(courtId); FrontMessage msg = new FrontMessage(list, count); JSONUtils.toJson(ServletActionContext.getResponse(), msg); } catch (Exception e) { e.printStackTrace(); JSONUtils.toJson(ServletActionContext.getResponse(), Constant.ERROR); } return null; }
@SuppressWarnings("unchecked") public static Props fromJSONString(String json) { try { Map<String, String> obj = (Map<String, String>) JSONUtils.parseJSONFromString(json); Props props = new Props(null, obj); return props; } catch (IOException e) { return null; } }
public String restClient(CbssHdapPerformance entity) { String json = JSONUtils.getJsonString4JavaPOJO(entity); // 将对象转换成json格式字符串 log.info("---------json-----------:" + json); ClientResponse response = service.put(ClientResponse.class, json); log.info("-------response status-------" + response.getStatus()); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } String result = response.getEntity(String.class); return result; }
public void testToJSON() throws Exception { AdverseEventDTO ae = new AdverseEventDTO(); TermDTO t = new TermDTO(); t.setCode("1001"); t.setId(1); t.setName("Nausea"); ae.setTerm(t); ae.setGrade("NORMAL"); ae.setWhySerious("YES"); ae.setExpected("true"); ae.setAttribution("LIKELY"); ae.setVerbatim("Sick to Stomach"); String s = JSONUtils.toJSON(ae); System.out.println(s); }
@Test public void testBuildJSON() throws Exception { Map<String, Object> params = new HashMap<String, Object>(); params.put(OAuthError.OAUTH_ERROR, OAuthError.TokenResponse.INVALID_REQUEST); String json = JSONUtils.buildJSON(params); JSONObject obj = new JSONObject(json); AbstractXMLStreamReader reader = new MappedXMLStreamReader(obj); Assert.assertEquals(XMLStreamReader.START_ELEMENT, reader.next()); Assert.assertEquals(OAuthError.OAUTH_ERROR, reader.getName().getLocalPart()); Assert.assertEquals(OAuthError.TokenResponse.INVALID_REQUEST, reader.getText()); Assert.assertEquals(XMLStreamReader.CHARACTERS, reader.next()); Assert.assertEquals(XMLStreamReader.END_ELEMENT, reader.next()); Assert.assertEquals(XMLStreamReader.END_DOCUMENT, reader.next()); }
public void testDoubleToString_infinite() { assertEquals("null", JSONUtils.doubleToString(Double.POSITIVE_INFINITY)); }
public void testStripQuotes_twoChars_singleQuote() { String quoted = "''"; String actual = JSONUtils.stripQuotes(quoted); assertEquals("", actual); }
public static String json(Object object) { if (JSONUtils.isArray(object)) return JSONArray.fromObject(object).toString(); if (JSONUtils.isObject(object)) return JSONObject.fromObject(object).toString(); return object.toString(); }
public void testGetFunctionParams() { assertEquals("", JSONUtils.getFunctionParams("function()")); assertEquals("a", JSONUtils.getFunctionParams("function(a)")); assertEquals("a,b", JSONUtils.getFunctionParams("function(a,b)")); assertEquals("", JSONUtils.getFunctionParams("notAFunction")); }
public void testQuote_escapeChars() { assertEquals("\"\\b\\t\\n\\r\\f\"", JSONUtils.quote("\b\t\n\r\f")); }
public void testQuote_emptyString() { assertEquals("\"\"", JSONUtils.quote("")); }
public static String toJSONString(Props props, boolean localOnly) { Map<String, String> map = toStringMap(props, localOnly); return JSONUtils.toJSON(map); }
public void testDoubleToString_nan() { assertEquals("null", JSONUtils.doubleToString(Double.NaN)); }
public void testDoubleToString_trailingZeros() { assertEquals("200", JSONUtils.doubleToString(200.00000)); }
static { GsonBuilder builder = JSONUtils.getDefaultGsonBuilder(); builder.registerTypeAdapter(Worker.class, new SimpleWorkerSerializer()); builder.registerTypeAdapter(AssignedLabel.class, new AssignSerializer()); serializer = builder.create(); }
public void testIsArray() { assertTrue(JSONUtils.isArray(new Object[0])); assertTrue(JSONUtils.isArray(new boolean[0])); assertTrue(JSONUtils.isArray(new byte[0])); assertTrue(JSONUtils.isArray(new char[0])); assertTrue(JSONUtils.isArray(new short[0])); assertTrue(JSONUtils.isArray(new int[0])); assertTrue(JSONUtils.isArray(new long[0])); assertTrue(JSONUtils.isArray(new float[0])); assertTrue(JSONUtils.isArray(new double[0])); // two dimensions assertTrue(JSONUtils.isArray(new Object[0][0])); assertTrue(JSONUtils.isArray(new boolean[0][0])); assertTrue(JSONUtils.isArray(new byte[0][0])); assertTrue(JSONUtils.isArray(new char[0][0])); assertTrue(JSONUtils.isArray(new short[0][0])); assertTrue(JSONUtils.isArray(new int[0][0])); assertTrue(JSONUtils.isArray(new long[0][0])); assertTrue(JSONUtils.isArray(new float[0][0])); assertTrue(JSONUtils.isArray(new double[0][0])); // collections assertTrue(JSONUtils.isArray(Collections.EMPTY_SET)); assertTrue(JSONUtils.isArray(Collections.EMPTY_LIST)); // jsonArray assertTrue(JSONUtils.isArray(new JSONArray())); }
public void testStripQuotes_singleChar_doubleeQuote() { String quoted = "\""; String actual = JSONUtils.stripQuotes(quoted); assertEquals(quoted, actual); }
public void testQuote_jsonFunction() { JSONFunction jsonFunction = new JSONFunction("a"); assertEquals("function(){ a }", JSONUtils.quote(jsonFunction.toString())); }
// ---- json ------------------------------------------------------- public static String asJSON(Object object) { return JSONUtils.toJSONString(object); }
public void testQuote_nullString() { assertEquals("\"\"", JSONUtils.quote(null)); }
public ResultsJSONingTest() { GsonBuilder builder = JSONUtils.getFilledDefaultGsonBuilder(); gson = builder.create(); }