FastJson add mark hidden characters

What we need to do is to encrypt sensitive information when printing based on the log. In simple terms. For example, your ID number is 145267267889297929X, so that 1452******29X is displayed when the log is printed.
In order to meet the needs, I studied Ali's fastjson.
fastjson has a high degree of controllability and can customize serialization by itself, such as annotation processing
1. Customize serialization
through @JSONField 2. Customize serialization through @JSONType
Specific use:
@JSONField(serialize=false) //Do not serialize fields
@JSONField(name="usname") //Modify the property value of the serialized output
@JSONType(ignores ={"id", "sex"}) //Modify on the class
@JSONType(includes={"name","sex"}) //What fields are included in serialization

The annotation is relatively simple to use, but it is not the effect I want, and the annotation needs to be parsed during serialization, and the performance is not very good. So I am using SerializeFilter custom processing here.
   protected List<BeforeFilter>       beforeFilters       = null;
    protected List<AfterFilter>        afterFilters        = null;
    protected List<PropertyFilter>     propertyFilters     = null;
    protected List<ValueFilter>        valueFilters        = null;
    protected List<NameFilter>         nameFilters         = null;
    protected List<PropertyPreFilter>  propertyPreFilters  = null;
    protected List<LabelFilter>        labelFilters        = null;
    protected List<ContextValueFilter> contextValueFilters = null;
//fastjson provides a variety of SerializeFilter: in JSON.toJSONString
PropertyPreFilter: Determine whether to serialize according to PropertyName;
PropertyFilter: Determine whether to serialize according to PropertyName and PropertyValue;
NameFilter: Modify the Key, if you need to modify the Key, the process return value can be;
ValueFilter: Modify Value;
BeforeFilter: Add content to the front during serialization;
AfterFilter: Add content at the end when serializing;
LabelFilter: rewrite

Create the class where we need to use the validation
//Ignore set, get operations
class Lists {
    public String name;

    public Lists(String name) {
        this.name = name;
    }
}

class Logger {
    private String name;
    private String idNum;
    private Lists name1=new Lists("333");

    public Logger(String name, String idNum) {
        this.name = name;
        this.idNum = idNum;
    }
}


PropertyPreFilter: Determine whether to serialize according to PropertyName;
when using it, we use SimplePropertyPreFilter, otherwise it will be no different from PropertyFilter
//Look at the source code first, we see that the excludes attribute is not used, so we can only configure the attributes that need to be serialized
public class SimplePropertyPreFilter implements PropertyPreFilter {
    private final Class<?>    clazz;
    private final Set<String> includes = new HashSet<String>();
    private final Set<String> excludes = new HashSet<String>();
    private int               maxLevel = 0;

    public SimplePropertyPreFilter(String... properties){
        this(null, properties);
    }

    public SimplePropertyPreFilter(Class<?> clazz, String... properties){
        super();
        this.clazz = clazz;
        for (String item : properties) {
            if (item != null) {
                this.includes.add(item);
            }
        }

...
// execute code
 System.out.println(LogSecurity.toJSONString(new Logger("Janle", "1872871278"), new SimplePropertyPreFilter(Logger.class,"name")));
//Results of the
{"name":"Janle"}


PropertyFilter: Specifying properties for serialization
I simply did a process that will only output the serialization corresponding to the name.
//If you want to output the name of the list, you can see it yourself, the value needs to be judged.
System.out.println(LogSecurity.toJSONString(new Logger("Janle", "1872871278"), new PropertyFilter() {
            @Override
            public boolean apply(Object object, String name, Object value) {
                return name.equals("name")?true:false;
            }
        }));
//Results of the
{"name":"Janle"}

NameFilter: It is to modify the properties of the object after serialization.
ValueFilter: To modify the value during serialization, I just need to use this. The main processing is as follows
/**
 * Mainly for serialization of log output.
 * Created by lijianzhen1 on 2017/9/5.
 */
public class LogMarkFilter implements ValueFilter {
    /**
     * Add tool class for mark
     */
    private final WebMarkUtils markUtils;
    /**
     * Corresponding to the required properties
     */
    private Map<String, Boolean> params = Maps.newHashMap();

    public LogMarkFilter(String... params) {
        this.markUtils = new WebMarkUtils();
        for (String param : params)
            this.params.put(param, Boolean.TRUE);
    }

    @Override
    public Object process(Object object, String name, Object value) {
        if (null != params.get(name)
                && params.get(name)
                && value instanceof String) {
            //Execute the mark operation
            return markUtils.getMarkInfo(value.toString());
        }
        return value;
    }
}


BeforeFilter: Add content to the front during serialization;
System.out.println(LogSecurity.toJSONString(new Logger("Janle", "1872871278"), new BeforeFilter() {
            @Override
            public void writeBefore(Object object) {
                writeKeyValue("aaa","333");
            }
        }));
The result after execution adds a fixed attribute to each object of json
{"aaa":"333","idNum":"1872871278","name":"Janle","name1":{"aaa":"333","name":"333"}}


AfterFilter: Add content at the end when serializing; same as above,


LabelFilter: When serializing, the corresponding annotation will be found.
We add the value of the corresponding label to the field System.out.println(JSONArray.toJSONString(new
Logger("Janle", "1872871278"), Labels.includes("name"))); @JSONField(label = "name")
The execution result is what we want

ContextValueFilter: You can define some annotations to be parsed according to the annotation method and yourself, look at the source code:
Object process(BeanContext context, Object object, String name, Object value);

There is one more context in it, you can get all the properties of the corresponding bean, and you can also find the fields you want for processing.
You can write a simple test yourself.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326876110&siteId=291194637