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
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
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.
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?
No, desktop
I have a GUI for editing settings as long as an "open JSON file" button
json is saved to a file?
Yup
then just save what user writes? after validating it can deserialize ofc
i guess that will work until you (app) makes changes..
Yup. That's the issue.
If user were to change the setting in the app, the formatting is entirely screwed
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
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
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)
To actually accomplish this, you'd need a custom library.
None of the existing JSON libraries will maintain the structure.