Coder Social home page Coder Social logo

json-tokenize's Introduction

json-tokenize Build status NPM version Dependency Status License Js Standard Style

Splits a JSON string into an annotated list of tokens.

  • Keeps track of a tokens position
  • Whitespace significant
  • Doesn't validate the correctness of the syntax

Screenshot - formatted tokens outputted to stdout

View Example

Installation

npm install --save json-tokenize

Or even better

yarn add json-tokenize

Import and Usage Example

const tokenize = require('json-tokenize')

const obj = { "test": 1.0 }

tokenize(JSON.stringify(obj))
// ->
[
  { type: 'punctuation',
    position: { lineno: 1, column: 1 },
    raw: '{',
    value: '{' },
  { type: 'whitespace',
    position: { start: [Object], end: [Object] },
    raw: '\n  ',
    value: '\n  ' },
  { type: 'string',
    position: { start: [Object], end: [Object] },
    raw: '"test"',
    value: 'test' },
  { type: 'punctuation',
    position: { lineno: 2, column: 9 },
    raw: ':',
    value: ':' },
  { type: 'whitespace',
    position: { lineno: 2, column: 10 },
    raw: ' ',
    value: ' ' },
  { type: 'number',
    position: { lineno: 2, column: 11 },
    raw: '1',
    value: 1 },
  { type: 'whitespace',
    position: { start: [Object], end: [Object] },
    raw: '\n',
    value: '\n' },
  { type: 'punctuation',
    position: { lineno: 3, column: 1 },
    raw: '}',
    value: '}' }
]

Tokens

  • whitespace - Allowed white space between the actual relevant tokens.
  • punctuator - The characters sorrounding your data: {, }, [, ], : and ,
  • string - JSON String
  • number - JSON Number
  • literal - true, false or null

Author

json-tokenize © Fabian Eichenberger, Released under the MIT License.
Authored and maintained by Fabian Eichenberger with help from contributors (list).

GitHub @queckezz · Twitter @queckezz

json-tokenize's People

Contributors

queckezz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

json-tokenize's Issues

Double-encoded JSON fails

Basically, if you've got a blob of JSON inside of another JSON string, the tokenizing fails:

  /home/espenh/webdev/json-tokenize/dist.js:65

   64:   }, null);                         
   65:   var createToken = ref.createToken;
   66:   var str = ref.str;                

  Error: Cannot read property 'createToken' of null

  tokenize (dist.js:65:24)
  next (dist.js:95:12)
  tokenize (dist.js:85:10)
  next (dist.js:95:12)
  tokenize (dist.js:85:10)
  next (dist.js:95:12)
  tokenize (dist.js:85:10)
  next (dist.js:95:12)
  tokenize (dist.js:85:10)
  next (dist.js:95:12)
  tokenize (dist.js:85:10)
  next (dist.js:95:12)
  tokenize (dist.js:85:10)
  next (dist.js:95:12)
  tokenize (dist.js:85:10)
  next (dist.js:95:12)
  tokenize (dist.js:85:10)

Test case:

test('double-encoded json', (t) => {
  const inner = JSON.stringify({ x: 1, y: '2' })
  const outer = JSON.stringify({ type: 'blob', data: inner })
  const innerEnc = JSON.stringify(inner)
  validateJson(t, outer, [
    { type: 'punctuation', raw: '{', value: '{' },
    { type: 'string', raw: '"type"', value: 'type' },
    { type: 'punctuation', raw: ':', value: ':' },
    { type: 'string', raw: '"blob"', value: 'blob' },
    { type: 'punctuation', raw: ',', value: ',' },
    { type: 'string', raw: '"data"', value: 'data' },
    { type: 'punctuation', raw: ':', value: ':' },
    { type: 'string', raw: innerEnc, value: inner },
    { type: 'punctuation', raw: '}', value: '}' }
  ])
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.