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_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 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 static final String toJSONString(Object object, SerializeConfig config, SerializeFilter filter, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out, config); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } setFilter(serializer, filter); serializer.write(object); return out.toString(); } finally { out.close(); } }
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_point() throws Exception { JSONSerializer serializer = new JSONSerializer(); Assert.assertEquals(AwtCodec.class, serializer.getObjectWriter(Point.class).getClass()); Point point = new Point(3, 4); String text = JSON.toJSONString(point, SerializerFeature.WriteClassName); System.out.println(text); Object obj = JSON.parse(text); Point point2 = (Point) obj; Assert.assertEquals(point, point2); Point point3 = (Point) JSON.parseObject(text, Point.class); Assert.assertEquals(point, point3); }
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 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_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_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.0}", text); }
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() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { Assert.assertTrue(value instanceof Character); 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_writer_1() throws Exception { SerializeWriter out = new SerializeWriter(14); out.config(SerializerFeature.QuoteFieldNames, true); try { JSONSerializer serializer = new JSONSerializer(out); VO vo = new VO(); vo.setValue("#"); serializer.write(vo); Assert.assertEquals("{\"value\":\"#\"}", out.toString()); } finally { out.close(); } }
public void test_2() throws Exception { SerializeWriter out = new SerializeWriter(); ListSerializer listSerializer = new ListSerializer(); LinkedList<Object> list = new LinkedList<Object>(); for (int i = 0; i < 100000; i++) { list.add(i); } long start = System.currentTimeMillis(); listSerializer.write(new JSONSerializer(out), list, null, null, 0); long end = System.currentTimeMillis(); System.out.println("linkedList time: " + (end - start)); }
public void test_0() throws Exception { JSONSerializer serializer = new JSONSerializer(); serializer.setDateFormat("yyyy"); Assert.assertEquals("yyyy", ((SimpleDateFormat) serializer.getDateFormat()).toPattern()); Assert.assertEquals("yyyy", serializer.getDateFormatPattern()); serializer.setDateFormat("yyyy-MM"); Assert.assertEquals("yyyy-MM", ((SimpleDateFormat) serializer.getDateFormat()).toPattern()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); format.setTimeZone(JSON.defaultTimeZone); serializer.setDateFormat(format); Assert.assertEquals("yyyy-MM-dd", serializer.getDateFormatPattern()); serializer.close(); }
public static final String toJSONString(Object object, SerializeFilter[] filters, 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.config(SerializerFeature.WriteDateUseDateFormat, true); setFilter(serializer, filters); 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")) { Assert.assertTrue(value instanceof Long); 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() 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_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_1() throws Exception { PropertyFilter filter = new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if ("id".equals(name)) { Assert.assertTrue(value instanceof Long); 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_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_writer_1() throws Exception { StringWriter strOut = new StringWriter(); SerializeWriter out = new SerializeWriter(strOut, 14); out.config(SerializerFeature.BrowserCompatible, true); try { JSONSerializer serializer = new JSONSerializer(out); VO vo = new VO(); vo.setValue("abcd\t"); serializer.write(vo); } finally { out.close(); } Assert.assertEquals("{value:\"abcd\\t\"}", strOut.toString()); }
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.name = "chennp2008"; serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"name\":\"chennp2008\"}", text); }
public void test_3_s() throws Exception { SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); StringBuilder sb = new StringBuilder(); sb.append('['); int len = 1000; Integer[] array = new Integer[len]; for (int i = 0; i < array.length; ++i) { array[i] = i; if (i != 0) { sb.append(','); } sb.append(i); } sb.append(']'); serializer.write(array); Assert.assertEquals(sb.toString(), out.toString()); }
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_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); }
/** * @since 1.1.14 */ public static final 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(); } }
public void test_4() throws Exception { SerializeWriter out = new SerializeWriter(); Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("TOP", "value"); map.put("bytes", new byte[] { 1, 2 }); MapSerializer mapSerializer = new MapSerializer(); mapSerializer.write(new JSONSerializer(out), map, null, null, 0); String text = out.toString(); Assert.assertEquals("{\"TOP\":\"value\",\"bytes\":\"AQI=\"}", text); JSONObject json = JSON.parseObject(text); byte[] bytes = json.getBytes("bytes"); Assert.assertEquals(1, bytes[0]); Assert.assertEquals(2, bytes[1]); Assert.assertEquals(2, bytes.length); }
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); }
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) { if (filter != null) { if (filter instanceof PropertyPreFilter) { serializer.getPropertyPreFilters().add((PropertyPreFilter) filter); } if (filter instanceof NameFilter) { serializer.getNameFilters().add((NameFilter) filter); } if (filter instanceof ValueFilter) { serializer.getValueFilters().add((ValueFilter) filter); } if (filter instanceof PropertyFilter) { serializer.getPropertyFilters().add((PropertyFilter) filter); } if (filter instanceof BeforeFilter) { serializer.getBeforeFilters().add((BeforeFilter) filter); } if (filter instanceof AfterFilter) { serializer.getAfterFilters().add((AfterFilter) filter); } } }
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_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); Bean a = new Bean(); serializer.write(a); String text = out.toString(); Assert.assertEquals("{\"id\":\"AAA\"}", text); }
@SuppressWarnings("rawtypes") public void test_writer_1() throws Exception { StringWriter strOut = new StringWriter(); SerializeWriter out = new SerializeWriter(strOut, 1); try { JSONSerializer serializer = new JSONSerializer(out); Map map = Collections.singletonMap("", "a"); serializer.write(map); } finally { out.close(); } Assert.assertEquals("{\"\":\"a\"}", strOut.toString()); }
public void test_6() throws Exception { JSONSerializer serializer = new JSONSerializer(new SerializeWriter()); serializer.config(SerializerFeature.QuoteFieldNames, false); Assert.assertEquals(false, serializer.isEnabled(SerializerFeature.QuoteFieldNames)); serializer.config(SerializerFeature.UseSingleQuotes, true); Assert.assertEquals(true, serializer.isEnabled(SerializerFeature.UseSingleQuotes)); serializer.write(Collections.singletonMap("a'ge", 33)); Assert.assertEquals("{'a\\'ge':33}", serializer.getWriter().toString()); }