fastjson 직렬 화 시 null 필드 제거

2887 단어 자바

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jiepu.rpc.thrift.server.entity.User;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/1/5.
 */
public class TestFastJson {

    public static void main(String[] args){


        List list=new ArrayList<>();
        for(int i=0;i<3;i++)
        {
            User entity=new User();
            entity.setId((long) i);
            entity.setPassword("");
            entity.setUsername("    "+i);
            entity.setToken(UUID.randomUUID().toString());
            //list.add(entity);
            list.add(0,entity);
        }
        String json=JSON.toJSONString(list);
        System.out.println(json);
        /*
        QuoteFieldNames———-  key        ,   true
        WriteMapNullValue——–      null   ,   false
        WriteNullNumberAsZero—-       null,   0,  null
        WriteNullListAsEmpty—–List     null,   [],  null
        WriteNullStringAsEmpty—         null,   ”“,  null
        WriteNullBooleanAsFalse–Boolean     null,   false,  null
        */
        //     
        System.out.println(JSONObject.toJSONString(list, SerializerFeature.QuoteFieldNames));
        //    null   
        System.out.println(JSONObject.toJSONString(list, SerializerFeature.WriteMapNullValue));
        System.out.println(JSONObject.toJSONString(list, SerializerFeature.WriteNullNumberAsZero));
        System.out.println(JSONObject.toJSONString(new ArrayList<>(), SerializerFeature.WriteNullListAsEmpty));
        System.out.println(JSONObject.toJSONString(list, SerializerFeature.WriteNullStringAsEmpty));
        System.out.println(JSONObject.toJSONString(list, SerializerFeature.SortField));
    }
}

import com.alibaba.fastjson.annotation.JSONField;


public class User {

    private Long id;
    @JSONField(serialize = false)
    private String username;
    private String password;
    /**      */
    private String token;
    /**     */
    private String expired;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getExpired() {
        return expired;
    }

    public void setExpired(String expired) {
        this.expired = expired;
    }
}

좋은 웹페이지 즐겨찾기