jsonfile vs write-json-file vs lowdb
JSON 檔案處理
jsonfilewrite-json-filelowdb類似套件:

JSON 檔案處理

這些庫提供了簡單的 API 來讀取和寫入 JSON 檔案,適用於 Node.js 環境。它們各自具有獨特的功能和設計理念,滿足不同的使用場景。jsonfile 提供基本的讀寫功能,lowdb 則是一個小型的資料庫解決方案,支援 CRUD 操作,並且可以直接操作 JSON 檔案,write-json-file 專注於高效能的寫入操作,並支援格式化和自動創建檔案。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
jsonfile189,818,8071,21210.9 kB37 個月前MIT
write-json-file2,246,3712247.38 kB01 年前MIT
lowdb2,091,73922,57822.9 kB183 年前MIT

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

檔案讀取與寫入

  • jsonfile:

    jsonfile 提供簡單的讀取和寫入 JSON 檔案的功能。它支援異步和同步操作,適合各種使用場景。

  • write-json-file:

    write-json-file 專注於寫入 JSON 檔案,提供高效能的寫入方法,並支援流式寫入和格式化。

  • lowdb:

    lowdb 允許讀取和寫入 JSON 檔案,但它的主要功能是作為一個小型資料庫,支援 CRUD 操作。

資料庫功能

  • jsonfile:

    jsonfile 不具備資料庫功能,僅提供檔案操作。

  • write-json-file:

    write-json-file 不具備資料庫功能,專注於檔案寫入。

  • lowdb:

    lowdb 提供完整的資料庫功能,支援 CRUD 操作,並允許使用者透過鍵值對方式存取資料。

效能

  • jsonfile:

    jsonfile 在處理小型檔案時效能良好,但對於大型檔案,讀取和寫入速度會受到影響。

  • write-json-file:

    write-json-file 在寫入大型檔案時表現優異,特別是使用流式寫入時,可以有效降低記憶體使用量。

  • lowdb:

    lowdb 由於需要處理資料庫操作,效能會受到資料量大小的影響,但對於小型資料集來說仍然相當快。

格式化選項

  • jsonfile:

    jsonfile 在寫入檔案時不提供格式化選項。

  • write-json-file:

    write-json-file 提供格式化選項,允許使用者在寫入檔案時指定縮排級別,這對於提高檔案可讀性非常有幫助。

  • lowdb:

    lowdb 允許使用者自訂資料格式,但預設情況下不提供格式化功能。

範例程式碼

  • jsonfile:

    使用 jsonfile 讀取和寫入 JSON 檔案

    const jsonfile = require('jsonfile');
    const file = 'data.json';
    
    // 寫入檔案
    const obj = { name: 'Alice', age: 25 };
    jsonfile.writeFile(file, obj, { spaces: 2 }, (err) => {
      if (err) console.error(err);
    });
    
    // 讀取檔案
    jsonfile.readFile(file, (err, data) => {
      if (err) console.error(err);
      console.log(data);
    });
    
  • write-json-file:

    使用 write-json-file 寫入 JSON 檔案

    const writeJsonFile = require('write-json-file');
    const file = 'data.json';
    const data = { name: 'Bob', age: 30 };
    
    // 寫入檔案
    writeJsonFile(file, data, { indent: 2 }).then(() => {
      console.log('檔案已寫入');
    });
    
  • lowdb:

    使用 lowdb 進行 CRUD 操作

    const { Low, JSONFile } = require('lowdb');
    const file = 'db.json';
    const adapter = new JSONFile(file);
    const db = new Low(adapter);
    
    // 初始化資料庫
    await db.read();
    db.data ||= { users: [] };
    
    // 新增使用者
    db.data.users.push({ id: 1, name: 'Alice' });
    await db.write();
    
    // 讀取使用者
    console.log(db.data.users);
    

如何選擇: jsonfile vs write-json-file vs lowdb

  • jsonfile:

    選擇 jsonfile 如果你只需要簡單的讀取和寫入 JSON 檔案,且不需要額外的功能。它的 API 簡單明瞭,適合快速開發和小型專案。

  • write-json-file:

    選擇 write-json-file 如果你需要高效能的寫入操作,特別是在處理大型檔案時。它提供了流式寫入和格式化選項,適合對效能有要求的應用程式。

  • lowdb:

    選擇 lowdb 如果你需要一個輕量級的資料庫解決方案,並且希望能夠進行 CRUD 操作。它適合需要持久化資料但不想使用完整資料庫系統的專案。

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 [email protected]