This may look a dumb question, but I would like to deserialize a JSON object to a POJO MyObject with a field named class, which is a reserved keyword in Java... If possible, I'd like to keep MyObject Jackson/Json/Whatever-dependency free.
Do I need to add a (standard or Jackson-specific) Java annotation to my class field member to handle this? Or is there another work-around to have a "pure" java object? I can configure the Jackson object mapper if needed.
public class MyObject {
public String field1;
public String class; // FAIL
}
Since class is a reserved Java keyword, you cannot use it directly. If you simply want the generated JSON to have it, use @JsonProperty and set its value attribute.
public class MyObject {
public String field1;
@JsonProperty(value = "class")
public String clazz;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With