---
title: "FastJson添加mark隐藏字符"
author: "Janle"
date: "2017-09-05T16:46:00Z"
tags: []
categories: ["编程语言"]
slug: "fastjson-mark"
draft: false
lastmod: "2017-09-05T16:46:00Z"
id: 2392230
url: "https://janle.iteye.com/blog/2392230"
---

我们要做的是一个基于日志打印时候需要对敏感信息进行加密。简单的来说。就是比如你的身份证号码是145267267889297929X，让在日志打印时候显示1452******29X这个样子。
为了满足需求研究了下阿里的fastjson。
fastjson具有高度的可控性，可以自己定制序列化，比如当中有注解处理的
1.通过@JSONField定制序列化
2.通过@JSONType定制序列化
具体的使用：

```java
@JSONField(serialize=false) //不进行序列化的字段
@JSONField(name="usname") //修改序列化的输出的属性值
@JSONType(ignores ={"id", "sex"}) //在类上修饰
@JSONType(includes={"name","sex"}) //序列化包括哪些字段

```

注解在使用时候比较简单，但是不是我要的效果，并且注解在序列化时候需要解析注解，性能不是很好。所以我这里使用的是SerializeFilter定制处理。

```java

   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提供了多种SerializeFilter：在JSON.toJSONString
PropertyPreFilter： 根据PropertyName判断是否序列化；
PropertyFilter： 根据PropertyName和PropertyValue来判断是否序列化；
NameFilter： 修改Key，如果需要修改Key,process返回值则可；
ValueFilter： 修改Value；
BeforeFilter： 序列化时在最前添加内容；
AfterFilter： 序列化时在最后添加内容；
LabelFilter：重新写

```


```java
创建我们需要使用验证的类
//忽略set，get操作
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： 根据PropertyName判断是否序列化；
使用的时候我们使用SimplePropertyPreFilter，不然就和PropertyFilter没有什么区别了

```java

//先看看源码，我们看见excludes属性就没有使用，所有我们只能配置需要序列化的属性
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);
            }
        }

...
//执行代码
 System.out.println(LogSecurity.toJSONString(new Logger("Janle", "1872871278"), new SimplePropertyPreFilter(Logger.class,"name")));
//执行结果
{"name":"Janle"}

```


PropertyFilter：指定属性进行序列化
我简单做了一个处理，将只输出对应name的序列化。

```java

//如果想输出lists的name自己可以看看，值需要做个判断就可以。
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;
            }
        }));
//执行结果
{"name":"Janle"}

```

NameFilter ：就是修改对象的属性序列化后的显示
ValueFilter ：序列化时修改value，我就是需要用到这个,主要做了如下处理

```java
/**
 * 主要是对日志输出时候的序列化操作。
 * Created by lijianzhen1 on 2017/9/5.
 */
public class LogMarkFilter implements ValueFilter {
    /**
     * 添加mark的工具类
     */
    private final WebMarkUtils markUtils;
    /**
     * 对应需要的属性
     */
    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) {
            //执行mark操作
            return markUtils.getMarkInfo(value.toString());
        }
        return value;
    }
}

```


BeforeFilter： 序列化时在最前添加内容；

```java
 System.out.println(LogSecurity.toJSONString(new Logger("Janle", "1872871278"), new BeforeFilter() {
            @Override
            public void writeBefore(Object object) {
                writeKeyValue("aaa","333");
            }
        }));
执行后的结果在json的每个对象前添加固定的属性
{"aaa":"333","idNum":"1872871278","name":"Janle","name1":{"aaa":"333","name":"333"}}


```

AfterFilter： 序列化时在最后添加内容；同上，


LabelFilter：序列化时候会找到对应注解
我们字段上加上对应的label的值 System.out.println(JSONArray.toJSONString(new 
```java
Logger("Janle", "1872871278"), Labels.includes("name"))); @JSONField(label = "name") 
执行结果就是我们想要的

```

ContextValueFilter：这个就自己可自己按照注解方式和自己定义一些要解析的注解，看看源码：
 
```java
Object process(BeanContext context, Object object, String name, Object value);
```

里边比别的多了一个context，你可以获得对应bean的所有属性，你也可以找到你想要的字段做处理。
具体自己可以写个简单的试试。