Skip to content

@JsonUnwrapped does not work with JsonNode, should #3593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
G0dC0der opened this issue Sep 7, 2022 · 3 comments
Closed

@JsonUnwrapped does not work with JsonNode, should #3593

G0dC0der opened this issue Sep 7, 2022 · 3 comments

Comments

@G0dC0der
Copy link

G0dC0der commented Sep 7, 2022

JsonUnwrapped does not work with JsonNode. Using jackson-databind:jar:2.13.2.1.

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public abstract class Widget {

    private static ObjectMapper mapper = new ObjectMapper();

    @JsonProperty
    String widgetType;

    public static void main(String[] args) throws JsonProcessingException {
        String response = """
            {
                "foo": "bar",
                "x": "y"
            }
            """;

        JsonNode jsonNode = mapper.readTree(response);

        DailyWidget dailyWidget = new DailyWidget();
        dailyWidget.content = jsonNode;
        dailyWidget.widgetType = "DAILY_WIDGET";

        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dailyWidget));
    }
}

class DailyWidget extends Widget {
    @JsonUnwrapped
    JsonNode content;
}

Outputs:

{
  "widgetType" : "DAILY_WIDGET",
  "content" : {
    "foo" : "bar",
    "x" : "y"
  }
}

I expect this output:

{
  "widgetType" : "DAILY_WIDGET",
  "foo" : "bar",
  "x" : "y"
}
@G0dC0der G0dC0der added the to-evaluate Issue that has been received but not yet evaluated label Sep 7, 2022
@cowtowncoder
Copy link
Member

cowtowncoder commented Sep 8, 2022

Correct: currently JsonNode does not consider @JsonUnwrapped. It'd make sense for that to be available I suppose.

PRs welcome.

Note: serialization would likely be easy enough to support; deserialization more challenging.

@cowtowncoder cowtowncoder changed the title JsonUnwrapped does not work with JsonNode @JsonUnwrapped does not work with JsonNode, should Sep 8, 2022
@cowtowncoder
Copy link
Member

Thinking about this now, I think that what would actually also work here -- but not yet implemented either -- would be to allow use of @JsonAnyGetter on ObjectNode (or maybe JsonNode generally) valued fields/getters.
I filed #3604 for this.

@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Sep 23, 2022
@cowtowncoder cowtowncoder removed the 2.14 label Mar 2, 2024
@cowtowncoder
Copy link
Member

@JsonUnwrapped is designed to only work on POJOs, not any other types (Maps, ObjectNodes, etc etc), so closing as "wont-implement". Leaving #3604 open as an alternative take.

@cowtowncoder cowtowncoder closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants