ฉันกำลังพยายามสร้าง JSON ตามคลาส Object POJO ของฉัน สำหรับบางฟิลด์ ฉันต้องการใช้ CustomSerializer เนื่องจากฉันต้องการสร้างฟิลด์ตามความต้องการของฉัน ดังนั้นฉันจึงสร้าง CustomSerializer.class
CustomSerializer
จะถูกเรียกโดย 2 ฟิลด์ที่แตกต่างกันใน POJO
ของฉัน และฉันต้องการจัดการสิ่งต่าง ๆ ที่แตกต่างกันไปตามฟิลด์ที่กำลังโทร สำหรับฟิลด์ใดฟิลด์หนึ่ง (extensions
) ฉันต้องการให้ fieldName
และสำหรับฟิลด์อื่น (withoutExtensions
) ฉันไม่ต้องการให้ fieldname
ใน JSON ของฉัน
ปัญหาที่ฉันกำลังเผชิญอยู่คือเมื่อมีการเรียก CustomSerializer
ฉันจะได้รับ fieldname
เดียวกันสำหรับการโทรทั้งสองสาย เนื่องจากฉันไม่สามารถแยกความแตกต่างได้ว่าฟิลด์ใดกำลังเรียก CustomSerializer
อยู่ในขณะนี้
ตัวอย่างโค้ดต่อไปนี้จะให้ความชัดเจนมากขึ้นเกี่ยวกับปัญหาที่ฉันกำลังเผชิญ:
Customer
คลาส POJO ที่ใช้สำหรับซีเรียลไลซ์ JSON:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@NoArgsConstructor
public class Customer {
private String isA;
private String name;
@JsonSerialize(using = CustomSerializer.class)
private Map<String, Object> extensions = new HashMap<>();
private Map<String, Object> withoutExtensions = new HashMap<>();
@JsonAnyGetter
@JsonSerialize(using = CustomSerializer.class)
public Map<String, Object> getWithoutExtensions() {
return withoutExtensions;
}
}
ต่อไปนี้คือ CustomSerializer
ของฉันซึ่งจะถูกเรียกโดย 2 ฟิลด์ (ส่วนขยายและไม่มีส่วนขยาย) ในระหว่างการสร้าง JSON:
public class CustomSerializer extends JsonSerializer<Map<String, Object>> {
@Override
public void serialize(Map<String, Object> value, JsonGenerator gen, SerializerProvider serializers) {
//I would like to create the outer object for "Extensions" but do not want to create outer object for "WithoutExtensions"
System.out.println(gen.getOutputContext().getCurrentName());
//In my case for both "Extensions" and "WithoutExtensions" i get the "currentName" as "Extensions" how can I ensure which field is calling this sealizer at
// present
}
}
ต่อไปนี้เป็นคลาส Main
ของฉันซึ่งจะสร้าง JSON:
public class Main {
public static void main(String[] args) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
final Customer customer = new Customer();
customer.setName("Jackson");
Map<String, Object> extensions = new HashMap<>();
extensions.put("WithObject", "With");
customer.setExtensions(extensions);
Map<String, Object> withoutExtensions = new HashMap<>();
extensions.put("WithoutObject", "Without");
customer.setWithoutExtensions(withoutExtensions);
final String eventAsJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
System.out.println(eventAsJson);
}
}
ดังที่เราเห็นเมื่อฉันเรียกใช้แอปพลิเคชัน CustomSerializer
จะพิมพ์ extensions
ในทั้งสองกรณี ฉันเชื่อว่าควรพิมพ์ extensions
เพียงครั้งเดียว และในกรณีถัดไปควรระบุ withoutExtensions
หรือสตริงว่าง
ฉันแค่อยากทราบว่านี่เป็นข้อบกพร่องในส่วนของ Jackson หรือไม่ หรือมีวิธีแก้ไขใดที่ฉันสามารถลองแยกความแตกต่างได้ว่าฟิลด์ใดที่กำลังเรียกไปยัง CustomSerializer
ของฉัน
ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมจริงๆ ขอบคุณ.