OldCurmudgeon
OldCurmudgeon

Reputation: 65793

Change field case with an ObjectMapper

I think I need to create a specialist ObjectMapper and cannot find any sample code to start the process.

The creator of the JSON is using .Net and public properties and therefore uses field names with an uppercase initial. I am parsing the JSON into POJOs so I would like to use a lowercase initial.

At their end:

    public class Facet
    {
        public string Name { get; set; }
        public string  Value { get; set; }
    }

At my end I must therefore have:

    public class Facet {
        public String Name;
        public String Value;
    }

I would much prefer:

    public class Facet {
        public String name;
        public String value;
    }

Am I right that this could be done with an ObjectMapper?

Upvotes: 22

Views: 39333

Answers (5)

6rzes
6rzes

Reputation: 11

Since v 2.13 use builder:

XmlMapper xmlMapper = (XmlMapper) getObjectMapper();

private ObjectMapper getObjectMapper() {
    return XmlMapper.builder()
        .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
        .build();
}

Upvotes: 0

dekay
dekay

Reputation: 1

Just a quick update as I was looking for same answer and a code snippet objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

Upvotes: 0

madx
madx

Reputation: 7203

This problem could be solved from Jackson 2.5.0 like this:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

From the javadoc:

com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES

Feature that will allow for more forgiving deserialization of incoming JSON. If enabled, the bean properties will be matched using their lower-case equivalents, meaning that any case-combination (incoming and matching names are canonicalized by lower-casing) should work.

Note that there is additional performance overhead since incoming property names need to be lower-cased before comparison, for cases where there are upper-case letters. Overhead for names that are already lower-case should be negligible however.

Feature is disabled by default.

Since: 2.5

Upvotes: 8

Programmer Bruce
Programmer Bruce

Reputation: 66935

Instead of annotating each field, the Jackson ObjectMapper can be configured to use a built-in or custom PropertyNamingStrategy, to apply a consistent translation between Java property/field names and JSON element names.

For example:

myObjectMapper.setPropertyNamingStrategy(PascalCaseStrategy);

Upvotes: 15

pb2q
pb2q

Reputation: 59607

Your first issue can be addressed very simply with the @JsonProperty annotation:

// java-side class
public class Facet
{
    @JsonProperty("Name")
    public String name;

    @JsonProperty("Value")
    public String value;
}

Now the ObjectMapper will match up the differently-cased field names. If you don't want to add annotations into your classes, you can create a Mix-in class to stand in for your Facet:

public class FacetMixIn
{
    @JsonProperty("Name")
    public String name;

    @JsonProperty("Value")
    public String value;
}

objectMapper.getDeserializationConfig().addMixInAnnotations(Facet.class, FacetMixIn.class);

This will achieve the same thing, without requiring additional annotations in your Facet class.

Upvotes: 28

Related Questions