jsonfile vs lowdb vs write-json-file
JSON 文件处理
jsonfilelowdbwrite-json-file类似的npm包:

JSON 文件处理

JSON 文件处理库是用于在 Node.js 应用程序中读取和写入 JSON 格式数据的工具。这些库提供简单的 API 来处理 JSON 文件,使得数据的持久化、读取和修改变得更加高效和便捷。它们通常支持异步和同步操作,能够处理大文件,提供错误处理机制,并且有助于简化与文件系统的交互。jsonfile 是一个轻量级的库,专注于简单的 JSON 文件读写,lowdb 是一个小型的数据库库,支持 JSON 文件作为数据存储,提供更复杂的数据操作功能,而 write-json-file 则专注于高效地写入 JSON 文件,支持流式写入和自定义序列化。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
jsonfile01,20910.8 kB28 个月前MIT
lowdb022,50422.9 kB152 年前MIT
write-json-file02237.38 kB06 个月前MIT

功能对比: jsonfile vs lowdb vs write-json-file

文件读写

  • jsonfile:

    jsonfile 提供简单的 readFilewriteFile 方法,支持异步和同步操作。

  • lowdb:

    lowdb 允许通过键值对方式读写数据,支持嵌套结构和复杂数据操作。

  • write-json-file:

    write-json-file 专注于高效写入 JSON 文件,提供流式写入和覆盖/追加选项。

数据结构

  • jsonfile:

    jsonfile 适用于简单的 JSON 数据结构,缺乏对复杂数据模型的支持。

  • lowdb:

    lowdb 支持嵌套和复杂数据结构,允许更灵活的数据组织和操作。

  • write-json-file:

    write-json-file 主要关注写入过程,不限制数据结构,但不提供数据操作功能。

性能

  • jsonfile:

    jsonfile 在处理小到中等大小的文件时性能良好,但不适合处理大文件。

  • lowdb:

    lowdb 性能适中,适合小型数据集,但随着数据量增加,查询和操作性能可能下降。

  • write-json-file:

    write-json-file 在写入大文件时表现优越,支持流式写入以减少内存占用。

易用性

  • jsonfile:

    jsonfile 提供直观的 API,易于快速上手,适合简单文件操作。

  • lowdb:

    lowdb 具有简单易用的 API,但对复杂数据操作可能需要一些学习。

  • write-json-file:

    write-json-file API 简洁明了,特别是在写入操作方面。

示例代码

  • jsonfile:

    使用 jsonfile 读取和写入 JSON 文件

    const jsonfile = require('jsonfile');
    const file = 'data.json';
    
    // 写入 JSON 文件
    const obj = { name: 'Alice', age: 25 };
    jsonfile.writeFile(file, obj)
      .then(() => console.log('写入完成'))
      .catch(err => console.error(err));
    
    // 读取 JSON 文件
    jsonfile.readFile(file)
      .then(data => console.log('读取数据:', data))
      .catch(err => console.error(err));
    
  • lowdb:

    使用 lowdb 进行简单的数据库操作

    const { Low, JSONFile } = require('lowdb');
    const file = 'db.json';
    const adapter = new JSONFile(file);
    const db = new Low(adapter);
    
    // 初始化数据库
    async function init() {
      await db.read();
      db.data ||= { users: [] }; // 如果没有数据则初始化
      await db.write();
    }
    
    // 添加用户
    async function addUser(name, age) {
      db.data.users.push({ name, age });
      await db.write();
    }
    
    // 读取用户
    async function getUsers() {
      await db.read();
      console.log(db.data.users);
    }
    
    init().then(() => {
      addUser('Alice', 25);
      addUser('Bob', 30);
      getUsers();
    });
    
  • write-json-file:

    使用 write-json-file 高效写入 JSON 文件

    const writeJsonFile = require('write-json-file');
    const file = 'data.json';
    const data = { name: 'Alice', age: 25 };
    
    // 写入 JSON 文件
    writeJsonFile(file, data)
      .then(() => console.log('写入完成'))
      .catch(err => console.error(err));
    

如何选择: jsonfile vs lowdb vs write-json-file

  • jsonfile:

    选择 jsonfile 如果您只需要一个简单、轻量级的解决方案来读取和写入 JSON 文件。它的 API 直观,适合快速实现基本的文件操作。

  • lowdb:

    选择 lowdb 如果您需要一个小型的、基于 JSON 的数据库,支持更复杂的数据操作,如查询、更新和嵌套数据结构。它适合需要持久化数据但不想使用完整数据库系统的项目。

  • write-json-file:

    选择 write-json-file 如果您关注写入性能,特别是在处理大文件时。它支持流式写入,允许您逐块写入数据,从而减少内存使用。

jsonfile的README

Node.js - jsonfile

Easily read/write JSON files in Node.js. Note: this module cannot be used in the browser.

npm Package linux build status windows Build status

Standard JavaScript

Why?

Writing JSON.stringify() and then fs.writeFile() and JSON.parse() with fs.readFile() enclosed in try/catch blocks became annoying.

Installation

npm install --save jsonfile

API


readFile(filename, [options], callback)

options (object, default undefined): Pass in any fs.readFile options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If JSON.parse throws an error, pass this error to the callback. If false, returns null for the object.
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file, function (err, obj) {
  if (err) console.error(err)
  console.dir(obj)
})

You can also use this method with promises. The readFile method will return a promise if you do not pass a callback function.

const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file)
  .then(obj => console.dir(obj))
  .catch(error => console.error(error))

readFileSync(filename, [options])

options (object, default undefined): Pass in any fs.readFileSync options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If an error is encountered reading or parsing the file, throw the error. If false, returns null for the object.
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'

console.dir(jsonfile.readFileSync(file))

writeFile(filename, obj, [options], callback)

options: Pass in any fs.writeFile options or set replacer for a JSON replacer. Can also pass in spaces, or override EOL string or set finalEOL flag as false to not save the file with EOL at the end.

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, function (err) {
  if (err) console.error(err)
})

Or use with promises as follows:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj)
  .then(res => {
    console.log('Write complete')
  })
  .catch(error => console.error(error))

formatting with spaces:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {
  if (err) console.error(err)
})

overriding EOL:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
  if (err) console.error(err)
})

disabling the EOL at the end of file:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2, finalEOL: false }, function (err) {
  if (err) console.log(err)
})

appending to an existing JSON file:

You can use fs.writeFile option { flag: 'a' } to achieve this.

const jsonfile = require('jsonfile')

const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
  if (err) console.error(err)
})

writeFileSync(filename, obj, [options])

options: Pass in any fs.writeFileSync options or set replacer for a JSON replacer. Can also pass in spaces, or override EOL string or set finalEOL flag as false to not save the file with EOL at the end.

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj)

formatting with spaces:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2 })

overriding EOL:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })

disabling the EOL at the end of file:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2, finalEOL: false })

appending to an existing JSON file:

You can use fs.writeFileSync option { flag: 'a' } to achieve this.

const jsonfile = require('jsonfile')

const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { flag: 'a' })

License

(MIT License)

Copyright 2012-2016, JP Richardson jprichardson@gmail.com