Search in sources :

Example 1 with PropertyFilter

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

the class WriteNullStringAsEmptyTest method test_features.

public void test_features() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object object, String name, Object value) {
            if (value == null && object instanceof Model && "id".equals(name)) {
                return false;
            }
            return true;
        }
    };
    Model model = new Model();
    String json = JSON.toJSONString(model, filter, SerializerFeature.WriteNullStringAsEmpty);
    System.out.println(json);
}
Also used : PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter)

Example 2 with PropertyFilter

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

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

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

the class PropertyFilterClassLevelTest method test_0.

public void test_0() throws Exception {
    Object[] array = { new ModelA(), new ModelB() };
    SerializeConfig config = new SerializeConfig();
    //
    config.addFilter(//
    ModelA.class, new PropertyFilter() {

        @Override
        public boolean apply(Object object, String name, Object value) {
            return false;
        }
    });
    //
    config.addFilter(//
    ModelB.class, new PropertyFilter() {

        @Override
        public boolean apply(Object object, String name, Object value) {
            return true;
        }
    });
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{},{\"id\":1002}]", text2);
    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
}
Also used : SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter)

Example 5 with PropertyFilter

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

the class PropertyFilterTest method test_toJSONString.

public void test_toJSONString() throws Exception {
    PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            return false;
        }
    };
    Assert.assertEquals("{}", JSON.toJSONString(new A(), filter));
}
Also used : PropertyFilter(com.alibaba.fastjson.serializer.PropertyFilter)

Aggregations

PropertyFilter (com.alibaba.fastjson.serializer.PropertyFilter)60 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)54 SerializeWriter (com.alibaba.fastjson.serializer.SerializeWriter)54 HashMap (java.util.HashMap)20 JSONException (com.alibaba.fastjson.JSONException)2 NameFilter (com.alibaba.fastjson.serializer.NameFilter)2 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)2 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ArrayList (java.util.ArrayList)1