Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

วิธีควบคุมลำดับ Properties ใน JSON Serialization ด้วย Jackson @JsonPropertyOrder

Annotation @JsonPropertyOrder ใช้ระดับ class-level ระบุลำดับฟิลด์ที่จะปรากฏใน JSON output หลัง serialize ฟิลด์ที่ระบุใน annotation จะถูก serialize ก่อน (ตามลำดับที่กำหนด) ตามด้วยฟิลด์อื่นที่ไม่ได้ระบุ

ไวยากรณ์


public @interface JsonPropertyOrder

ตัวอย่างโค้ด


import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.*;
import java.io.*;

public class JsonPropertyOrderTest {
    public static void main(String[] args) throws Exception {
        Employee emp = new Employee();
        emp.setFirstName("Adithya");
        emp.setEmpId(25);
        emp.setLastName("Jai");
        emp.getTechnologies().add("Java");
        emp.getTechnologies().add("Scala");
        emp.getTechnologies().add("Python");
        
        ObjectMapper mapper = new ObjectMapper();
        mapper.writerWithDefaultPrettyPrinter().writeValue(System.out, emp);
    }
}

// Employee class
@JsonPropertyOrder({ "firstName", "lastName", "technologies", "empId" })
class Employee {
    private int empId;
    private String firstName;
    private String lastName;
    private List<String> technologies = new ArrayList<>();
    
    // Getters & Setters...
    public int getEmpId() { return empId; }
    public void setEmpId(int empId) { this.empId = empId; }
    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }
    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }
    public List<String> getTechnologies() { return technologies; }
    public void setTechnologies(List<String> technologies) { this.technologies = technologies; }
}

ผลลัพธ์


{
  "firstName" : "Adithya",
  "lastName" : "Jai",
  "technologies" : [ "Java", "Scala", "Python" ],
  "empId" : 125
}

สังเกตว่าลำดับเป็น: firstName → lastName → technologies → empId ตามที่กำหนดใน @JsonPropertyOrder

คุณสมบัติเพิ่มเติม

alphabetic = true (Jackson 2.12+)

จัดลำดับอัตโนมัติแบบ alphabetical สำหรับฟิลด์ที่ไม่ได้ระบุใน annotation


@JsonPropertyOrder(value = {"id", "name"}, alphabetic = true)
class MyClass { ... }

ร่วมกับ @JsonProperty (เปลี่ยนชื่อฟิลด์)


@JsonPropertyOrder({"firstName", "lastName", "empId"})
class Employee {
    @JsonProperty("employee_id")
    private int empId;
    // ...
}

JSON output จะใช้ชื่อ "employee_id" แต่ลำดับยังคงเป็น empId (ชื่อ property Java)

กรณีใช้งานจริง

  • API contract ต้องการลำดับฟิลด์เฉพาะ (legacy system, external API)
  • Human-readable JSON (log, config file) - ให้ฟิลด์สำคัญอยู่บน
  • Testing/Comparison - JSON ที่มีลำดับคงที่ง่ายต่อการ diff

ข้อควรระวัง

  • Jackson serialize ตามลำดับ property (getter) ไม่ใช่ field declaration order
  • ฟิลด์ที่ไม่ได้ระบุใน @JsonPropertyOrder จะตามหลัง (ลำดับไม่คงที่ - ขึ้นกับ JVM reflection)
  • ใช้ alphabetic = true เพื่อให้ลำดับที่เหลือมีความคงที่