alemures / fast-json Goto Github PK
View Code? Open in Web Editor NEWA lightning fast on the fly JSON parser
A lightning fast on the fly JSON parser
Hi,
when waiting for a json key event's to fire, I cannot write a syntax for jsons were (unfortuantely) the key has a .
within like
{
"mykey": {
"myinnerkey.dotted": []
}
}
fastJson.on('mykey["myinnerkey.dotted"]', (value) => {
console.log('mykey.myinnerkey.dotted' ->', value.toString());
});
Looking at the code, I assume this happens here:
_processPath(path) {
if (!path) {
return '/';
}
path = path.replace(/[\.\[]/g, '/');
path = path.replace(/\]/g, '');
return path[0] !== '/' ? '/' + path : path;
}
but I don't figure out how to avoid path = path.replace(/[\.\[]/g, '/');
for those inner .
.
Thank you!
fastjson is not deserializing at all. It's only cutting out the specific part of the json string and gives a string back as a result (the json serialized string).
This library makes no sense if I understand it correctly how it works.
What i did:
const fastJson = new FastJson();
fastJson.on("data", (data: any) => {
console.log(data);
});
fastJson.write(mySerializedJsonString);
as a result I get a serialized json as a result. What am I doing wrong? I'm expecting to get the de-serialized object back.
The following JSON string will return an invalid value for the primitive null (and the same will happen all primitives):
fastJson.on('a', (value) => {
console.log('a ->', `"${value}"`);
});
fastJson.write('{"a": null\n}');
It will output:
a -> "null
"
The FastJson#._onPrimitive
is actually reading up to a CLOSE_BRACKET, CLOSE_BRACE or COMMA character so, any whitespace character will be included.
We have to find a way to exclude those whitespace characters from the primitive values which can appear in both sides of the primitive value, support also the implementation with unit tests covering all edge cases.
Is there any even like on('end', () => {})
.. Because my i am trying to parse json array and I need to know when the parsing is done.
Research about complex JSON structures and examples in order to create more and better unit tests.
https://www.json.org/json-en.html
http://seriot.ch/parsing_json.php
Currently we support JSON path keys with dots '.' using an array path, it could be great to have the string path separator configurable so, if we spect keys having dots, we could set the separator to slash '/' and keep using string paths.
const fastJson = new FastJson({ pathSeparator: '/' });
fastJson.on('user/first.name', (value) => { console.log(value); });
`var $jscomp = $jscomp || {};
$jscomp.scope = {};
$jscomp.getGlobal = function(a) {
a = ["object" == typeof globalThis && globalThis, a, "object" == typeof window && window, "object" == typeof self && self, "object" == typeof global && global];
for (var b = 0; b < a.length; ++b) {
var c = a[b];
if (c && c.Math == Math) return c
}
throw Error("Cannot find global object");
};
$jscomp.global = $jscomp.getGlobal(this);
$jscomp.checkEs6ConformanceViaProxy = function() {
try {
var a = {},
b = Object.create(new $jscomp.global.Proxy(a, {
get: function(c, d, h) {
return c == a && "q" == d && h == b
}
}));
return !0 === b.q
} catch (c) {
return !1
}
};
$jscomp.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS = !1;
$jscomp.ES6_CONFORMANCE = $jscomp.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS && $jscomp.checkEs6ConformanceViaProxy();
$jscomp.arrayIteratorImpl = function(a) {
var b = 0;
return function() {
return b < a.length ? {
done: !1,
value: a[b++]
} : {
done: !0
}
}
};
$jscomp.arrayIterator = function(a) {
return {
next: $jscomp.arrayIteratorImpl(a)
}
};
$jscomp.ASSUME_ES5 = !1;
$jscomp.ASSUME_NO_NATIVE_MAP = !1;
$jscomp.ASSUME_NO_NATIVE_SET = !1;
$jscomp.SIMPLE_FROUND_POLYFILL = !1;
$jscomp.ISOLATE_POLYFILLS = !1;
$jscomp.FORCE_POLYFILL_PROMISE = !1;
$jscomp.FORCE_POLYFILL_PROMISE_WHEN_NO_UNHANDLED_REJECTION = !1;
$jscomp.defineProperty = $jscomp.ASSUME_ES5 || "function" == typeof Object.defineProperties ? Object.defineProperty : function(a, b, c) {
if (a == Array.prototype || a == Object.prototype) return a;
a[b] = c.value;
return a
};
$jscomp.IS_SYMBOL_NATIVE = "function" === typeof Symbol && "symbol" === typeof Symbol("x");
$jscomp.TRUST_ES6_POLYFILLS = !$jscomp.ISOLATE_POLYFILLS || $jscomp.IS_SYMBOL_NATIVE;
$jscomp.polyfills = {};
$jscomp.propertyToPolyfillSymbol = {};
$jscomp.POLYFILL_PREFIX = "$jscp$";
var $jscomp$lookupPolyfilledValue = function(a, b) {
var c = $jscomp.propertyToPolyfillSymbol[b];
if (null == c) return a[b];
c = a[c];
return void 0 !== c ? c : a[b]
};
$jscomp.polyfill = function(a, b, c, d) {
b && ($jscomp.ISOLATE_POLYFILLS ? $jscomp.polyfillIsolated(a, b, c, d) : $jscomp.polyfillUnisolated(a, b, c, d))
};
$jscomp.polyfillUnisolated = function(a, b, c, d) {
c = $jscomp.global;
a = a.split(".");
for (d = 0; d < a.length - 1; d++) {
var h = a[d];
if (!(h in c)) return;
c = c[h]
}
a = a[a.length - 1];
d = c[a];
b = b(d);
b != d && null != b && $jscomp.defineProperty(c, a, {
configurable: !0,
writable: !0,
value: b
})
};
$jscomp.polyfillIsolated = function(a, b, c, d) {
var h = a.split(".");
a = 1 === h.length;
d = h[0];
d = !a && d in $jscomp.polyfills ? $jscomp.polyfills : $jscomp.global;
for (var m = 0; m < h.length - 1; m++) {
var l = h[m];
if (!(l in d)) return;
d = d[l]
}
h = h[h.length - 1];
c = $jscomp.IS_SYMBOL_NATIVE && "es6" === c ? d[h] : null;
b = b(c);
null != b && (a ? $jscomp.defineProperty($jscomp.polyfills, h, {
configurable: !0,
writable: !0,
value: b
}) : b !== c && (void 0 === $jscomp.propertyToPolyfillSymbol[h] && ($jscomp.propertyToPolyfillSymbol[h] = $jscomp.IS_SYMBOL_NATIVE ? $jscomp.global.Symbol(h) :
$jscomp.POLYFILL_PREFIX + h), h = $jscomp.propertyToPolyfillSymbol[h], $jscomp.defineProperty(d, h, {
configurable: !0,
writable: !0,
value: b
})))
};
$jscomp.initSymbol = function() {};
$jscomp.polyfill("Symbol", function(a) {
if (a) return a;
var b = function(h, m) {
this.$jscomp$symbol$id_ = h;
$jscomp.defineProperty(this, "description", {
configurable: !0,
writable: !0,
value: m
})
};
b.prototype.toString = function() {
return this.$jscomp$symbol$id_
};
var c = 0,
d = function(h) {
if (this instanceof d) throw new TypeError("Symbol is not a constructor");
return new b("jscomp_symbol_" + (h || "") + "_" + c++, h)
};
return d
}, "es6", "es3");
$jscomp.polyfill("Symbol.iterator", function(a) {
if (a) return a;
a = Symbol("Symbol.iterator");
for (var b = "Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array".split(" "), c = 0; c < b.length; c++) {
var d = $jscomp.global[b[c]];
"function" === typeof d && "function" != typeof d.prototype[a] && $jscomp.defineProperty(d.prototype, a, {
configurable: !0,
writable: !0,
value: function() {
return $jscomp.iteratorPrototype($jscomp.arrayIteratorImpl(this))
}
})
}
return a
}, "es6",
"es3");
$jscomp.iteratorPrototype = function(a) {
a = {
next: a
};
a[Symbol.iterator] = function() {
return this
};
return a
};
$jscomp.makeIterator = function(a) {
var b = "undefined" != typeof Symbol && Symbol.iterator && a[Symbol.iterator];
return b ? b.call(a) : $jscomp.arrayIterator(a)
};
$jscomp.owns = function(a, b) {
return Object.prototype.hasOwnProperty.call(a, b)
};
$jscomp.polyfill("WeakMap", function(a) {
function b() {
if (!a || !Object.seal) return !1;
try {
var e = Object.seal({}),
g = Object.seal({}),
k = new a([
[e, 2],
[g, 3]
]);
if (2 != k.get(e) || 3 != k.get(g)) return !1;
k.delete(e);
k.set(g, 4);
return !k.has(e) && 4 == k.get(g)
} catch (n) {
return !1
}
}
function c() {}
function d(e) {
var g = typeof e;
return "object" === g && null !== e || "function" === g
}
function h(e) {
if (!$jscomp.owns(e, l)) {
var g = new c;
$jscomp.defineProperty(e, l, {
value: g
})
}
}
function m(e) {
if (!$jscomp.ISOLATE_POLYFILLS) {
var g = Object[e];
g && (Object[e] =
function(k) {
if (k instanceof c) return k;
Object.isExtensible(k) && h(k);
return g(k)
})
}
}
if ($jscomp.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS) {
if (a && $jscomp.ES6_CONFORMANCE) return a
} else if (b()) return a;
var l = "$jscomp_hidden_" + Math.random();
m("freeze");
m("preventExtensions");
m("seal");
var p = 0,
f = function(e) {
this.id_ = (p += Math.random() + 1).toString();
if (e) {
e = $jscomp.makeIterator(e);
for (var g; !(g = e.next()).done;) g = g.value, this.set(g[0], g[1])
}
};
f.prototype.set = function(e, g) {
if (!d(e)) throw Error("Invalid WeakMap key");
h(e);
if (!$jscomp.owns(e, l)) throw Error("WeakMap key fail: " + e);
e[l][this.id_] = g;
return this
};
f.prototype.get = function(e) {
return d(e) && $jscomp.owns(e, l) ? e[l][this.id_] : void 0
};
f.prototype.has = function(e) {
return d(e) && $jscomp.owns(e, l) && $jscomp.owns(e[l], this.id_)
};
f.prototype.delete = function(e) {
return d(e) && $jscomp.owns(e, l) && $jscomp.owns(e[l], this.id_) ? delete e[l][this.id_] : !1
};
return f
}, "es6", "es3");
$jscomp.MapEntry = function() {};
$jscomp.polyfill("Map", function(a) {
function b() {
if ($jscomp.ASSUME_NO_NATIVE_MAP || !a || "function" != typeof a || !a.prototype.entries || "function" != typeof Object.seal) return !1;
try {
var f = Object.seal({
x: 4
}),
e = new a($jscomp.makeIterator([
[f, "s"]
]));
if ("s" != e.get(f) || 1 != e.size || e.get({
x: 4
}) || e.set({
x: 4
}, "t") != e || 2 != e.size) return !1;
var g = e.entries(),
k = g.next();
if (k.done || k.value[0] != f || "s" != k.value[1]) return !1;
k = g.next();
return k.done || 4 != k.value[0].x || "t" != k.value[1] || !g.next().done ? !1 : !0
} catch (n) {
return !1
}
}
if ($jscomp.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS) {
if (a && $jscomp.ES6_CONFORMANCE) return a
} else if (b()) return a;
var c = new WeakMap,
d = function(f) {
this.data_ = {};
this.head_ = l();
this.size = 0;
if (f) {
f = $jscomp.makeIterator(f);
for (var e; !(e = f.next()).done;) e = e.value, this.set(e[0], e[1])
}
};
d.prototype.set = function(f, e) {
f = 0 === f ? 0 : f;
var g = h(this, f);
g.list || (g.list = this.data_[g.id] = []);
g.entry ? g.entry.value = e : (g.entry = {
next: this.head_,
previous: this.head_.previous,
head: this.head_,
key: f,
value: e
}, g.list.push(g.entry),
this.head_.previous.next = g.entry, this.head_.previous = g.entry, this.size++);
return this
};
d.prototype.delete = function(f) {
f = h(this, f);
return f.entry && f.list ? (f.list.splice(f.index, 1), f.list.length || delete this.data_[f.id], f.entry.previous.next = f.entry.next, f.entry.next.previous = f.entry.previous, f.entry.head = null, this.size--, !0) : !1
};
d.prototype.clear = function() {
this.data_ = {};
this.head_ = this.head_.previous = l();
this.size = 0
};
d.prototype.has = function(f) {
return !!h(this, f).entry
};
d.prototype.get = function(f) {
return (f =
h(this, f).entry) && f.value
};
d.prototype.entries = function() {
return m(this, function(f) {
return [f.key, f.value]
})
};
d.prototype.keys = function() {
return m(this, function(f) {
return f.key
})
};
d.prototype.values = function() {
return m(this, function(f) {
return f.value
})
};
d.prototype.forEach = function(f, e) {
for (var g = this.entries(), k; !(k = g.next()).done;) k = k.value, f.call(e, k[1], k[0], this)
};
d.prototype[Symbol.iterator] = d.prototype.entries;
var h = function(f, e) {
var g = e && typeof e;
"object" == g || "function" == g ? c.has(e) ? g = c.get(e) :
(g = "" + ++p, c.set(e, g)) : g = "p_" + e;
var k = f.data_[g];
if (k && $jscomp.owns(f.data_, g))
for (f = 0; f < k.length; f++) {
var n = k[f];
if (e !== e && n.key !== n.key || e === n.key) return {
id: g,
list: k,
index: f,
entry: n
}
}
return {
id: g,
list: k,
index: -1,
entry: void 0
}
},
m = function(f, e) {
var g = f.head_;
return $jscomp.iteratorPrototype(function() {
if (g) {
for (; g.head != f.head_;) g = g.previous;
for (; g.next != g.head;) return g = g.next, {
done: !1,
value: e(g)
};
g = null
}
return {
done: !0,
value: void 0
}
})
},
l = function() {
var f = {};
return f.previous = f.next = f.head = f
},
p = 0;
return d
}, "es6", "es3");
$jscomp.polyfill("Set", function(a) {
function b() {
if ($jscomp.ASSUME_NO_NATIVE_SET || !a || "function" != typeof a || !a.prototype.entries || "function" != typeof Object.seal) return !1;
try {
var d = Object.seal({
x: 4
}),
h = new a($jscomp.makeIterator([d]));
if (!h.has(d) || 1 != h.size || h.add(d) != h || 1 != h.size || h.add({
x: 4
}) != h || 2 != h.size) return !1;
var m = h.entries(),
l = m.next();
if (l.done || l.value[0] != d || l.value[1] != d) return !1;
l = m.next();
return l.done || l.value[0] == d || 4 != l.value[0].x || l.value[1] != l.value[0] ? !1 : m.next().done
} catch (p) {
return !1
}
}
if ($jscomp.USE_PROXY_FOR_ES6_CONFORMANCE_CHECKS) {
if (a && $jscomp.ES6_CONFORMANCE) return a
} else if (b()) return a;
var c = function(d) {
this.map_ = new Map;
if (d) {
d = $jscomp.makeIterator(d);
for (var h; !(h = d.next()).done;) this.add(h.value)
}
this.size = this.map_.size
};
c.prototype.add = function(d) {
d = 0 === d ? 0 : d;
this.map_.set(d, d);
this.size = this.map_.size;
return this
};
c.prototype.delete = function(d) {
d = this.map_.delete(d);
this.size = this.map_.size;
return d
};
c.prototype.clear = function() {
this.map_.clear();
this.size = 0
};
c.prototype.has =
function(d) {
return this.map_.has(d)
};
c.prototype.entries = function() {
return this.map_.entries()
};
c.prototype.values = function() {
return this.map_.values()
};
c.prototype.keys = c.prototype.values;
c.prototype[Symbol.iterator] = c.prototype.values;
c.prototype.forEach = function(d, h) {
var m = this;
this.map_.forEach(function(l) {
return d.call(h, l, l, m)
})
};
return c
}, "es6", "es3");
var EventEmitter = require("events"),
OPEN_BRACE = 123,
CLOSE_BRACE = 125,
OPEN_BRACKET = 91,
CLOSE_BRACKET = 93,
QUOTE = 34,
SPACE = 32,
NEW_LINE = 10,
CARRIAGE_RETURN = 13,
TAB = 9,
COLON = 58,
COMMA = 44,
BACKSLASH = 92,
FastJson = function(a) {
this._options = a || {};
this._stack = Array(16);
this._level = -1;
this._postColon = !1;
this._lastString = null;
this._events = new EventEmitter;
this._subPaths = new Set(["/"]);
this._isString = !1
};
FastJson.prototype.on = function(a, b) {
a = this._processPath(a);
this._addToSubPaths(a);
this._events.on(a, b)
};
FastJson.prototype.write = function(a) {
this._isString = "string" === typeof a;
for (var b = 0; b < a.length; b++) switch (this._get(a, b)) {
case OPEN_BRACE:
b = this._onOpenBrace(a, b);
break;
case OPEN_BRACKET:
b = this._onOpenBracket(a, b);
break;
case CLOSE_BRACE:
case CLOSE_BRACKET:
this._onCloseBraceOrBracket(a, b);
break;
case QUOTE:
b = this._onQuote(a, b);
break;
case TAB:
case CARRIAGE_RETURN:
case NEW_LINE:
case SPACE:
break;
case COLON:
this._postColon = !0;
break;
case COMMA:
this._onComma();
break;
default:
b = this._onPrimitive(a, b)
}
};
FastJson.prototype._onOpenBrace = function(a, b) {
this._level++;
var c = this._resolveParentKey(a),
d = this._resolvePath(c);
if (!this._subPaths.has(d)) return this._level--, this._skipBlock(a, b, OPEN_BRACE, CLOSE_BRACE);
this._stack[this._level] = {
type: "object",
start: b,
parent: c,
path: d
};
this._postColon = !1;
return b
};
FastJson.prototype._onOpenBracket = function(a, b) {
this._level++;
var c = this._resolveParentKey(a),
d = this._resolvePath(c);
if (!this._subPaths.has(d)) return this._level--, this._skipBlock(a, b, OPEN_BRACKET, CLOSE_BRACKET);
this._stack[this._level] = {
type: "array",
start: b,
parent: c,
path: d,
index: 0
};
this._postColon = !1;
return b
};
FastJson.prototype._onCloseBraceOrBracket = function(a, b) {
var c = this._stack[this._level];
c.end = b;
this._hasListeners(c.path) && (a = a.slice(c.start, c.end + 1), this._events.emit(c.path, a));
this._level--
};
FastJson.prototype._onQuote = function(a, b) {
var c = this._stack[this._level],
d = {
start: b + 1,
end: this._parseString(a, b)
};
this._postColon ? (c = this._resolvePathForPrimitiveObject(a), this._hasListeners(c) && this._events.emit(c, a.slice(d.start, d.end))) : "array" === c.type && (c = this._resolvePathForPrimitiveArray(a), this._hasListeners(c) && this._events.emit(c, a.slice(d.start, d.end)));
this._postColon = !1;
this._lastString = d;
return b + (d.end - d.start + 1)
};
FastJson.prototype._onComma = function() {
var a = this._stack[this._level];
"array" === a.type && a.index++
};
FastJson.prototype._onPrimitive = function(a, b) {
var c = this._stack[this._level],
d = this._parsePrimitive(a, b);
this._postColon ? (c = this._resolvePathForPrimitiveObject(a), this._hasListeners(c) && this._events.emit(c, a.slice(b, d))) : "array" === c.type && (c = this._resolvePathForPrimitiveArray(a), this._hasListeners(c) && this._events.emit(c, a.slice(b, d)));
this._postColon = !1;
return b + (d - b - 1)
};
FastJson.prototype._skipBlock = function(a, b, c, d) {
var h = 1;
for (b++;;) {
switch (this._get(a, b)) {
case QUOTE:
var m = this._parseString(a, b);
b += m - b;
break;
case c:
h++;
break;
case d:
if (h--, 0 === h) return b
}
b++
}
};
FastJson.prototype._parseString = function(a, b) {
for (b++;;) {
switch (this._get(a, b)) {
case QUOTE:
return b;
case BACKSLASH:
b++
}
b++
}
};
FastJson.prototype._parsePrimitive = function(a, b) {
for (;;) {
switch (this._get(a, b)) {
case CLOSE_BRACKET:
case CLOSE_BRACE:
case COMMA:
return b
}
b++
}
};
FastJson.prototype._resolveParentKey = function(a) {
return 0 >= this._level ? "/" : "array" === this._stack[this._level - 1].type ? this._stack[this._level - 1].index : this._toString(a, this._lastString.start, this._lastString.end)
};
FastJson.prototype._resolvePath = function(a) {
return 0 === this._level ? a : 1 === this._level ? this._stack[this._level - 1].path + a : this._stack[this._level - 1].path + "/" + a
};
FastJson.prototype._hasListeners = function(a) {
return 0 < this._events.listenerCount(a)
};
FastJson.prototype._resolvePathForPrimitiveObject = function(a) {
return 0 === this._level ? "/" + this._toString(a, this._lastString.start, this._lastString.end) : this._stack[this._level].path + "/" + this._toString(a, this._lastString.start, this._lastString.end)
};
FastJson.prototype._resolvePathForPrimitiveArray = function(a) {
return 0 === this._level ? "/" + this._stack[this._level].index : this._stack[this._level].path + "/" + this._stack[this._level].index
};
FastJson.prototype._processPath = function(a) {
if (!a) return "/";
a = a.replace(/[.[]/g, "/");
a = a.replace(/]/g, "");
return "/" !== a[0] ? "/" + a : a
};
FastJson.prototype._addToSubPaths = function(a) {
var b = "";
a = a.split("/");
for (var c = 1; c < a.length; c++) b += "/" + a[c], this._subPaths.add(b)
};
FastJson.prototype._get = function(a, b) {
return this._isString ? a.charCodeAt(b) : a[b]
};
FastJson.prototype._toString = function(a, b, c) {
return this._isString ? a.slice(b, c) : a.toString(void 0, b, c)
};
module.exports = FastJson;`
How do I get multiple values out of a json string?
According to the documentation this is possible:
// Paths can be also an array of keys
fastJson.on(['spain', 'people', '1', 'name'], (value) => {
console.log(['spain', 'people', '1', 'name'], '->', value);
});
but when i use it this way, it doesnt work:
// Paths can be also an array of keys
fastJson.on(['data.spain', 'something.people', 'other.name'], (value) => {
console.log(value);
});
i need to substract values at the same time and i'd need to use it this way.
But this never fires. Can you suggest me a way to do this?
Does buffer can be a stream-reader?
So it can parse large json stream from file or http
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.