jsonfile vs write-json-file vs lowdb
JSON Dosyası İşleme
jsonfilewrite-json-filelowdbBenzer Paketler:

JSON Dosyası İşleme

JSON dosyası işleme kütüphaneleri, JavaScript uygulamalarında JSON (JavaScript Object Notation) formatındaki verileri okumak, yazmak ve düzenlemek için kullanılan araçlardır. Bu kütüphaneler, JSON verilerini dosyalardan hızlı bir şekilde almak veya dosyalara kaydetmek için basit ve etkili yöntemler sunar. Özellikle Node.js ortamında, bu kütüphaneler geliştiricilerin veri kalıcılığı sağlamak, yapılandırma dosyaları oluşturmak veya küçük ölçekli veri tabanları ile çalışmak için ihtiyaç duyduğu işlevselliği sağlar. jsonfile, JSON dosyalarıyla çalışmak için basit bir API sunan hafif bir kütüphanedir. lowdb, düşük seviyeli bir JSON veritabanı sağlayan ve basit API'si ile hızlı veri okuma/yazma işlemleri yapmaya olanak tanıyan bir kütüphanedir. write-json-file, JSON verilerini dosyaya yazmak için optimize edilmiş bir kütüphanedir ve akış (stream) desteği ile büyük verileri verimli bir şekilde yazma imkanı sunar.

npm İndirme Trendi

3 Yıl

GitHub Stars Sıralaması

İstatistik Detayı

Paket
İndirmeler
Stars
Boyut
Issues
Yayın Tarihi
Lisans
jsonfile189,818,8071,21210.9 kB3il y a 7 moisMIT
write-json-file2,246,3712247.38 kB0il y a un anMIT
lowdb2,091,73922,57822.9 kB18il y a 3 ansMIT

Özellik Karşılaştırması: jsonfile vs write-json-file vs lowdb

Veri Okuma ve Yazma

  • jsonfile:

    jsonfile kütüphanesi, JSON dosyalarından veri okuma ve yazma işlemlerini basit bir şekilde gerçekleştirir. readFile ve writeFile gibi temel yöntemler sunar.

  • write-json-file:

    write-json-file kütüphanesi, JSON verilerini dosyaya yazmak için optimize edilmiştir. write yöntemi ile verileri hızlı ve verimli bir şekilde yazabilirsiniz.

  • lowdb:

    lowdb kütüphanesi, JSON dosyası tabanlı bir veritabanı oluşturur ve verileri anahtar-değer çiftleri şeklinde yönetir. get, set, push gibi yöntemlerle verileri kolayca okuyup yazabilirsiniz.

Veri Yapısı

  • jsonfile:

    jsonfile kütüphanesi, düz JSON verileri ile çalışır. Karmaşık veri yapıları için özel bir destek sağlamaz.

  • write-json-file:

    write-json-file kütüphanesi, düz ve karmaşık JSON verileri ile çalışabilir. Ancak, veri yapısının yazma işlemi sırasında bellek kullanımını etkileyebileceğini unutmamak gerekir.

  • lowdb:

    lowdb kütüphanesi, hiyerarşik ve karmaşık veri yapıları ile çalışabilir. JSON dosyası içinde iç içe nesneler ve diziler oluşturmanıza olanak tanır.

Bellek Kullanımı

  • jsonfile:

    jsonfile kütüphanesi, küçük ve orta boyutlu JSON dosyaları ile çalışırken bellek verimliliği sağlar. Ancak, dosya boyutu arttıkça bellek kullanımı da artar.

  • write-json-file:

    write-json-file kütüphanesi, akış (stream) desteği sayesinde büyük verileri yazarken bellek kullanımını en aza indirir. Bu özellik, büyük veri setleri ile çalışırken performansı artırır.

  • lowdb:

    lowdb kütüphanesi, verileri bellekte tutar, bu nedenle büyük veri setleri ile çalışırken bellek tüketimi artabilir. Ancak, veriler JSON dosyasında saklandığı için kalıcılık sağlar.

Akış Desteği

  • jsonfile:

    jsonfile kütüphanesi, akış desteği sunmaz. Tüm dosya okuma ve yazma işlemleri, dosyanın tamamını belleğe alarak gerçekleştirilir.

  • write-json-file:

    write-json-file kütüphanesi, akış desteği sunar. Bu özellik, büyük veri setlerini yazarken bellek kullanımını azaltır ve yazma işlemini daha verimli hale getirir.

  • lowdb:

    lowdb kütüphanesi, akış desteği sunmaz. Veriler bellekte tutulur ve dosyaya yazılırken tüm veri aynı anda işlenir.

Örnek Kullanım

  • jsonfile:

    jsonfile kütüphanesi ile JSON dosyasına veri yazma ve okuma işlemi

    const jsonfile = require('jsonfile');
    const file = 'data.json';
    const obj = { name: 'Alice', age: 25 };
    
    // Veriyi dosyaya yazma
    jsonfile.writeFile(file, obj)
      .then(() => console.log('Veri yazıldı!'))
      .catch(console.error);
    
    // Veriyi dosyadan okuma
    jsonfile.readFile(file)
      .then(data => console.log('Okunan veri:', data))
      .catch(console.error);
    
  • write-json-file:

    write-json-file kütüphanesi ile JSON verisini dosyaya yazma

    const writeJsonFile = require('write-json-file');
    const file = 'data.json';
    const data = { name: 'Alice', age: 25 };
    
    // Veriyi dosyaya yazma
    writeJsonFile(file, data)
      .then(() => console.log('Veri yazıldı!'))
      .catch(console.error);
    
  • lowdb:

    lowdb kütüphanesi ile basit bir JSON veritabanı oluşturma

    const { Low, JSONFile } = require('lowdb');
    const file = 'db.json';
    const adapter = new JSONFile(file);
    const db = new Low(adapter);
    
    // Veritabanını başlat
    await db.read();
    
    // Veritabanına veri ekleme
    db.data ||= { users: [] };
    db.data.users.push({ name: 'Alice', age: 25 });
    await db.write();
    
    // Verileri okuma
    console.log(db.data.users);
    

Nasıl Seçilir: jsonfile vs write-json-file vs lowdb

  • jsonfile:

    jsonfile kütüphanesini seçin, eğer JSON dosyalarıyla basit okuma ve yazma işlemleri yapmanız gerekiyorsa. Hızlı ve hafif bir çözüm arıyorsanız, bu kütüphane ihtiyaçlarınızı karşılayacaktır.

  • write-json-file:

    write-json-file kütüphanesini seçin, eğer büyük JSON verilerini dosyaya yazmanız gerekiyorsa ve yazma işlemi sırasında bellek verimliliği önem taşıyorsa. Akış desteği sayesinde büyük veri setleri ile çalışırken performans kaybını en aza indirir.

  • lowdb:

    lowdb kütüphanesini seçin, eğer küçük ölçekli bir veritabanı çözümüne ihtiyacınız varsa. JSON dosyası tabanlı, hızlı ve kolay bir veri yönetimi arıyorsanız, bu kütüphane ideal bir seçimdir.

jsonfile için 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]