@JsonRawValue uses not only annotated method but all fileds too
See original GitHub issueHello,
I’ve faced some strange issue with @JsonRawValue
annotation that duplicates fields in a response while @JsonValue
works as expected.
So, let’s consider some simplified POJO
@JacksonXmlRootElement(localName = "result")
public class PostSdkLicenseResponse implements Response {
private final StatusCodeType status;
private final String errorDetails;
private final String errorCode;
private final String errorMessage;
@JsonRawValue
@Override
public String toString() {
return String.format("%s%s%s%s",
XmlUtils.addSomeAttrib(errorCode, "errorCode", false, true, true),
XmlUtils.addSomeAttrib(errorDetails, "errorDetails", false, true, true),
XmlUtils.addSomeAttrib(errorMessage, "errorMessage", false, false, true),
XmlUtils.addSomeAttrib(status, "status", false, false, true)
);
}
}
which serialized to the following XML response
<result xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<errorCode/>
<errorDetails/>
<errorMessage/>
<status>Success</status>
<errorCode i:nil="true" xmlns="" />
<errorDetails i:nil="true" xmlns="" />
<errorMessage xmlns="" />
<status xmlns="">Success</status>
</result>
It looks like @JsonRawValue
uses not only annotated method but all fileds too. And there is no such problem with @JsonValue
annotation.
Tested on 2.8.11.1 and 2.9.4.
Cheers.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How can I include raw JSON in an object using Jackson?
So I was able to store raw value of JSON in rawJsonValue variable and then it was no problem to deserialize it (as...
Read more >Jackson Annotation Examples - Baeldung
We can add the @JsonProperty annotation to indicate the property name in JSON. Let's use @JsonProperty to serialize/deserialize the property ...
Read more >Jackson Annotations - Jenkov.com
The Jackson JSON toolkit contains a set of Java annotations which you can use to influence how JSON is read into objects, or...
Read more >Jackson Annotations For Java Application - GeeksforGeeks
Let us discuss each of the annotations in order to understand them to deeper roots by implementing them providing fragment codes for all...
Read more >Jackson Annotations for JSON - Spring Framework Guru
Jackson ignores the field in both JSON serialization and deserialization. ... This annotation tells Jackson to use this method to generate the JSON...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is misunderstanding, then. Although names are similar, intents are different:
@JsonRawValue
is NOT a specialization of@JsonValue
. I can see how this is confusing: in hindsight, perhaps it should have been named something different like@JsonAsRaw
or@JsonRaw
.So: it is simply a marker that a property should be embedded exactly as-is, presumably having been already encoded in format in question (json, xml).
Given this, code works as meant to be, although naming can imply different logic.
@aint Ok glad it works. Based on your original report, I think I’ll file a new feature request to ensure that combination of
@JsonValue
and@JsonRawValue
either works as-is, or if not, maybe add something like new property in@JsonValue
to allow “output as raw”. Latter might make more sense.