public String toJSONString() { SerializeWriter out = new SerializeWriter(); try { new JSONSerializer(out).write(this); return out.toString(); } finally { out.close(); } }
public void writeJSONString(Appendable appendable) { SerializeWriter out = new SerializeWriter(); try { new JSONSerializer(out).write(this); appendable.append(out.toString()); } catch (IOException e) { throw new JSONException(e.getMessage(), e); } finally { out.close(); } }
public static String toJSONStringZ( Object object, SerializeConfig mapping, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(features); try { JSONSerializer serializer = new JSONSerializer(out, mapping); serializer.write(object); return out.toString(); } finally { out.close(); } }
public String encode(Object object) throws Exception { SerializeWriter out = new SerializeWriter(); out.config(SerializerFeature.DisableCircularReferenceDetect, true); // out.config(SerializerFeature.DisableCheckSpecialChar, true); JSONSerializer serializer = new JSONSerializer(out); serializer.write(object); String text = out.toString(); out.close(); return text; }
public static String toJSONString(Object object, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } serializer.write(object); return out.toString(); } finally { out.close(); } }
public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { return "ID"; } return name; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getNameFilters().add(filter); Bean a = new Bean(); serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"ID\":0}", text); }
/** @since 1.1.14 */ public static String toJSONStringWithDateFormat( Object object, String dateFormat, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (SerializerFeature feature : features) { serializer.config(feature, true); } serializer.config(SerializerFeature.WriteDateUseDateFormat, true); if (dateFormat != null) { serializer.setDateFormat(dateFormat); } serializer.write(object); return out.toString(); } finally { out.close(); } }