Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Lấy cài đặt JsonGenerator dùng Jackson (Java)

JsonGenerator ghi JSON streaming. Settings trong enum JsonGenerator.Feature. Kiểm tra qua isEnabled(feature).

Cú pháp

public static enum JsonGenerator.Feature extends Enum<JsonGenerator.Feature>
boolean isEnabled(JsonGenerator.Feature feature)

Ví dụ liệt kê settings

import com.fasterxml.jackson.core.*;
import java.io.*;

public class JsonGeneratorSettingsTest {
    public static void main(String[] args) throws IOException {
        StringWriter writer = new StringWriter();
        JsonFactory factory = new JsonFactory();
        JsonGenerator generator = factory.createGenerator(writer);
        
        System.out.println("JsonGenerator Default Settings:");
        for (JsonGenerator.Feature feature : JsonGenerator.Feature.values()) {
            System.out.println(feature.name() + ": " + generator.isEnabled(feature));
        }
        generator.close();
    }
}

Output (Jackson 2.15 defaults)

AUTO_CLOSE_TARGET: true
AUTO_CLOSE_JSON_CONTENT: true
FLUSH_PASSED_TO_STREAM: true
QUOTE_FIELD_NAMES: true
QUOTE_NON_NUMERIC_NUMBERS: true
ESCAPE_NON_ASCII: false
WRITE_NUMBERS_AS_STRINGS: false
WRITE_BIGDECIMAL_AS_PLAIN: false
STRICT_DUPLICATE_DETECTION: false
IGNORE_UNKNOWN: false

Giải thích các Feature quan trọng

Feature Mặc định Mô tả
AUTO_CLOSE_TARGET true Tự đóng OutputStream/Writer khi generator.close()
AUTO_CLOSE_JSON_CONTENT true Tự close JSON content (array/object) khi generator.close()
FLUSH_PASSED_TO_STREAM true Flush underlying stream khi generator.flush()
QUOTE_FIELD_NAMES true Quote field name: {"name":...} vs {name:...}
QUOTE_NON_NUMERIC_NUMBERS true Quote non-numeric number: NaN, Infinity → "NaN"
ESCAPE_NON_ASCII false Escape non-ASCII char thành \uXXXX (bật để ASCII-only output)
WRITE_NUMBERS_AS_STRINGS false Ghi tất cả number dưới dạng string: 123 → "123"
WRITE_BIGDECIMAL_AS_PLAIN false Ghi BigDecimal không scientific notation (plain)
STRICT_DUPLICATE_DETECTION false Throw exception nếu write duplicate field name
IGNORE_UNKNOWN false Ignore unknown type khi serialize (fallback toString)

Tùy chỉnh Generator Settings

JsonFactory factory = new JsonFactory()
    .enable(JsonGenerator.Feature.ESCAPE_NON_ASCII)      // ASCII-safe output
    .enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN) // BigDecimal plain
    .disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);   // Không auto-close stream

// Hoặc trên ObjectMapper
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);

Use case thường gặp

  • ESCAPE_NON_ASCII=true: Legacy system chỉ hỗ trợ ASCII, XML embedding.
  • WRITE_BIGDECIMAL_AS_PLAIN=true: Financial data, tránh scientific notation (1E-10).
  • QUOTE_FIELD_NAMES=false: Non-standard JSON (JavaScript object literal), ít dùng.
  • STRICT_DUPLICATE_DETECTION=true: Debug duplicate field bug.

Lưu ý

  • Settings này cho **streaming write**. Data binding (POJO→JSON) dùng SerializationFeature trên ObjectMapper.
  • ObjectMapper delegate to shared JsonFactory → config factory ảnh hưởng global.
  • Tạo JsonFactory riêng nếu cần config khác cho từng use case.