Insert a single object into json file without rewriting entire file

后端 未结 2 736
借酒劲吻你
借酒劲吻你 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: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.

提交回复
热议问题