#jackson json question

21 messages · Page 1 of 1 (latest)

mild galleon
#

suppose i have json array stored inside the file in the projects variable.
how can i update it properly without rewriting it?

floral mossBOT
#

This post has been reserved for your question.

Hey @mild galleon! 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.

civic briar
#

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.

mild galleon
#

well then, what would be the recommended way to do that with jackson? how can i just update it

civic briar
#

Is it a static file?

mild galleon
#

static file?

civic briar
#

I mean: Are you sure the file exists?

mild galleon
#

yes

civic briar
#

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();```
mild galleon
#

@civic briar thanks it works, any idea if Path can be converted from string with jackson?

floral mossBOT
mild galleon
#

i want to deserialize some json to this object:

#

and im doing it like this:

civic briar
#

Hm, is this a different question?

mild galleon
#

do i need to make new thread

tawdry sage
#

I mean, Jackson could serialize Path just fine. I did it just now.

mild galleon
#

where the hell is the problem then

tawdry sage
#

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);