Search in sources :

Example 11 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class FieldSerializerTest2 method toJSONString.

public static final String toJSONString(Object object, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.getPropertyFilters().add(new PropertyFilter() {

            public boolean apply(Object source, String name, Object value) {
                if ("id".equals(name)) {
                    return false;
                }
                return true;
            }
        });
        serializer.getNameFilters().add(new NameFilter() {

            public String process(Object source, String name, Object value) {
                if ("v".equals(name)) {
                    return "value";
                }
                return name;
            }
        });
        serializer.getValueFilters().add(new ValueFilter() {

            public Object process(Object source, String name, Object value) {
                if ("v".endsWith(name)) {
                    return "xxx";
                }
                return value;
            }
        });
        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
            serializer.config(feature, true);
        }
        serializer.write(object);
        return out.toString();
    } catch (StackOverflowError e) {
        throw new JSONException("maybe circular references", e);
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) NameFilter(com.alibaba.fastjson.serializer.NameFilter) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) JSONException(com.alibaba.fastjson.JSONException) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 12 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class FieldSerializerTest3 method toJSONString.

public static final String toJSONString(Object object, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();
    try {
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.getPropertyFilters().add(new PropertyFilter() {

            public boolean apply(Object source, String name, Object value) {
                if ("id".equals(name)) {
                    return false;
                }
                return true;
            }
        });
        serializer.getNameFilters().add(new NameFilter() {

            public String process(Object source, String name, Object value) {
                return name;
            }
        });
        serializer.getValueFilters().add(new ValueFilter() {

            public Object process(Object source, String name, Object value) {
                if ("v".endsWith(name)) {
                    return "xxx";
                }
                return value;
            }
        });
        for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
            serializer.config(feature, true);
        }
        serializer.write(object);
        return out.toString();
    } catch (StackOverflowError e) {
        throw new JSONException("maybe circular references", e);
    } finally {
        out.close();
    }
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) NameFilter(com.alibaba.fastjson.serializer.NameFilter) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) JSONException(com.alibaba.fastjson.JSONException) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter) ValueFilter(com.alibaba.fastjson.serializer.ValueFilter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 13 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class DateFormatSerializerTest method test_date_4.

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);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SimpleDateFormat(java.text.SimpleDateFormat) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 14 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class DateFormatSerializerTest method test_date_2.

public void test_date_2() throws Exception {
    SerializeWriter out = new SerializeWriter();
    SerializeConfig config = new SerializeConfig();
    JSONSerializer serializer = new JSONSerializer(out, config);
    serializer.config(SerializerFeature.WriteMapNullValue, true);
    serializer.write(new VO());
    Assert.assertEquals("{\"format\":null}", out.toString());
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Example 15 with SerializeWriter

use of com.alibaba.fastjson.serializer.SerializeWriter in project fastjson by alibaba.

the class JSONSerializerTest1 method test_0.

public void test_0() throws Exception {
    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    Assert.assertEquals(0, serializer.getNameFilters().size());
    Assert.assertEquals(0, serializer.getNameFilters().size());
    Assert.assertEquals(0, serializer.getValueFilters().size());
    Assert.assertEquals(0, serializer.getValueFilters().size());
    Assert.assertEquals(0, serializer.getPropertyFilters().size());
    Assert.assertEquals(0, serializer.getPropertyFilters().size());
    serializer.writeWithFormat("123", null);
}
Also used : SerializeWriter(com.alibaba.fastjson.serializer.SerializeWriter) JSONSerializer(com.alibaba.fastjson.serializer.JSONSerializer)

Aggregations

SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)344 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)205 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)54 HashMap (java.util.HashMap)47 StringWriter (java.io.StringWriter)38 NameFilter (com.alibaba.fastjson.serializer.NameFilter)36 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)19 ArrayList (java.util.ArrayList)12 ListSerializer (com.alibaba.fastjson.serializer.ListSerializer)11 MapSerializer (com.alibaba.fastjson.serializer.MapSerializer)10 JavaBeanSerializer (com.alibaba.fastjson.serializer.JavaBeanSerializer)8 Date (java.util.Date)8 Map (java.util.Map)8 CollectionCodec (com.alibaba.fastjson.serializer.CollectionCodec)6 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)6 LinkedHashMap (java.util.LinkedHashMap)6 JSONException (com.alibaba.fastjson.JSONException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 A (com.alibaba.json.bvt.serializer.filters.PropertyFilterTest.A)4 JSONObject (com.alibaba.fastjson.JSONObject)3