Esempio n. 1
0
 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; }"));
 }
Esempio n. 2
0
  @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"));
  }
Esempio n. 3
0
 public void testNumberToString_null() {
   try {
     JSONUtils.numberToString(null);
     fail("Should have thrown a JSONException");
   } catch (JSONException expected) {
     // ok
   }
 }
Esempio n. 4
0
 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);
   }
 }
Esempio n. 5
0
 public void testValidity_inifiniteFloat() {
   try {
     JSONUtils.testValidity(new Float(Float.POSITIVE_INFINITY));
     fail("Should have thrown a JSONException");
   } catch (JSONException expected) {
     // ok
   }
 }
Esempio n. 6
0
 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);
 }
Esempio n. 8
0
 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;
 }
Esempio n. 9
0
 @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;
   }
 }
Esempio n. 10
0
 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;
 }
Esempio n. 11
0
  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);
  }
Esempio n. 12
0
  @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());
  }
Esempio n. 13
0
 public void testDoubleToString_infinite() {
   assertEquals("null", JSONUtils.doubleToString(Double.POSITIVE_INFINITY));
 }
Esempio n. 14
0
 public void testStripQuotes_twoChars_singleQuote() {
   String quoted = "''";
   String actual = JSONUtils.stripQuotes(quoted);
   assertEquals("", actual);
 }
Esempio n. 15
0
 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();
 }
Esempio n. 16
0
 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"));
 }
Esempio n. 17
0
 public void testQuote_escapeChars() {
   assertEquals("\"\\b\\t\\n\\r\\f\"", JSONUtils.quote("\b\t\n\r\f"));
 }
Esempio n. 18
0
 public void testQuote_emptyString() {
   assertEquals("\"\"", JSONUtils.quote(""));
 }
Esempio n. 19
0
 public static String toJSONString(Props props, boolean localOnly) {
   Map<String, String> map = toStringMap(props, localOnly);
   return JSONUtils.toJSON(map);
 }
Esempio n. 20
0
 public void testDoubleToString_nan() {
   assertEquals("null", JSONUtils.doubleToString(Double.NaN));
 }
Esempio n. 21
0
 public void testDoubleToString_trailingZeros() {
   assertEquals("200", JSONUtils.doubleToString(200.00000));
 }
Esempio n. 22
0
 static {
   GsonBuilder builder = JSONUtils.getDefaultGsonBuilder();
   builder.registerTypeAdapter(Worker.class, new SimpleWorkerSerializer());
   builder.registerTypeAdapter(AssignedLabel.class, new AssignSerializer());
   serializer = builder.create();
 }
Esempio n. 23
0
  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()));
  }
Esempio n. 24
0
 public void testStripQuotes_singleChar_doubleeQuote() {
   String quoted = "\"";
   String actual = JSONUtils.stripQuotes(quoted);
   assertEquals(quoted, actual);
 }
Esempio n. 25
0
 public void testQuote_jsonFunction() {
   JSONFunction jsonFunction = new JSONFunction("a");
   assertEquals("function(){ a }", JSONUtils.quote(jsonFunction.toString()));
 }
Esempio n. 26
0
 // ---- json -------------------------------------------------------
 public static String asJSON(Object object) {
   return JSONUtils.toJSONString(object);
 }
Esempio n. 27
0
 public void testQuote_nullString() {
   assertEquals("\"\"", JSONUtils.quote(null));
 }
 public ResultsJSONingTest() {
   GsonBuilder builder = JSONUtils.getFilledDefaultGsonBuilder();
   gson = builder.create();
 }