Skip to content

stagas/fast-json-data

Repository files navigation

fast-json-data

Fast JSON data disk read/write.

npm i fast-json-data pnpm add fast-json-data yarn add fast-json-data

Efficiently read and write well-formed JSON data to the disk. About x1.5-2 times as fast as JSON.parse/stringify.

import { Literal, parse, stringify } from 'fast-json-data'

const schema = [[Literal, [
  '_id',
  '_rev',
  'name',
  'homepage',
  ['versions', ['*', [
    'name',
    'gitHead',
    ['os', [Literal]],
    ['cpu', [Literal]],
    ['engines', ['*']],
    ['dist', [
      'shasum',
      'tarball',
      'unpackedSize',
    ]],
    ['dependencies', [
      '*',
    ]],
  ]]],

  ['time', ['*']],
]]]

...

// packumentCache is a Map<string, Packument>

await fsp.writeFile(
  options.packumentCachePath,
  stringify(schema, [...packumentCache]),
  'utf-8'
)

...

json = parse(schema, await fsp.readFile(options.packumentCachePath, 'utf-8'))

API

# Literal  =  ... src/fast-json-data.ts#L1
# parse(schema, input) src/fast-json-data.ts#L62

    # schema

      any

    # input

      string

    parse(schema, input)  =>

      any

# parseLines(schema, input) src/fast-json-data.ts#L66
# stringify(schema, input) src/fast-json-data.ts#L3

    # schema

      any

    # input

      any

    stringify(schema, input)  =>

      string

Contributing

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2022 stagas

About

Fast JSON data disk read/write.

Resources

License

Stars

Watchers

Forks

Packages

No packages published