Fastjson API SerializeFilter


Introduction to Fastjson API SerializeFilter

SerializeFilter customizes serialization through programmatic extension. fastjson supports 6 types of SerializeFilter for customized serialization in different scenarios.

  1. PropertyPreFilter determines whether to serialize based on PropertyName
  2. PropertyFilter determines whether to serialize based on PropertyName and PropertyValue
  3. NameFilter Modifies the Key. If you need to modify the Key, the process returns the value Then you can
  4. ValueFilter Modify Value
  5. BeforeFilter Add content at the front during serialization
  6. AfterFilter Add content at the end during serialization

PropertyFilter determines whether to serialize based on PropertyName and PropertyValue

  public interface PropertyFilter extends SerializeFilter {
      boolean apply(Object object, String propertyName, Object propertyValue);
  }

It can be extended to determine whether serialization is required based on the object or attribute name or attribute value. For example:

  PropertyFilter filter = new PropertyFilter() {

        public boolean apply(Object source, String name, Object value) {
            if ("id".equals(name)) {
                int id = ((Integer) value).intValue();
                return id >= 100;
            }
            return false;
        }
    };

    JSON.toJSONString(obj, filter); // 序列化的时候传入filter

PropertyPreFilter determines whether to serialize based on PropertyName

is different from PropertyFilter and only determines based on object and name. Before calling getter, This avoids possible exceptions in getter calls.

   public interface PropertyPreFilter extends SerializeFilter {
      boolean apply(JSONSerializer serializer, Object object, String name);
  }

NameFilter Modifies Key during serialization

If you need to modify Key, process return value can

public interface NameFilter extends SerializeFilter {
    String process(Object object, String propertyName, Object propertyValue);
}

fastjson has a built-in PascalNameFilter, which is used to output Pascal style with the first character capitalized. For example:

import com.alibaba.fastjson.serializer.PascalNameFilter;

Object obj = ...;
String jsonStr = JSON.toJSONString(obj, new PascalNameFilter());

ValueFilter serialization is to modify Value

   public interface ValueFilter extends SerializeFilter {
      Object process(Object object, String propertyName, Object propertyValue);
  }

BeforeFilter serialization Add content at the front

Perform certain operations before serializing all properties of the object, such as calling writeKeyValue to add content

   public abstract class BeforeFilter implements SerializeFilter {
      protected final void writeKeyValue(String key, Object value) { ... }
      // 需要实现的抽象方法,在实现中调用writeKeyValue添加内容
      public abstract void writeBefore(Object object);
  }

AfterFilter adds content at the end when serializing

Performs certain operations after serializing all properties of the object, such as calling writeKeyValue to add content

    public abstract class AfterFilter implements SerializeFilter {
      protected final void writeKeyValue(String key, Object value) { ... }
      // 需要实现的抽象方法,在实现中调用writeKeyValue添加内容
      public abstract void writeAfter(Object object);
  }