Java Code Examples for com.alibaba.fastjson.JSON.toJSON()

The following are Jave code examples for showing how to use toJSON() of the com.alibaba.fastjson.JSON class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: xxpay-master   File: PayOrderController.java   Source Code and License Vote up 6 votes
@RequestMapping("/view.html")
public String viewInput(String payOrderId, ModelMap model) {
    PayOrder item = null;
    if(StringUtils.isNotBlank(payOrderId)) {
        item = payOrderService.selectPayOrder(payOrderId);
    }
    if(item == null) {
        item = new PayOrder();
        model.put("item", item);
        return "pay_order/view";
    }
    JSONObject object = (JSONObject) JSON.toJSON(item);
    if(item.getPaySuccTime() != null) object.put("paySuccTime", DateUtil.date2Str(new Date(item.getPaySuccTime())));
    if(item.getLastNotifyTime() != null) object.put("lastNotifyTime", DateUtil.date2Str(new Date(item.getLastNotifyTime())));
    if(item.getExpireTime() != null) object.put("expireTime", DateUtil.date2Str(new Date(item.getExpireTime())));
    if(item.getAmount() != null) object.put("amount", AmountUtil.convertCent2Dollar(item.getAmount()+""));
    model.put("item", object);
    return "pay_order/view";
}
 
Example 2
Project: pay-xxpay-master   File: PayOrderController.java   Source Code and License Vote up 6 votes
@RequestMapping("/view.html")
public String viewInput(String payOrderId, ModelMap model) {
    PayOrder item = null;
    if(StringUtils.isNotBlank(payOrderId)) {
        item = payOrderService.selectPayOrder(payOrderId);
    }
    if(item == null) {
        item = new PayOrder();
        model.put("item", item);
        return "pay_order/view";
    }
    JSONObject object = (JSONObject) JSON.toJSON(item);
    if(item.getPaySuccTime() != null) object.put("paySuccTime", DateUtil.date2Str(new Date(item.getPaySuccTime())));
    if(item.getLastNotifyTime() != null) object.put("lastNotifyTime", DateUtil.date2Str(new Date(item.getLastNotifyTime())));
    if(item.getExpireTime() != null) object.put("expireTime", DateUtil.date2Str(new Date(item.getExpireTime())));
    if(item.getAmount() != null) object.put("amount", AmountUtil.convertCent2Dollar(item.getAmount()+""));
    model.put("item", object);
    return "pay_order/view";
}
 
Example 3
Project: xxpay-master   File: MchNotifyController.java   Source Code and License Vote up 6 votes
@RequestMapping("/view.html")
public String viewInput(String orderId, ModelMap model) {
    MchNotify item = null;
    if(StringUtils.isNotBlank(orderId)) {
        item = mchNotifyService.selectMchNotify(orderId);
    }
    if(item == null) {
        item = new MchNotify();
        model.put("item", item);
        return "mch_notify/view";
    }
    JSONObject object = (JSONObject) JSON.toJSON(item);
    if(item.getCreateTime() != null) object.put("createTime", DateUtil.date2Str(item.getCreateTime()));
    if(item.getUpdateTime() != null) object.put("updateTime", DateUtil.date2Str(item.getUpdateTime()));
    if(item.getLastNotifyTime() != null) object.put("lastNotifyTime", DateUtil.date2Str(item.getLastNotifyTime()));
    model.put("item", object);
    return "mch_notify/view";
}
 
Example 4
Project: GitHub   File: AnnotationTest.java   Source Code and License Vote up 6 votes
public void test_annoation() throws Exception {
        Bob bob = new Bob("Bob", 30, true);
//        JSONObject obj = (JSONObject) JSON.toJSON(bob);
//        assertEquals(3, obj.size());
//        assertEquals(Boolean.TRUE, obj.get("sex"));
//        assertEquals("Bob", obj.get("name"));
//        assertEquals(new Integer(30), obj.get("age"));

        PersonInfo info = Bob.class.getAnnotation(PersonInfo.class);
        JSONObject obj = (JSONObject) JSON.toJSON(info);

        assertEquals(3, obj.size());
        assertEquals(Boolean.TRUE, obj.get("sex"));
        assertEquals("Bob", obj.get("name"));
        assertEquals(new Integer(30), obj.get("age"));
    }
 
Example 5
Project: HiTSDB-Client   File: HiTSDBClient.java   Source Code and License Vote up 6 votes
@Override
public List<LastDPValue> lastdp(Collection<Timeline> timelines) throws HttpUnknowStatusException {
	Object timelinesJSON = JSON.toJSON(timelines);
	JSONObject obj = new JSONObject();
	obj.put("queries", timelinesJSON);
	String jsonString = obj.toJSONString();
	HttpResponse httpResponse = httpclient.post(HttpAPI.QUERY_LASTDP, jsonString);
	ResultResponse resultResponse = ResultResponse.simplify(httpResponse, this.httpCompress);
	HttpStatus httpStatus = resultResponse.getHttpStatus();
	switch (httpStatus) {
	case ServerSuccessNoContent:
		return null;
	case ServerSuccess:
		String content = resultResponse.getContent();
		List<LastDPValue> queryResultList = JSON.parseArray(content, LastDPValue.class);
		return queryResultList;
	case ServerNotSupport:
		throw new HttpServerNotSupportException(resultResponse);
	case ServerError:
		throw new HttpServerErrorException(resultResponse);
	default:
		throw new HttpUnknowStatusException(resultResponse);
	}
}
 
Example 6
Project: xxpay-master   File: RefundOrderController.java   Source Code and License Vote up 6 votes
@RequestMapping("/view.html")
public String viewInput(String refundOrderId, ModelMap model) {
    RefundOrder item = null;
    if(StringUtils.isNotBlank(refundOrderId)) {
        item = refundOrderService.selectRefundOrder(refundOrderId);
    }
    if(item == null) {
        item = new RefundOrder();
        model.put("item", item);
        return "refund_order/view";
    }
    JSONObject object = (JSONObject) JSON.toJSON(item);
    if(item.getRefundSuccTime() != null) object.put("refundSuccTime", DateUtil.date2Str(item.getRefundSuccTime()));
    if(item.getExpireTime() != null) object.put("expireTime", DateUtil.date2Str(item.getExpireTime()));
    if(item.getRefundAmount() != null) object.put("amount", AmountUtil.convertCent2Dollar(item.getRefundAmount()+""));
    model.put("item", object);
    return "refund_order/view";
}
 
Example 7
Project: iunet-blog   File: SerializerUtil.java   Source Code and License Vote up 5 votes
public static JSONObject encryptionByName(HttpSession session, Object data, Set<String> valueSet, boolean encryption) {
	JSONObject req = (JSONObject) JSON.toJSON(data);
	SerializerUtil serializer = new SerializerUtil(session, null, valueSet, encryption);
	if(null != valueSet) {
		return (JSONObject) req.parse(req.toJSONString(req, serializer.valuefilter));
	}
	return req;
}
 
Example 8
Project: GitHub   File: JSONFromObjectTest.java   Source Code and License Vote up 5 votes
public void test_1() throws Exception {
    JSONObject user = new JSONObject();
    user.put("id", 3);
    user.put("name", "周访");

    JSONObject json = (JSONObject) JSON.toJSON(user);

    Assert.assertEquals(new Long(3), json.getLong("id"));
    Assert.assertEquals("周访", json.getString("name"));
}
 
Example 9
Project: GitHub   File: JSONFromObjectTest.java   Source Code and License Vote up 5 votes
public void test_2() throws Exception {
    HashMap user = new HashMap();
    user.put("id", 3);
    user.put("name", "周访");

    JSONObject json = (JSONObject) JSON.toJSON(user);

    Assert.assertEquals(new Long(3), json.getLong("id"));
    Assert.assertEquals("周访", json.getString("name"));
}
 
Example 10
Project: GitHub   File: JSONFromObjectTest.java   Source Code and License Vote up 5 votes
public void test_3() throws Exception {
    List users = new ArrayList();
    HashMap user = new HashMap();
    user.put("id", 3);
    user.put("name", "周访");
    users.add(user);

    JSONArray array = (JSONArray) JSON.toJSON(users);
    JSONObject json = array.getJSONObject(0);

    Assert.assertEquals(new Long(3), json.getLong("id"));
    Assert.assertEquals("周访", json.getString("name"));
}
 
Example 11
Project: GitHub   File: JSONFromObjectTest.java   Source Code and License Vote up 5 votes
public void test_error() throws Exception {
    C c = new C();

    JSONException error = null;
    try {
        JSON.toJSON(c);
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
 
Example 12
Project: GitHub   File: TypeUtilsTest3.java   Source Code and License Vote up 5 votes
public void test_ex() throws Exception {
    RuntimeException ex = new RuntimeException();
    JSONObject object = (JSONObject) JSON.toJSON(ex);
    JSONArray array = object.getJSONArray("stackTrace");
    array.getJSONObject(0).put("lineNumber", null);
    
    JSON.parseObject(object.toJSONString(), Exception.class);
}
 
Example 13
Project: GitHub   File: PageToJSONTest.java   Source Code and License Vote up 5 votes
public void test_page() throws Exception {
    List<Post> postList = new ArrayList<Post>();
    {
        postList.add(new Post(1001));
    }
    
    Page<Post> page = new PageImpl(postList);
    
    JSONObject obj = (JSONObject) JSON.toJSON(page);
    Assert.assertNotNull(obj);
    Assert.assertEquals(1, obj.getJSONArray("content").size());
}
 
Example 14
Project: GitHub   File: Issue955.java   Source Code and License Vote up 5 votes
public void test_checkObject() {
    Art origin = makeOrigin();
    JSONObject articleObj = (JSONObject) JSON.toJSON(origin);

    JSONObject dataObj = new JSONObject();
    dataObj.put("art", articleObj);

    Art other = dataObj.getObject("art", Art.class);// return null;
    assertSame(origin, other); // test failed
}
 
Example 15
Project: GitHub   File: Issue978.java   Source Code and License Vote up 5 votes
public void test_for_issue() throws Exception {
    Model model = new Model();
    model.date = new Date(1483413683714L);

    JSONObject obj = (JSONObject) JSON.toJSON(model);
    assertEquals("{\"date\":\"2017-01-03 11:21:23\"}", obj.toJSONString());
}
 
Example 16
Project: GitHub   File: Issue1023.java   Source Code and License Vote up 5 votes
public void test_for_issue() throws Exception {
    Date now = new Date();

    GregorianCalendar gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
    gregorianCalendar.setTime(now);

    XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);

    String jsonString = JSON.toJSONString(calendar);
    // success
    calendar = JSON.parseObject(jsonString, XMLGregorianCalendar.class);

    Object toJSON1 = JSON.toJSON(calendar); // debug看到是 Long 类型
    // 所以这里会报错:
    // error: java.lang.ClassCastException: java.lang.Long cannot be cast to com.alibaba.fastjson.JSONObject
    //JSONObject jsonObject = (JSONObject) JSON.toJSON(calendar);
    // 所以 这里肯定会报错, 因为 jsonObject 不是JSONObject类型
    //calendar = jsonObject.toJavaObject(XMLGregorianCalendar.class);

    List<XMLGregorianCalendar> calendarList = new ArrayList<XMLGregorianCalendar>();
    calendarList.add(calendar);
    calendarList.add(calendar);
    calendarList.add(calendar);

    Object toJSON2 = JSON.toJSON(calendarList); // debug 看到是 JSONArray 类型

    // success: 通过 JSONArray.parseArray 方法可以正确转换
    JSONArray jsonArray = (JSONArray) JSON.toJSON(calendarList);
    jsonString = jsonArray.toJSONString();
    List<XMLGregorianCalendar> calendarList1 = JSONArray.parseArray(jsonString, XMLGregorianCalendar.class);

    // 通过 jsonArray.toJavaList 无法转换
    // error: com.alibaba.fastjson.JSONException: can not cast to : javax.xml.datatype.XMLGregorianCalendar
    List<XMLGregorianCalendar> calendarList2 = jsonArray.toJavaList(XMLGregorianCalendar.class);
    assertNotNull(calendarList2);
    assertEquals(3, calendarList2.size());
}
 
Example 17
Project: GitHub   File: Bug_101_for_rongganlin_case3.java   Source Code and License Vote up 5 votes
public void test_for_bug() throws Exception {
    Entity entity = new Entity();
    entity.setHolder(new Holder<String>("AAA"));

    JSONObject json = (JSONObject) JSON.toJSON(entity);
    Entity entity2 = JSON.toJavaObject(json, Entity.class);
    Assert.assertEquals(JSON.toJSONString(entity), JSON.toJSONString(entity2));
}
 
Example 18
Project: pay4j   File: ListSuffixResultSerializer.java   Source Code and License Vote up 5 votes
public static JSONObject serializeToJSON(Object object) {
    JSONObject result = (JSONObject) JSON.toJSON(object);
    Map<Field, String[]> listSuffixFields = ListSuffixResultDeserializer
            .getListSuffixFields(object.getClass());
    if (!listSuffixFields.isEmpty()) {
        JSONField jsonField = null;
        Object value = null;
        for (Field field : listSuffixFields.keySet()) {
            jsonField = field.getAnnotation(JSONField.class);
            if (jsonField != null && Strings.isNullOrEmpty(jsonField.name())) {
                result.remove(jsonField.name());
            } else {
                result.remove(field.getName());
            }
            try {
                field.setAccessible(true);
                value = field.get(object);
            } catch (Exception e) {

            }
            if (value != null && value instanceof List) {
                result.putAll(listSuffixConvertMap((List<?>) value));
            }
        }
    }
    return result;
}
 
Example 19
Project: elasticsearch-jdbc   File: FastJSONAdaptor.java   Source Code and License Vote up 4 votes
@Override
public JSONObject toJSONObject(Object obj) {
    return new FastJSONObject((com.alibaba.fastjson.JSONObject) JSON.toJSON(obj));
}
 
Example 20
Project: GitHub   File: Issue274.java   Source Code and License Vote up 4 votes
public void test() throws Exception {
    Customer cus = new Customer();
    cus.setId(1L);
    cus.setName("name");

    Object json = JSON.toJSON(cus);
    System.out.println(json);

    String cusJson = json.toString();

    cusJson = "{\"name\":\"name\",\"id\":1}";

    Customer customer = JSON.parseObject(cusJson, Customer.class);

    System.out.println(customer);
}