C
C#2w ago
Dongle

Avoid reserializing entire JSON?

Is there a way to avoid reserializing and reformatting an entire JSON file when it's only a property that got changed? I don't want to mess up the user's formatting.
12 Replies
tera
tera2w ago
dont think so. but why bother? imo json has only 2 acceptable formats and both can be done by the serializer: indented and compact. anything else is just.. why
Dongle
Dongle2w ago
My app allows the user to directly modify JSON files, which mean they can format the thing in very strange ways like comments and stuff. I wanted to respect that for simple changes like changing the value of properties that can be in one line.
tera
tera2w ago
you could just keep showing them what they were editing then.. as long as they are on that screen. past that there is no way to preserve custom formatting other than saving raw json string unless i misunderstood is this web app?
Dongle
Dongle2w ago
No, desktop I have a GUI for editing settings as long as an "open JSON file" button
tera
tera2w ago
json is saved to a file?
Dongle
Dongle2w ago
Yup
tera
tera2w ago
then just save what user writes? after validating it can deserialize ofc i guess that will work until you (app) makes changes..
Dongle
Dongle2w ago
Yup. That's the issue. If user were to change the setting in the app, the formatting is entirely screwed
tera
tera2w ago
honestly you are better off your sanity not doing this, than trying to support something non standard as this other than that I dont have any ideas :p then again someone must have done something like this already.. cos i think vscode respects that in its settings json
jeffijoe
jeffijoe2w ago
I dont think its worth the effort, but if you tracked changes in a way that you know which properties have changed, you could locate the original range of the property in the JSON file and replace it with the newly serialized one
Anu6is
Anu6is2w ago
Imo the user should either use standard json format or you shouldn't present the data to them in json format at all (of course that depends on context and json might make sense. Supporting custom formatting still doesn't seem worth it)
wasabi
wasabi2w ago
To actually accomplish this, you'd need a custom library. None of the existing JSON libraries will maintain the structure.