Search in sources :

Example 1 with SerializerFeature

use of com.alibaba.fastjson.serializer.SerializerFeature 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 2 with SerializerFeature

use of com.alibaba.fastjson.serializer.SerializerFeature 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 3 with SerializerFeature

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

the class ByteArrayTest method test_bytes.

public void test_bytes() throws Exception {
    VO vo = new VO();
    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty };
    String text1 = JSON.toJSONString(vo, mapping, features);
    Assert.assertEquals("{\"value\":[]}", text1);
    String text2 = JSON.toJSONString(vo, features);
    Assert.assertEquals("{\"value\":[]}", text2);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature)

Example 4 with SerializerFeature

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

the class EnumTest2 method test_enum_noasm.

public void test_enum_noasm() throws Exception {
    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    Date date = new Date(1308841916550L);
    // 1308841916550
    Assert.assertEquals("1308841916550", JSON.toJSONString(date, mapping));
    // "2011-06-23T23:11:56.550"
    Assert.assertEquals("\"2011-06-23T23:11:56.550+08:00\"", JSON.toJSONString(date, mapping, SerializerFeature.UseISO8601DateFormat));
    SerializerFeature[] features = { SerializerFeature.UseISO8601DateFormat, SerializerFeature.UseSingleQuotes };
    // '2011-06-23T23:11:56.550'
    Assert.assertEquals("'2011-06-23T23:11:56.550+08:00'", JSON.toJSONString(date, mapping, features));
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) Date(java.util.Date)

Example 5 with SerializerFeature

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

the class ByteArrayTest method test_bytes_1.

public void test_bytes_1() throws Exception {
    VO vo = new VO();
    vo.setValue(new byte[] { 1, 2, 3 });
    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);
    SerializerFeature[] features = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty };
    String text1 = JSON.toJSONString(vo, mapping, features);
    Assert.assertEquals("{\"value\":\"AQID\"}", text1);
    String text2 = JSON.toJSONString(vo, features);
    Assert.assertEquals("{\"value\":\"AQID\"}", text2);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature)

Aggregations

SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)6 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)3 JSONException (com.alibaba.fastjson.JSONException)2 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)2 NameFilter (com.alibaba.fastjson.serializer.NameFilter)2 PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)2 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)2 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)2 Date (java.util.Date)2