Insert a single object into json file without rewriting entire file

后端 未结 2 734
借酒劲吻你
借酒劲吻你 2021-01-20 15:20

I\'m working on a method that uses JSON.NET to add a horse object to a JSON-formatted database of horses. One option is to deserialize the entire file into a list of horses,

相关标签:
2条回答
  • 2021-01-20 15:35

    No, you couldn't, because serializer should read entire file to complete building JSON object structure. At the same time, this can be done with manual text file editing, but this solution will be doubtful. If you want fast read/write access to data, maybe you should consider another data-structure forms, for example, keepeing horses in DB.

    0 讨论(0)
  • 2021-01-20 15:38

    JSON isn't a database format, and a JSON file on disk is just a flat file with data that happens to be structured, which allows you to treat it - in memory - as objects. But on disk, it's just a text file, and filesystems are not designed for efficient insertion of data into the middle of a file, not without very tricky manipulation of the filesystem itself, as the answers here suggest.

    Yes, it is inefficient. If you want efficiency, use a database that's designed for updates. However, for relatively small JSON files, this theoretical inefficiency may be meaningless if saving the entire file to disk takes a fraction of a second.

    0 讨论(0)
提交回复
热议问题