JSON property file case insensitive in Jackson
You can use
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
This feature is in Jackson version 2.5.0 onwards.
Case Sensitive POJO to Json mapping Hybris
If you are using com.fasterxml.jackson.databind.ObjectMapper
, you can specify final name for each field using com.fasterxml.jackson.annotation.JsonProperty
for example:
@JsonProperty("AccountName")
private String AccountName;
Or you can “tell” to your mapper to use fields instead of getters for creating a final JSON. In order to do so you can just configure your mapper class as follows:
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
Case Insensitive for @JsonSubTypes
If you need to convert the type
value to a lowercased string you can convert the json file to a JsonNode
tree and after convert it to your pojo with the ObjectMapper#treeToValue
method:
JsonNode root = mapper.readTree(json);
((ObjectNode) root).put("type", root.get("type").asText().toLowerCase());
YourPojo yourPojo = mapper.treeToValue(root, YourPojo.class);
Jackson databind enum case insensitive
Jackson 2.9
This is now very simple, using jackson-databind
2.9.0 and above
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
// objectMapper now deserializes enums in a case-insensitive manner
Full example with tests
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
private enum TestEnum { ONE }
private static class TestObject { public TestEnum testEnum; }
public static void main (String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
try {
TestObject uppercase =
objectMapper.readValue("{ \"testEnum\": \"ONE\" }", TestObject.class);
TestObject lowercase =
objectMapper.readValue("{ \"testEnum\": \"one\" }", TestObject.class);
TestObject mixedcase =
objectMapper.readValue("{ \"testEnum\": \"oNe\" }", TestObject.class);
if (uppercase.testEnum != TestEnum.ONE) throw new Exception("cannot deserialize uppercase value");
if (lowercase.testEnum != TestEnum.ONE) throw new Exception("cannot deserialize lowercase value");
if (mixedcase.testEnum != TestEnum.ONE) throw new Exception("cannot deserialize mixedcase value");
System.out.println("Success: all deserializations worked");
} catch (Exception e) {
e.printStackTrace();
}
}
}
jooq + jackson fails to convert json to pojo due to case sensitivity
Fixing the Jackson mapping use-case
In your particular query, you could auto-alias all columns to their lower case equivalent, e.g.:
final var account =
dc.select(Stream
.of(ACCOUNT.fields())
.map(f -> f.as(f.getName().toLowerCase()))
.toList())
.from(ACCOUNT)
.limit(1)
.forJSON().path().withoutArrayWrapper()
.fetchOneInto(Account.class);
Avoiding the use-case entirely
However, given your particular query, I don't really see the point of passing through the JSON serialisation only to benefit from a third party mapper. Why not use jOOQ for that as well?
final var account =
dc.selectFrom(ACCOUNT)
.limit(1)
.fetchOneInto(Account.class);
Or, if Account
is an immutable class (e.g. a Java record), why not use type safe mapping like this:
final var account =
dc.selectFrom(ACCOUNT)
.limit(1)
.fetchOne(Records.mapping(Account::new));
Make JSON payload fields case insensitive when mapping to Java Object in REST API developed using SpringBoot
Well, if its Spring Boot application you can have this in your application.properties
file:spring.jackson.mapper.accept_case_insensitive_properties=true
or if you use yaml:
spring:
jackson:
mapper:
accept_case_insensitive_properties: true
Related Topics
How to Convert Words to a Number
Gwt: Timer and Scheduler Classes
Cannot Rerun Java JPAckage Installer If Already Installed, Second Time Just Exits Without Warning
In Java How to Re-Open System.In After Closing It
Configuration Using Annotation @Springbootapplication
How to Use Jersey as Jax-Rs Implementation Without Web.Xml
Java.Io.Streamcorruptedexception: Invalid Type Code: 00
How to Append a Node to an Existing Xml File in Java
Making Spring-Data-Mongodb Multi-Tenant
Are Two Java Objects with Same Hashcodes Not Necessarily Equal
How to Serialize an Object That Includes Bufferedimages
How to Extract Parameters from a Given Url
Removing All the Rows of Defaulttablemodel
How to Load Image File to Imageview