[Interest] json file handling

André Somers andre at familiesomers.nl
Wed Apr 15 16:36:15 CEST 2015


Mathieu Slabbinck schreef op 15-4-2015 om 16:17:
> Hi,
>
> I'm using Qt5 to write json objects to a file.
> The json structure contains one array with x data elements, like:
>
> {
>      "datapointsarray": [
>          {
>              "datapoint":"1",
>              "datapoint":"2",
>              ...
>          }
>       ]
> }
> Each x seconds, a "datapoint" should be added.
>
> The dilemma I'm in is, how can I tell the json writer to "append" to a
> current json file without loading the whole file back in memory (eg
> read json from file->edit in Qt->write back to file). Because, after a
> while, I suppose the file will get quite large.
>
I don't think JSON is particulary suitable for this kind of application 
(and neither is XML). The problem is that you cannot just append to the 
end of the file, and that is really what you want. You could fake that 
by abusing you knowledge of the structure of the document to write your 
datapoint directly and then write out the closing stuff (that is "}]}" 
in your example) yourself as well. Not nice, but it works (at least with 
the simple structure you have now).

André




More information about the Interest mailing list