public void test_4() throws Exception { PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if ("name".equals(name)) { return false; } return true; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getPropertyFilters().add(filter); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", 3); map.put("name", "chennp2008"); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"id\":3}", text); }
public void test_valuefilter_1() throws Exception { ValueFilter filter = new ValueFilter() { public Object process(Object source, String name, Object value) { if (name.equals("name")) { return "AAA"; } return value; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getValueFilters().add(filter); Bean a = new Bean(); serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"id\":false,\"name\":\"AAA\"}", text); }
public void test_namefilter_1() 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); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", (byte) 0); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":0}", text); }
public void test_bean_3() throws Exception { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", 123); PO vo = TypeUtils.castToJavaBean(map, PO.class); Assert.assertEquals(123, vo.id); SerializeWriter out = new SerializeWriter(); try { SerializeConfig config = new SerializeConfig(); JSONSerializer serializer = new JSONSerializer(out, config); config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", (String) null))); serializer.write(vo); Assert.assertEquals("{}", out.toString()); } finally { out.close(); } }
public static final String toJSONString(Object object, SerializeFilter[] filters, 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); setFilter(serializer, filters); serializer.write(object); String serializeWriter = out.toString(); return serializeWriter; } finally { out.close(); } }
public void test_0() throws Exception { PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { return false; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getPropertyFilters().add(filter); A a = new A(); serializer.write(a); String text = out.toString(); Assert.assertEquals("{}", text); }
public void test_namefilter_1() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { Assert.assertTrue(value instanceof Short); return "ID"; } return name; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getNameFilters().add(filter); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", (short) 0); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":0}", text); }
public void test_1() throws Exception { PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if ("id".equals(name)) { Assert.assertTrue(value instanceof Short); return true; } return false; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getPropertyFilters().add(filter); A a = new A(); serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"id\":0}", text); }
public void test_0() throws Exception { StringWriter out = new StringWriter(); SerializeWriter writer = new SerializeWriter(out, 10); Assert.assertEquals(10, writer.getBufferLength()); for (int ch = 'a'; ch <= 'z'; ++ch) { writer.write(ch); } writer.close(); String text = out.toString(); Assert.assertEquals(26, text.length()); for (int i = 0; i < 26; ++i) { Assert.assertEquals(text.charAt(i), (char)('a' + i)); } }
public void test_1() throws Exception { PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if ("id".equals(name)) { Assert.assertTrue(value instanceof Float); return true; } return false; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getPropertyFilters().add(filter); A a = new A(); serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"id\":0.0}", text); }
public void test_0() throws Exception { StringWriter out = new StringWriter(); SerializeWriter writer = new SerializeWriter(out, 10); Assert.assertEquals(10, writer.getBufferLength()); for (char ch = 'a'; ch <= 'z'; ++ch) { char[] chars = new char[] { ch, ch, ch }; writer.write(chars, 0, chars.length); } writer.close(); String text = out.toString(); Assert.assertEquals(26 * 3, text.length()); for (int i = 0; i < 26; ++i) { Assert.assertEquals(text.charAt(i * 3), (char) ('a' + i)); Assert.assertEquals(text.charAt(i * 3 + 1), (char) ('a' + i)); Assert.assertEquals(text.charAt(i * 3 + 2), (char) ('a' + i)); } }
public void test_namefilter_1() 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); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", 0); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":0}", text); }
public void test_valuefilter() throws Exception { ValueFilter filter = new ValueFilter() { public Object process(Object source, String name, Object value) { if (name.equals("id")) { return "AAA"; } return value; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getValueFilters().add(filter); A a = new A(); serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"id\":\"AAA\"}", text); }
public void test_2() throws Exception { PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if ("name".equals(name)) { return true; } return false; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getPropertyFilters().add(filter); A a = new A(); a.setName("chennp2008"); serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"name\":\"chennp2008\"}", text); }
public static final byte[] toJSONBytes(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.toBytes("UTF-8"); } finally { out.close(); } }
public static final String toJSONString(Object object, SerializeFilter filter, 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); setFilter(serializer, filter); serializer.write(object); String serializeWriter = out.toString(); return serializeWriter; } finally { out.close(); } }
public void test_namefilter_1() 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); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", true); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":true}", text); }
public void test_3() throws Exception { PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if ("name".equals(name)) { return true; } return false; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getPropertyFilters().add(filter); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "chennp2008"); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"name\":\"chennp2008\"}", text); }
public static final String toJSONString(Object object) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); serializer.config(SerializerFeature.SortField, false); serializer.config(SerializerFeature.UseSingleQuotes, true); serializer.write(object); return out.toString(); } catch (StackOverflowError e) { throw new JSONException("maybe circular references", e); } finally { out.close(); } }
public void test_valuefilter_2() throws Exception { ValueFilter filter = new ValueFilter() { public Object process(Object source, String name, Object value) { if (name.equals("name")) { return "AAA"; } return value; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getValueFilters().add(filter); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", null); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"name\":\"AAA\"}", text); }
public void test_direct() throws Exception { SerializeWriter out = new SerializeWriter(6); out.config(SerializerFeature.QuoteFieldNames, true); try { JSONSerializer serializer = new JSONSerializer(out); VO vo = new VO(); vo.setValue(123456789); serializer.write(vo); Assert.assertEquals("{\"value\":123456789}", out.toString()); } finally { out.close(); } }
public void test_valuefilter_3() throws Exception { ValueFilter filter = new ValueFilter() { public Object process(Object source, String name, Object value) { if (name.equals("name")) { return null; } return value; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getValueFilters().add(filter); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "AA"); serializer.write(map); String text = out.toString(); Assert.assertEquals("{}", text); }
public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { Assert.assertTrue(value instanceof Integer); 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); }
public void test_valuefilter_1() throws Exception { ValueFilter filter = new ValueFilter() { public Object process(Object source, String name, Object value) { if (name.equals("name")) { return "AAA"; } return value; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getValueFilters().add(filter); A a = new A(); serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"id\":0,\"name\":\"AAA\"}", text); }
public String encode(Object object) throws Exception { SerializeConfig config = new SerializeConfig(); SerializeWriter out = new SerializeWriter(); out.config(SerializerFeature.DisableCircularReferenceDetect, true); // out.config(SerializerFeature.DisableCheckSpecialChar, true); JSONSerializer serializer = new JSONSerializer(out, config); serializer.write(object); String text = out.toString(); out.close(); return text; }
public void test_6() throws Exception { SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.config(SerializerFeature.UseISO8601DateFormat, true); Assert.assertEquals(true, serializer.isEnabled(SerializerFeature.UseISO8601DateFormat)); serializer.write(new Date(1294502460000L)); Assert.assertEquals("\"2011-01-09T00:01:00+08:00\"", out.toString()); }
public void test_14() throws Exception { SerializeWriter out = new SerializeWriter(4); out.config(SerializerFeature.UseSingleQuotes, false); out.writeString("1\""); Assert.assertEquals("\"1\\\"\"", out.toString()); }
public String toJSONString() { SerializeWriter out = new SerializeWriter(); try { new JSONSerializer(out).write((Object) this); String serializeWriter = out.toString(); return serializeWriter; } finally { out.close(); } }
public void test_0() throws Exception { SerializeWriter writer = new SerializeWriter(); writer.append('A'); writer.writeInt(156); Assert.assertEquals("A156", writer.toString()); writer.writeLong(345); Assert.assertEquals("A156345", writer.toString()); }
public void test_date_4() throws Exception { SerializeWriter out = new SerializeWriter(); SerializeConfig config = new SerializeConfig(); JSONSerializer serializer = new JSONSerializer(out, config); SimpleDateFormat format = new SimpleDateFormat("yyyy"); format.setTimeZone(JSON.defaultTimeZone); serializer.write(new VO(format)); Assert.assertEquals("{\"format\":\"yyyy\"}", out.toString()); JSON.parseObject(out.toString(), VO.class); }
@Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { Model model = (Model) object; SerializeWriter out = serializer.getWriter(); out.writeFieldValue('{', "ID", model.id); out.write('}'); }
public void test_2_s() throws Exception { SerializeWriter out = new SerializeWriter(); CollectionCodec listSerializer = new CollectionCodec(); List<Object> list = new ArrayList<Object>(); list.add(1); list.add(2); listSerializer.write(new JSONSerializer(out), list, null, null, 0); Assert.assertEquals("[1,2]", out.toString()); }
@Override public String toString(Object value, Object meta, boolean pretty, boolean insertMeta) { return toString(value, meta, insertMeta, (input) -> { final SerializeWriter out = new SerializeWriter((Writer) null, JSON.DEFAULT_GENERATE_FEATURE, pretty ? prettyFeatures : normalFeatures); final JSONSerializer writer = new JSONSerializer(out); if (Config.USE_TIMESTAMPS) { writer.setDateFormat(Config.TIMESTAMP_FORMATTER); } writer.write(input); final String json = out.toString(); writer.close(); return json; }); }