Skip to content

README.md: add example treemodel heterogenous operations #4135

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

Merged
merged 1 commit into from
Oct 2, 2023

Conversation

jonenst
Copy link
Contributor

@jonenst jonenst commented Oct 1, 2023

Hi,
maybe someone with deeper developper knowledge can proof read this proposed docs.
For me the most important part is to give an example of reading a subfield as a pojo (or list of Pojo), because this corresponds to the very frequent use case where your code models only part of a json document. I added the rest as it felt that it was the right place to showcase the nice shortcut APIs.
This uses 2.16+ functionality for some of the code.
What do you think ?

Copy link
Member

@cowtowncoder cowtowncoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could possibly by edited a bit more, but I think can be merged as-is as a starting point.
Thanks for sharing!

@cowtowncoder
Copy link
Member

As per my comment, I'll merged as is -- can be tweaked afterwards, looks useful!

@cowtowncoder cowtowncoder merged commit 265884a into FasterXML:2.16 Oct 2, 2023
@jonenst
Copy link
Contributor Author

jonenst commented Oct 2, 2023

Awesome. Are you planning to do the 2.16 release soon ? These docs are live on the main page https://github.com/FasterXML/jackson-databind but some won't work with latest 2.15.2. I think it's only these two:

 // 2.15
List<Person> friends = mapper.treeToValue(root.get("friends"), mapper.getTypeFactory().constructCollectionType( List.class, Person.class ));
 // more convenient, 2.16+
List<Person> samefriends = mapper.treeToValue(root.get("friends"), new TypeReference<List<Person>>() { });
 // 2.15 
root.with("deep").with("large").with("hiearchy").put("important", 42);
 // 2.16+, withObject accepts only jsonpath in 2.15
root.withObject("deep").withObject("large").withObject("hiearchy").put("important", 42);

Also I put these 3:

Map<String, Object> dynamicmetadata = Map.of("Some", "Metadata");
root.putPOJO("dynamicmetadata", dynamicmetadata);  // collections
root.putPOJO("dynamicmetadata", mapper.valueToTree(dynamicmetadata)); // same thing
root.set("dynamicmetadata", mapper.valueToTree(dynamicmetadata)); // same thing

But maybe the ones with valueToTree could be omitted ? Should valueToTree only be recommended for cases where you need to pass the data to some code that needs a JsonNode input ? I think I lack the global vision to make a good choice here.

@JooHyukKim
Copy link
Member

JooHyukKim commented Oct 2, 2023

https://github.com/FasterXML/jackson-databind but some won't work with latest 2.15.2. I think it's only these two

Maybe we can simply add versions

// since 2.15
....some code
....some code

// since 2.16
....some code
....some code

@cowtowncoder
Copy link
Member

Ah. Good point, I moved too fast. Yes, examples should point out minimum version. I do plan on making 2.16.0-rc1 release this week.

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

Successfully merging this pull request may close these issues.

3 participants