Skip to content
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

Could you provide the "ObjectMapper.treeToValue(TreeNode, TypeReference)" method? #4046

Closed
fantasy0v0 opened this issue Jul 18, 2023 · 7 comments
Labels
2.16 Issues planned for 2.16 good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project
Milestone

Comments

@fantasy0v0
Copy link
Contributor

Is your feature request related to a problem? Please describe.

The situation now is that without this overloaded method, I need to write extra code to achieve the result I want.

ObjectMapper mapper = new ObjectMapper();
ArrayNode node = mapper.createArrayNode();
node.add("123").add("456");
JavaType javaType = mapper.constructType(new TypeReference<List<String>>() {});
try {
    mapper.treeToValue(node, javaType);
} catch(Exception e) {
    e.printStackTrace();
}

Describe the solution you'd like

Could you provide this method to simplify my code?

Like the code below.

ObjectMapper mapper = new ObjectMapper();
ArrayNode node = mapper.createArrayNode();
node.add("123").add("456");
try {
    mapper.treeToValue(node, new TypeReference<List<String>>() {});
} catch(Exception e) {
    e.printStackTrace();
}

Usage example

No response

Additional context

No response

@fantasy0v0 fantasy0v0 added the to-evaluate Issue that has been received but not yet evaluated label Jul 18, 2023
@cowtowncoder
Copy link
Member

This seems like something that could be an easy PR for someone looking for things to tackle; will label as such.

@cowtowncoder cowtowncoder added good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project 2.16 Issues planned for 2.16 and removed to-evaluate Issue that has been received but not yet evaluated labels Jul 18, 2023
@timnick-snow
Copy link

convertValue may helpful

        ObjectMapper mapper = new ObjectMapper();
        ArrayNode node = mapper.createArrayNode();
        node.add("123").add("456");
        try {
            List<String> list = mapper.convertValue(node, new TypeReference<List<String>>() {
            });
        } catch(Exception e) {
            e.printStackTrace();
        }

@JooHyukKim
Copy link
Member

convertValue may helpful

Are you suggesting method name changed to convertValue? Or is there more to it. Asking just to make things clear.

@timnick-snow

@timnick-snow
Copy link

timnick-snow commented Jul 21, 2023

convertValue may helpful转换值可能有帮助

Are you suggesting method name changed to convertValue? Or is there more to it. Asking just to make things clear.您是否建议将方法名称更改为 convertValue ?或者还有更多的事情要做。询问只是为了把事情说清楚。

No. 'convertValue' is an existing method. When fromValue is 'JsonNode', it should be equivalent to 'treeToValue'. But I'm not sure if there is a performance difference between them at the moment, as 'convertValue' seems to need to be serialized first. When fromValue is 'JsonNode', can this step be omitted?

@JooHyukKim

@JooHyukKim
Copy link
Member

first. When fromValue is 'JsonNode', can this step be omitted?

I think not and there is a reason for it. I don't remember in which issue or PR, but has been a discussion around it.

@cowtowncoder
Copy link
Member

first. When fromValue is 'JsonNode', can this step be omitted?

I think not and there is a reason for it. I don't remember in which issue or PR, but has been a discussion around it.

Actually serialization part can in fact be avoided because it is possible to create JsonParser that directly traverses JsonNode hierarchy.
So there is this performance benefit; although even serialization for convertValue() can avoid actual JSON generation: it will only add values into TokenBuffer, from which JsonParser (token stream) can be accessed.
I don't think performance difference has been measured but I would expect treeToValue() to be slightly faster.

@cowtowncoder
Copy link
Member

Fixed via #4056, closing. Will be in 2.16.0

@cowtowncoder cowtowncoder added this to the 2.16.0 milestone Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.16 Issues planned for 2.16 good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project
Projects
None yet
Development

No branches or pull requests

4 participants