JsonFactory tạo instance của parser/writer. Settings nằm trong enum JsonFactory.Feature.
Cú pháp
public static enum JsonFactory.Feature extends Enum<JsonFactory.Feature>
boolean isEnabled(JsonFactory.Feature feature)
Ví dụ liệt kê settings
import com.fasterxml.jackson.core.JsonFactory;
public class JsonFactorySettingsTest {
public static void main(String[] args) {
JsonFactory factory = new JsonFactory();
System.out.println("JsonFactory Default Settings:");
for (JsonFactory.Feature feature : JsonFactory.Feature.values()) {
boolean enabled = factory.isEnabled(feature);
System.out.println(feature.name() + ": " + enabled);
}
}
}
Output (Jackson 2.15 defaults)
INTERN_FIELD_NAMES: true
CANONICALIZE_FIELD_NAMES: true
FAIL_ON_SYMBOL_HASH_OVERFLOW: true
USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING: true
Giải thích các Feature
| Feature | Mặc định | Mô tả |
|---|---|---|
| INTERN_FIELD_NAMES | true | Intern field name string (deduplicate) để tiết kiệm memory & so sánh nhanh |
| CANONICALIZE_FIELD_NAMES | true | Chuẩn hóa field name (trùng với intern, legacy) |
| FAIL_ON_SYMBOL_HASH_OVERFLOW | true | Fail nếu symbol table overflow (bảo vệ DoS qua nhiều field name unique) |
| USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING | true | Dùng ThreadLocal recycle buffer cho parser/generator (performance) |
Tùy chỉnh JsonFactory
JsonFactory factory = new JsonFactory()
.disable(JsonFactory.Feature.INTERN_FIELD_NAMES) // Tắt interning nếu memory critical
.disable(JsonFactory.Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING); // Tắt thread-local
// Hoặc configure trên ObjectMapper (shared factory)
ObjectMapper mapper = new ObjectMapper();
mapper.getFactory().disable(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW);
Lưu ý
ObjectMappershareJsonFactoryinstance → config factory ảnh hưởng toàn bộ mapper.INTERN_FIELD_NAMESquan trọng cho performance khi parse nhiều JSON cùng structure (field name lặp lại).FAIL_ON_SYMBOL_HASH_OVERFLOWbảo vệ khỏi attack gửi JSON với hàng nghìn field name unique gây hash collision.- Thread-local buffer recycling: tắt nếu dùng trong môi trường thread-pool lớn gây memory leak (rare).