Jackson - converting java object to json - Need all key keys to upper case

寵の児 提交于 2019-12-01 07:16:37

There are multiple ways to do it with Jackson.

Annotations

You could annotate your object with @JsonProperty annotations on your fields or on your getter methods.

Example:

@JsonProperty("Name")
public final String name;

@JsonProperty("Location")
public String getLocation() {
  return location;
}

Implement JsonSerializableWithType interface

@Override
public void serialize(final JsonGenerator jG, final SerializerProvider p)
    throws IOException, JsonProcessingException
{
    serializeWithType(jG, p, null);
}

@Override
public void serializeWithType(final JsonGenerator jG, final SerializerProvider p, final TypeSerializer typeSer)
    throws IOException, JsonProcessingException
{
    // here you can do your own serialization
}

I would advice to use the @JsonNaming annotation on class level.

Yet afaik there is no strategy out there to fit your needs for total uppercase. But you can probably just write your own.

In my case I needed first character uppercase. So I used the existing @JsonNaming(value = UpperCamelCaseStrategy.class).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!