question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@JsonRawValue uses not only annotated method but all fileds too

See original GitHub issue

Hello,

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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Feb 21, 2018

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.

0reactions
cowtowncodercommented, Feb 22, 2018

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found