@Test public void testToJSONString2() throws Exception { SimpleJSON ss1 = SimpleJSON.getInstance() .addItem("Level", "\n") .addItem("MM", false) .addItem("Name", "SLf4j"); final SimpleJSON ss2 = SimpleJSON.getInstance() .addItem("str1", ss1) .addItem("Name", "SLf4j") .addItem("LL", 125L) .addItem("now", new Date()); //System.out.println(ss2.toString()); final SimpleJSON ss3 = SimpleJSON.getInstance() .addItem("str1", ss1.getMap()) .addItem("Name", "SLf4j") .addItem("LL", 125L) .addItem("now", new Date()); final SerializeConfig mapping = new SerializeConfig(); mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss")); Assert.assertEquals(JSON.toJSONString(ss3.getMap(), mapping), ss2.toString()); }
public void test_codec() throws Exception { SerializeConfig mapping = new SerializeConfig(); mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); V0 v = new V0(); v.setValue(new Date()); String text = JSON.toJSONString(v, mapping); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale); format.setTimeZone(JSON.defaultTimeZone); Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text); }
public void test_codec_no_asm() throws Exception { V0 v = new V0(); v.setValue(new Date()); SerializeConfig mapping = new SerializeConfig(); mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); mapping.setAsmEnable(false); String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale); format.setTimeZone(JSON.defaultTimeZone); Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text); }
public void test_codec_asm() throws Exception { V0 v = new V0(); v.setValue(new Date()); SerializeConfig mapping = new SerializeConfig(); mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); mapping.setAsmEnable(true); String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale); format.setTimeZone(JSON.defaultTimeZone); Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text); }
public void test_codec_null_asm() throws Exception { V0 v = new V0(); SerializeConfig mapping = new SerializeConfig(); mapping.setAsmEnable(true); String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue); mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); Assert.assertEquals("{\"value\":null}", text); V0 v1 = JSON.parseObject(text, V0.class); Assert.assertEquals(v1.getValue(), v.getValue()); }
public void test_codec_null_no_asm() throws Exception { V0 v = new V0(); SerializeConfig mapping = new SerializeConfig(); mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); mapping.setAsmEnable(false); String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue); Assert.assertEquals("{\"value\":null}", text); V0 v1 = JSON.parseObject(text, V0.class); Assert.assertEquals(v1.getValue(), v.getValue()); }
public void test_0() throws Exception { SerializeConfig config = new SerializeConfig(); config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); config.setAsmEnable(false); Entity object = new Entity(); object.setValue(new Date()); String text = JSON.toJSONString(object, config); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale); format.setTimeZone(JSON.defaultTimeZone); Assert.assertEquals("{\"value\":\"" + format.format(object.getValue()) + "\"}", text); }
public void test_0() throws Exception { SerializeConfig mapping = new SerializeConfig(); mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); Entity object = new Entity(); object.setValue(new Date()); String text = JSON.toJSONString(object, mapping); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale); format.setTimeZone(JSON.defaultTimeZone); Assert.assertEquals("{\"value\":\"" + format.format(object.getValue()) + "\"}", text); }
public void test_for_loveflying() throws Exception { User user = new User(); user.setId(1l); user.setName("loveflying"); user.setCreateTime(new java.sql.Timestamp(new Date().getTime())); UserLog userLog = new UserLog(); userLog.setId(1l); userLog.setUser(user); user.getUserLogs().add(userLog); userLog = new UserLog(); userLog.setId(2l); userLog.setUser(user); user.getUserLogs().add(userLog); SerializeConfig mapping = new SerializeConfig(); mapping.put(java.sql.Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); mapping.put(java.sql.Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss")); // mapping.put(User.class, new JavaBeanSerializer(User.class, // Collections.singletonMap("id", "uid"))); JSONObject jsonObject = (JSONObject) JSON.toJSON(user); jsonObject.put("ext", "新加的属性"); System.out.println(jsonObject.toJSONString(jsonObject, mapping)); }
public static String createSuccessResponse(Integer httpCode, String message, Object result, SerializerFeature serializerFeature, SerializeFilter filter, HttpServletResponse response){ PrintWriter printWriter = null; String jsonString = ""; try { response.setCharacterEncoding(RESPONSE_CHARACTERENCODING); response.setContentType(RESPONSE_CONTENTTYPE); response.setStatus(httpCode); printWriter = response.getWriter(); SerializeConfig config = new SerializeConfig(); config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); Map<String, Object> map = new HashMap<String, Object>(); if(null != result){ map.put("res_code", httpCode); map.put("message", message); map.put("data",result); if(null!=filter){ jsonString = JSONObject.toJSONString(map,filter,serializerFeature); }else{ // jsonString = JSONObject.toJSONString(map,config,serializerFeature); jsonString = JSONObject.toJSONStringWithDateFormat(map,"yyyy-MM-dd"); } printWriter.write(jsonString); } printWriter.flush(); } catch (Exception e) { log.error("createResponse failed", e); } finally { if(null!=printWriter)printWriter.close(); } return jsonString; }
/** * 设置java.util.Date和java.sql.Date的格式(用于fastjson) * @param format */ public static void setDateFormat(String format) { mapping.put(Date.class, new SimpleDateFormatSerializer(format)); mapping.put(java.sql.Date.class, new SimpleDateFormatSerializer(format)); }
/** * 设置java.sql.Time的格式(用于fastjson) * @param format */ public static void setTimeFormat(String format) { mapping.put(java.sql.Time.class, new SimpleDateFormatSerializer(format)); }
/** * 设置java.sql.Timestamp的格式(用于fastjson) * @param format */ public static void setTimestampFormat(String format) { mapping.put(java.sql.Timestamp.class, new SimpleDateFormatSerializer(format)); }
public JsonSeriaziler(String dateFormat){ features[features.length] = SerializerFeature.WriteDateUseDateFormat; mapping.put(java.util.Date.class, new SimpleDateFormatSerializer(dateFormat)); }