Search in sources :

Example 81 with JSONObject

use of com.alibaba.fastjson.JSONObject in project fastjson by alibaba.

the class Bug_for_issue_320 method test_for_issue.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_for_issue() throws Exception {
    Map map = new HashMap();
    map.put(1001L, "aaa");
    String text = JSON.toJSONString(map);
    Assert.assertEquals("{1001:\"aaa\"}", text);
    JSONObject obj = JSON.parseObject(text);
    Assert.assertEquals("aaa", obj.get("1001"));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 82 with JSONObject

use of com.alibaba.fastjson.JSONObject in project fastjson by alibaba.

the class XMLGregorianCalendarTest method test_for_issue.

public void test_for_issue() throws Exception {
    GregorianCalendar gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
    XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
    String text = JSON.toJSONString(calendar);
    Assert.assertEquals(Long.toString(gregorianCalendar.getTimeInMillis()), text);
    XMLGregorianCalendar calendar1 = JSON.parseObject(text, XMLGregorianCalendar.class);
    assertEquals(calendar.toGregorianCalendar().getTimeInMillis(), calendar1.toGregorianCalendar().getTimeInMillis());
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("calendar", calendar);
    String json = JSON.toJSONString(jsonObject);
    Model model = JSON.parseObject(json).toJavaObject(Model.class);
    assertEquals(calendar.toGregorianCalendar().getTimeInMillis(), model.calendar.toGregorianCalendar().getTimeInMillis());
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) JSONObject(com.alibaba.fastjson.JSONObject) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar)

Example 83 with JSONObject

use of com.alibaba.fastjson.JSONObject in project fastjson by alibaba.

the class Issue978 method test_for_issue.

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());
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) Date(java.util.Date)

Example 84 with JSONObject

use of com.alibaba.fastjson.JSONObject in project fastjson by alibaba.

the class Issue_611 method test_for_issue.

public void test_for_issue() throws Exception {
    String text = "{\"priority\":1}";
    JSONObject obj = JSON.parseObject(text);
    Assert.assertEquals(1, obj.getInteger("priority").intValue());
    Assert.assertEquals(1, obj.getIntValue("priority"));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject)

Example 85 with JSONObject

use of com.alibaba.fastjson.JSONObject in project fastjson by alibaba.

the class Issue_748 method testJsonObjectWithClassName.

public void testJsonObjectWithClassName() {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("key1", "value1");
    jsonObject.put("key2", "value2");
    DataObject dataObject = new DataObject();
    dataObject.setValue(jsonObject);
    String jsonStr = JSON.toJSONString(dataObject, SerializerFeature.QuoteFieldNames, SerializerFeature.SkipTransientField, SerializerFeature.WriteClassName);
    //        System.out.println("parse之前:" + jsonStr);
    DataObject obj = (DataObject) JSON.parse(jsonStr, Feature.IgnoreNotMatch);
    Assert.assertNotNull(obj.value);
    Assert.assertNotNull(obj.value.get("key1"));
    Assert.assertNotNull(obj.value.get("key2"));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)374 JSONArray (com.alibaba.fastjson.JSONArray)37 Test (org.junit.Test)32 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)23 HashMap (java.util.HashMap)20 JSONException (com.alibaba.fastjson.JSONException)18 ArrayList (java.util.ArrayList)18 Date (java.util.Date)18 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)14 IOException (java.io.IOException)12 Map (java.util.Map)12 WXSDKInstanceTest (com.taobao.weex.WXSDKInstanceTest)11 JSMethod (com.taobao.weex.annotation.JSMethod)11 WXBridgeManagerTest (com.taobao.weex.bridge.WXBridgeManagerTest)9 BigDecimal (java.math.BigDecimal)9 List (java.util.List)9 ValueStack (com.roof.chain.api.ValueStack)7 GenericValueStack (com.roof.chain.support.GenericValueStack)7 Gson (com.google.gson.Gson)6 BigInteger (java.math.BigInteger)5