GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - TinyIO/tiny-json: tiny.json is a schema base json stringify. it's significantly faster than JSON.stringify()

BranchesTags
Open more actions menu

Latest commit

 

History

9 Commits
9 Commits

Folders and files

NameName
Last commit message
Last commit date
.vscode
.vscode
 
 
benchmark
benchmark
 
 
examples
examples
 
 
lib
lib
 
 
tests
tests
 
 
.codebeatignore
.codebeatignore
 
 
.coveralls.yml
.coveralls.yml
 
 
.editorconfig
.editorconfig
 
 
.eslintrc.js
.eslintrc.js
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 

Repository files navigation

tiny.json

Build Status codebeat badge Coverage Status install size dependencies Status

tiny.json is a schema base json stringify. it's use schema to generated code. so it's significantly faster than JSON.stringify() with small payload. and since it stringify with schema is also more secure.

API

createStringify(schema)

Build a stringify() function based on jsonschema. for now this implementation only support 'type' & 'default'.

'string'

If value is null or undefined the 'default' whill be used. otherwise value.toJSON() or value.toString() will be used.

Sample

{
    type: 'string',
    default: '"hello"'
}
// notice the `default` need has `"` included, for now

'boolean'

If value is null or undefined the 'default' whill be used. otherwise, will use !!(value) to enforce to got true|false as result.

Sample

{
    type: 'boolean',
    default: null
}

'number'

Result as parseFloat(value) if isFinite otherwise default whill be used.

Sample

{
    type: 'number',
    default: 100
}

'integer'

Result as parseInt(value) if isFinite otherwise default whill be used.

Sample

{
    type: 'integer',
    default: 200
}

'object'

use properties to declare propertie, all type can nested.

Sample

// {"text":"hello", flag: false}
{
    type: 'object',
    properties: {
        text: { type: 'string'},
        flag: { type: 'boolean'}
    }
}

'array'

Sample

// [1,2,3,4]
{
    type: 'array',
    items: {
        type: 'integer'
    }
}
// [{a:1},{a:2}]
{
    type: 'array',
    items: {
        type: 'object',
        properties: {
            a: { type: 'number'}
        }
    }
}
// [1, 2, false, "hello"]
{
    type: 'array'
}

NOTE: tiny.json won't perform any string escaping as is could be a performance impact. escaping may happend before stringify and stored as escaped.

you can useing JSON.stringify or regex as your need

Sample

// with JSON.stringify.
JSON.stringify('hello "world"'); // hello \"world\"

// with regex.
'hello "world"'.replace(/\n|\r|\t|\"|\\/gm, char => '\\' + char) // hello \"world\"

Benchmark

Checkout benchmarks code

native x 1,720,071 ops/sec ±0.25% (91 runs sampled)
tiny x 60,489,145 ops/sec ±1.66% (89 runs sampled)

# tiny is +3416.67% faster

the payload and schema for this benchmark

const obj = {
  hello: 'world',
  num: 20190522,
  flag: true
};

const tiny = createStringify({
  type: 'object',
  properties: {
    hello: {
      type: 'string'
    },
    num: {
      type: 'number'
    },
    flag: {
      type: 'boolean'
    }
  }
});

About

tiny.json is a schema base json stringify. it's significantly faster than JSON.stringify()

Topics

fastjsonjson-schemajson-stringifyspeedstringify

Resources

Readme
MIT license
Activity
Custom properties

Stars

2 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.