0

I have a pretty standard Java POJO. Let's call it Dog:

Dog.java has a series of fields. In Java, convention says field names should start with a lower case. This means I have:

  • public String name;
  • public String dogType;
  • public String weight;

At the same time, I have a requirement on my JSON format that the JSON members (name, dogType, and weight) all be written with a capital letter at the beginning i.e. Name, DogType, and Weight.

I am using Jackson.

If I use the standard serialization code as below, I get JSON member names written exactly the same way they are in Java.

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jReq);
System.out.println(json);

To address my problem, I have 2 options:

  • either I use the Jackson annotation @JsonProperty(value="DogType"), or
  • I use a naming strategy as described in the API.

My question is: is there a clean way of doing it?

There are quite a few questions on SO about this that explain both ways:

But none go into the pros and cons.

My initial hunch is that I should go for the naming strategy. Is that a good approach or a bad one?

1 Answer 1

1

I think you should go for the naming strategy, too. The advantage of this one is that you can always override it with the annotations, if needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.