com.alibaba.fastjson.serializer.NameFilter Java Examples

The following examples show how to use com.alibaba.fastjson.serializer.NameFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FastJsonUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenSerializeConfig_whenJavaObject_thanJsonCorrect() {
    NameFilter formatName = new NameFilter() {
        public String process(Object object, String name, Object value) {
            return name.toLowerCase()
                .replace(" ", "_");
        }
    };
    SerializeConfig.getGlobalInstance()
        .addFilter(Person.class, formatName);
    String jsonOutput = JSON.toJSONStringWithDateFormat(listOfPersons, "yyyy-MM-dd");
    assertEquals(jsonOutput, "[{\"first_name\":\"Doe\",\"last_name\":\"John\"," + "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":" + "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]");
    // resetting custom serializer
    SerializeConfig.getGlobalInstance()
        .put(Person.class, null);
}
 
Example #2
Source File: FastJsonProvider.java    From metrics with Apache License 2.0 4 votes vote down vote up
public String toJSONString(Object object, SerializeFilter filter, SerializerFeature[] features) {
    SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out, fastJsonConfig.serializeConfig);
        if (features != null) {
            for (SerializerFeature feature : features) {
                serializer.config(feature, true);
            }
        }

        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);
            }
        }

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}