Comparing version 1.0.0 to 1.0.1
@@ -5,2 +5,15 @@ 'use strict'; | ||
var OPEN_BRACE = '{'.charCodeAt(0); | ||
var CLOSE_BRACE = '}'.charCodeAt(0); | ||
var OPEN_BRACKET = '['.charCodeAt(0); | ||
var CLOSE_BRACKET = ']'.charCodeAt(0); | ||
var QUOTE = '"'.charCodeAt(0); | ||
var SPACE = ' '.charCodeAt(0); | ||
var NEW_LINE = '\n'.charCodeAt(0); | ||
var CARRIAGE_RETURN = '\r'.charCodeAt(0); | ||
var TAB = '\t'.charCodeAt(0); | ||
var COLON = ':'.charCodeAt(0); | ||
var COMMA = ','.charCodeAt(0); | ||
var BACKSLASH = '\\'.charCodeAt(0); | ||
class FastJson { | ||
@@ -10,3 +23,3 @@ constructor(options) { | ||
this._stack = []; | ||
this._stack = new Array(16); | ||
this._level = -1; | ||
@@ -18,3 +31,2 @@ | ||
this._events = new EventEmitter(); | ||
this._paths = new Set(); | ||
this._subPaths = new Set(['/']); | ||
@@ -25,6 +37,3 @@ } | ||
var processedPath = this._processPath(path); | ||
this._paths.add(processedPath); | ||
this._addToSubPaths(processedPath); | ||
this._events.on(processedPath, listener); | ||
@@ -35,21 +44,21 @@ } | ||
for (var i = 0; i < data.length; i++) { | ||
switch (data[i]) { | ||
case '{': | ||
switch (data.charCodeAt(i)) { | ||
case OPEN_BRACE: | ||
i = this._onOpenBrace(data, i); | ||
break; | ||
case '[': | ||
case OPEN_BRACKET: | ||
i = this._onOpenBracket(data, i); | ||
break; | ||
case '}': case ']': | ||
case CLOSE_BRACE: case CLOSE_BRACKET: | ||
this._onCloseBraceOrBracket(data, i); | ||
break; | ||
case '"': | ||
case QUOTE: | ||
i = this._onQuote(data, i); | ||
break; | ||
case '\t' : case '\r' : case '\n' : case ' ': | ||
case TAB : case CARRIAGE_RETURN : case NEW_LINE : case SPACE: | ||
break; | ||
case ':': | ||
case COLON: | ||
this._postColon = true; | ||
break; | ||
case ',': | ||
case COMMA: | ||
this._onComma(); | ||
@@ -71,3 +80,3 @@ break; | ||
this._level--; | ||
return this._skipBlock(data, index, '{', '}'); | ||
return this._skipBlock(data, index, OPEN_BRACE, CLOSE_BRACE); | ||
} | ||
@@ -95,3 +104,3 @@ | ||
this._level--; | ||
return this._skipBlock(data, index, '[', ']'); | ||
return this._skipBlock(data, index, OPEN_BRACKET, CLOSE_BRACKET); | ||
} | ||
@@ -135,5 +144,3 @@ | ||
} | ||
} | ||
if (frame.type === 'array') { | ||
} else if (frame.type === 'array') { | ||
path = this._resolvePathForPrimitiveArray(data); | ||
@@ -170,5 +177,3 @@ | ||
} | ||
} | ||
if (frame.type === 'array') { | ||
} else if (frame.type === 'array') { | ||
path = this._resolvePathForPrimitiveArray(data); | ||
@@ -191,9 +196,12 @@ | ||
while (index < data.length) { | ||
if (data[index] === '"') { | ||
var strEnd = this._parseString(data, index); | ||
index += strEnd - index; | ||
} else if (data[index] === openChar) { | ||
blockDepth++; | ||
} else if (data[index] === closeChar) { | ||
blockDepth--; | ||
switch (data.charCodeAt(index)) { | ||
case QUOTE: | ||
var strEnd = this._parseString(data, index); | ||
index += strEnd - index; | ||
break; | ||
case openChar: | ||
blockDepth++; | ||
break; | ||
case closeChar: | ||
blockDepth--; | ||
} | ||
@@ -213,3 +221,4 @@ | ||
index++; | ||
while (data[index] !== '"' || (data[index - 1] === '\\' && data[index - 2] !== '\\')) { | ||
while (data.charCodeAt(index) !== QUOTE || (data.charCodeAt(index - 1) === BACKSLASH && | ||
data.charCodeAt(index - 2) !== BACKSLASH)) { | ||
index++; | ||
@@ -222,3 +231,4 @@ } | ||
_parsePrimitive(data, index) { | ||
while (data[index] !== ']' && data[index] !== '}' && data[index] !== ',') { | ||
while (data.charCodeAt(index) !== CLOSE_BRACKET && data.charCodeAt(index) !== CLOSE_BRACE && | ||
data.charCodeAt(index) !== COMMA) { | ||
index++; | ||
@@ -241,5 +251,3 @@ } | ||
if (parentType === 'object') { | ||
return data.substring(this._lastString.start, this._lastString.end); | ||
} | ||
return data.substring(this._lastString.start, this._lastString.end); | ||
} | ||
@@ -258,3 +266,3 @@ | ||
_hasListeners(processedPath) { | ||
return this._paths.has(processedPath); | ||
return this._events.listenerCount(processedPath) > 0; | ||
} | ||
@@ -264,4 +272,3 @@ | ||
if (this._level === 0) { | ||
return this._stack[this._level].path + | ||
data.substring(this._lastString.start, this._lastString.end); | ||
return '/' + data.substring(this._lastString.start, this._lastString.end); | ||
} | ||
@@ -275,3 +282,3 @@ | ||
if (this._level === 0) { | ||
return this._stack[this._level].path + this._stack[this._level].index; | ||
return '/' + this._stack[this._level].index; | ||
} | ||
@@ -296,7 +303,5 @@ | ||
for (var i = 0; i < tokens.length; i++) { | ||
if (tokens[i]) { | ||
aux += '/' + tokens[i]; | ||
this._subPaths.add(aux); | ||
} | ||
for (var i = 1; i < tokens.length; i++) { | ||
aux += '/' + tokens[i]; | ||
this._subPaths.add(aux); | ||
} | ||
@@ -303,0 +308,0 @@ } |
{ | ||
"name": "fast-json", | ||
"description": "A fast json stream parser.", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "Alejandro Santiago Nieto", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -45,3 +45,3 @@ fast-json | ||
* fast-json: 0.89s / 198MB RAM | ||
* fast-json: 0.71s / 198MB RAM | ||
* JSON.parse: 1.9s / 640MB RAM | ||
@@ -52,6 +52,6 @@ * jsonparse: 9.6s / 370MB RAM | ||
* [**Improvement**] Tolerate malformed JSONs. | ||
* [**Feature**] Cache chunks to support fragmentation (value starts in a previous chunk). | ||
* [**Feature**] Make FastJson a Node.js Stream (is this worth it?). | ||
* [**Feature**] Allow Buffers. | ||
* [**Feature**] Process wildcards in paths. | ||
* [**Feature**] Public method to cancel actual write(). | ||
* [**Documentation**] Document public interface and create branch gh-pages using *jsdoc*. | ||
* [**Documentation**] More real life testing and examples. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9139
235
0