JSONシーケンス化keyソート問題とシーケンス化大文字と小文字の問題


1.JSONシーケンス化keyソート問題(fastjson)
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
//      
Student student = new Student();
student.setName("  ");
student.setSex(1);
student.setAge(18);
//    json key     
System.out.println(JSONObject.toJSONString(student ,SerializerFeature.MapSortField));
//     key age
 System.out.println(JSONObject.toJSONString(student , new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                if ("age".equals(name)) {
                    return false;
                }
                return true;
            }
        }, SerializerFeature.MapSortField));

2.JSONシーケンス化大文字と小文字の問題(fastjson)
//   
import com.alibaba.fastjson.annotation.JSONField;
public class Student {
    private String name;
    private Integer sex;
    private Integer age;
    @JSONField(name = "Name")		//      json,key Name
    public String getName() {
        return name;
    }
    @JSONField(name = "Name")		////  json(Name)         
    public void setName(String name) {
        this.name = name;
    }
    public Integer getSex() {
        return sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

3.jacksonシーケンス化大文字と小文字の問題(@ResponseBodyと@RequestBodyでのシーケンス化と逆シーケンス化はjackson)
 //   
import com.fasterxml.jackson.annotation.JsonProperty;
public class Student {
    @JsonProperty("Name")
    private String name;
    private Integer sex;
    private Integer age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getSex() {
        return sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

//     
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
@Test
public void test() throws Exception{
	 Student student = new Student();
	 student.setName("  ");
	 ObjectMapper MAPPER = new ObjectMapper();
	 //jackson   
	 String json = MAPPER.writeValueAsString(student);
	 System.out.println(json);
	 //jackson    
	 Student student2 = MAPPER.readValue(json, Student.class);
	 System.out.println(student2.getName());
}

4.jacksonシーケンス化null値の問題(fastjsonシーケンス化のデフォルトではnull値のキー値ペアが削除されます)
//         
   :(       )
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
   :
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)

5.jackson逆シーケンス化余分なjsonフィールドを無視
import com.fasterxml.jackson.databind.DeserializationFeature;
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

6.jacksonシーケンス化余分なjsonフィールドを無視
   :
@JsonIgnoreProperties:               json  。       ,    albums dataset   “tag”  ;
@JsonIgnoreProperties({ "tags" })

   :
@JsonIgnore:                   ;get   
@JsonIgnore 

7.jackson常用注記
@JsonAlias("Name")     		       
private String name;
@JsonProperty("Name")		            
private String name;