I'm having a problem where the JsonProperty annotation to change the name of a variable is not being honored. Here is the example app https://github.com/derrylvarghese/problematicjson
I am converting the following JSON to POJO and then converting it back to JSON
String json = "{\"response\": {\"companyname\": \"ABC Company\",\"issn\": [{\"format\": \"paper\",\"val\": \"0018-9480\"},{\"format\": \"online\",\"val\": \"1557-9670\"}]},\"teststring\":\"testing me out\"}";
ObjectMapper mapper = new ObjectMapper();
WrapperObj record = mapper.readValue(json, WrapperObj.class);
ObjectWriter writer = mapper.writer();
writer.writeValue(response.getOutputStream(), record);
I am expecting the 'val' in the 'issn' object to show up as 'value' since i have a @JsonProperty("value") applied on the 'val' field. Here is the output json (edited for brevity).
issn: [
{
format: "Print ISSN",
val: "0018-9480"
}
]
If i were to create an Issn object and do a record.getResponse.setIssn(setMyObject), the json output shows the proper JsonProperty tag. I am linking to a sample project that will show this if anyone is interested. (git link: https://github.com/derrylvarghese/problematicjson)
Update
Seems like since i annotated my POJO field with @JsonProperty("value"), the incoming JSON field should also be called 'value'. All this while i was seeing 'val' in the output because i had a catch all List field into which all the fields were going. I removed that JsonAnyGetter field and now my 'val' field does not show up.
@Codo say check your import, I used to have the same trouble when in my project few imports mixed library from different providers of Jaxson reader/writer Check that and I'm sure you'll find your bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With