jackson json question

suppose i have json array stored inside the file in the projects variable. how can i update it properly without rewriting it?
No description
18 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @Koblížkáč! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Peter Rader
Peter Rader7mo ago
You can not update contents of a file without rewriting the contents of a file. Vice versa, if you do not change the file you do not update the file.
Koblížkáč
KoblížkáčOP7mo ago
well then, what would be the recommended way to do that with jackson? how can i just update it
Peter Rader
Peter Rader7mo ago
Is it a static file?
Koblížkáč
KoblížkáčOP7mo ago
static file?
Peter Rader
Peter Rader7mo ago
I mean: Are you sure the file exists?
Koblížkáč
KoblížkáčOP7mo ago
yes
Peter Rader
Peter Rader7mo ago
Try
ObjectMapper om = new ObjectMapper();
var root = om.readValue(projects.toFile(), JsonNode.class);
//change the contents of the root according to your update
FileWriter fw = new FileWriter(projects.toFile());
fw.write(om.writeValueAsString(root));
fw.close();
ObjectMapper om = new ObjectMapper();
var root = om.readValue(projects.toFile(), JsonNode.class);
//change the contents of the root according to your update
FileWriter fw = new FileWriter(projects.toFile());
fw.write(om.writeValueAsString(root));
fw.close();
Koblížkáč
KoblížkáčOP7mo ago
@Peter Rader thanks it works, any idea if Path can be converted from string with jackson?
JavaBot
JavaBot7mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Koblížkáč
KoblížkáčOP7mo ago
i want to deserialize some json to this object:
No description
Koblížkáč
KoblížkáčOP7mo ago
and im doing it like this:
No description
Peter Rader
Peter Rader7mo ago
Hm, is this a different question?
Koblížkáč
KoblížkáčOP7mo ago
well its unrelated to my first question do i need to make new thread
szatkus
szatkus7mo ago
I mean, Jackson could serialize Path just fine. I did it just now.
Koblížkáč
KoblížkáčOP7mo ago
where the hell is the problem then
szatkus
szatkus7mo ago
No idea, this code works for me:
ObjectMapper om = new ObjectMapper();
var a = om.readValue("{\"p\":\"file:///C:/Users/basdfa/Downloads\"}", A.class);
System.out.println(a.p);
ObjectMapper om = new ObjectMapper();
var a = om.readValue("{\"p\":\"file:///C:/Users/basdfa/Downloads\"}", A.class);
System.out.println(a.p);
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server