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?

Community
  • 1
  • 1
David Brossard
  • 13,584
  • 6
  • 55
  • 88

1 Answers1

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.

Danix
  • 1,947
  • 1
  • 13
  • 18