public
Last active

event-only version of jsonparse

  • Download Gist
jsonparse.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
// Named constants with unique integer values
var C = {};
// Tokenizer States
var START = C.START = 0x11;
var TRUE1 = C.TRUE1 = 0x21;
var TRUE2 = C.TRUE2 = 0x22;
var TRUE3 = C.TRUE3 = 0x23;
var FALSE1 = C.FALSE1 = 0x31;
var FALSE2 = C.FALSE2 = 0x32;
var FALSE3 = C.FALSE3 = 0x33;
var FALSE4 = C.FALSE4 = 0x34;
var NULL1 = C.NULL1 = 0x41;
var NULL2 = C.NULL3 = 0x42;
var NULL3 = C.NULL2 = 0x43;
var NUMBER1 = C.NUMBER1 = 0x51;
var NUMBER2 = C.NUMBER2 = 0x52;
var NUMBER3 = C.NUMBER3 = 0x53;
var NUMBER4 = C.NUMBER4 = 0x54;
var NUMBER5 = C.NUMBER5 = 0x55;
var NUMBER6 = C.NUMBER6 = 0x56;
var NUMBER7 = C.NUMBER7 = 0x57;
var NUMBER8 = C.NUMBER8 = 0x58;
var STRING1 = C.STRING1 = 0x61;
var STRING2 = C.STRING2 = 0x62;
var STRING3 = C.STRING3 = 0x63;
var STRING4 = C.STRING4 = 0x64;
var STRING5 = C.STRING5 = 0x65;
var STRING6 = C.STRING6 = 0x66;
 
// Slow code to string converter (only used when throwing syntax errors)
function toknam(code) {
var keys = Object.keys(C);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (C[key] === code) { return key; }
}
return code && ("0x" + code.toString(16));
}
 
function SaxParser(callbacks) {
this.callbacks = callbacks;
 
this.state = START;
 
// for string parsing
this.string = undefined; // string data
this.unicode = undefined; // unicode escapes
 
// For number parsing
this.negative = undefined;
this.magnatude = undefined;
this.position = undefined;
this.exponent = undefined;
this.negativeExponent = undefined;
}
var proto = SaxParser.prototype;
proto.charError = function (buffer, i) {
this.callbacks.onError(new Error("Unexpected " + JSON.stringify(String.fromCharCode(buffer[i])) + " at position " + i + " in state " + toknam(this.state)));
};
proto.parse = function (buffer) {
if (typeof buffer === "string") buffer = new Buffer(buffer);
var n;
for (var i = 0, l = buffer.length; i < l; i++) {
switch (this.state) {
case START:
n = buffer[i];
switch (n) {
case 0x7b: // `{`
this.callbacks.onStartObject();
continue;
case 0x7d: // `}`
this.callbacks.onEndObject();
continue;
case 0x5b: // `[`
this.callbacks.onStartArray();
continue;
case 0x5d: // `]`
this.callbacks.onEndArray();
continue;
case 0x3a: // `:`
this.callbacks.onColon();
continue;
case 0x2c: // `,`
this.callbacks.onComma();
continue;
case 0x74: // `t`
this.state = TRUE1;
continue;
case 0x66: // `f`
this.state = FALSE1;
continue;
case 0x6e: // `n`
this.state = NULL1;
continue;
case 0x22: // `"`
this.string = "";
this.state = STRING1;
continue;
case 0x2d: // `-`
this.negative = true;
this.state = NUMBER1;
continue;
case 0x30: // `0`
this.magnatude = 0;
this.state = NUMBER2;
continue;
}
if (n > 0x30 && n < 0x40) { // 1-9
this.magnatude = n - 0x30;
this.state = NUMBER3;
continue;
}
if (n === 0x20 || n === 0x09 || n === 0x0a || n === 0x0d) {
continue; // whitespace
}
this.charError(buffer, i);
case STRING1: // After open quote
n = buffer[i];
switch (n) {
case 0x22: // `"`
this.callbacks.onString(this.string);
this.string = undefined;
this.state = START;
continue;
case 0x5c: // `\`
this.state = STRING2;
continue;
}
if (n >= 0x20) {
this.string += String.fromCharCode(n);
continue;
}
this.charError(buffer, i);
case STRING2: // After backslash
n = buffer[i];
switch (n) {
case 0x22: this.string += "\""; this.state = STRING1; continue;
case 0x5c: this.string += "\\"; this.state = STRING1; continue;
case 0x2f: this.string += "\/"; this.state = STRING1; continue;
case 0x62: this.string += "\b"; this.state = STRING1; continue;
case 0x66: this.string += "\f"; this.state = STRING1; continue;
case 0x6e: this.string += "\n"; this.state = STRING1; continue;
case 0x72: this.string += "\r"; this.state = STRING1; continue;
case 0x74: this.string += "\t"; this.state = STRING1; continue;
case 0x75: this.unicode = ""; this.state = STRING3; continue;
}
this.charError(buffer, i);
case STRING3: case STRING4: case STRING5: case STRING6: // unicode hex codes
n = buffer[i];
// 0-9 A-F a-f
if ((n >= 0x30 && n < 0x40) || (n > 0x40 && n <= 0x46) || (n > 0x60 && n <= 0x66)) {
this.unicode += String.fromCharCode(n);
if (this.state++ === STRING6) {
this.string += String.fromCharCode(parseInt(this.unicode, 16));
this.unicode = undefined;
this.state = STRING1;
}
continue;
}
this.charError(buffer, i);
case NUMBER1: // after minus
n = buffer[i];
if (n === 0x30) { // `0`
this.magnatude = 0;
this.state = NUMBER2;
continue;
}
if (n > 0x30 && n < 0x40) { // `1`-`9`
this.magnatude = n - 0x30;
this.state = NUMBER3;
continue;
}
this.charError(buffer, i);
case NUMBER2: // * After initial zero
switch (buffer[i]) {
case 0x2e: // .
this.position = 0.1; this.state = NUMBER4; continue;
case 0x65: case 0x45: // e/E
this.exponent = 0; this.state = NUMBER6; continue;
}
this.finish();
i--; // rewind to re-check this char
continue;
case NUMBER3: // * After digit (before period)
n = buffer[i];
switch (n) {
case 0x2e: // .
this.position = 0.1; this.state = NUMBER4; continue;
case 0x65: case 0x45: // e/E
this.exponent = 0; this.state = NUMBER6; continue;
}
if (n >= 0x30 && n < 0x40) { // 0-9
this.magnatude = this.magnatude * 10 + (n - 0x30);
continue;
}
this.finish();
i--; // rewind to re-check
continue;
case NUMBER4: // After period
n = buffer[i];
if (n >= 0x30 && n < 0x40) { // 0-9
this.magnatude += this.position * (n - 0x30);
this.position /= 10;
this.state = NUMBER5;
continue;
}
this.charError(buffer, i);
case NUMBER5: // * After digit (after period)
n = buffer[i];
if (n >= 0x30 && n < 0x40) { // 0-9
this.magnatude += this.position * (n - 0x30);
this.position /= 10;
continue;
}
if (n === 0x65 || n === 0x45) { // E/e
this.exponent = 0;
this.state = NUMBER6;
continue;
}
this.finish();
i--; // rewind
continue;
case NUMBER6: // After E
n = buffer[i];
if (n === 0x2b || n === 0x2d) { // +/-
if (n === 0x2d) { this.negativeExponent = true; }
this.state = NUMBER7;
continue;
}
if (n >= 0x30 && n < 0x40) {
this.exponent = this.exponent * 10 + (n - 0x30);
this.state = NUMBER8;
continue;
}
this.charError(buffer, i);
case NUMBER7: // After +/-
n = buffer[i];
if (n >= 0x30 && n < 0x40) { // 0-9
this.exponent = this.exponent * 10 + (n - 0x30);
this.state = NUMBER8;
continue;
}
this.charError(buffer, i);
case NUMBER8: // * After digit (after +/-)
n = buffer[i];
if (n >= 0x30 && n < 0x40) { // 0-9
this.exponent = this.exponent * 10 + (n - 0x30);
continue;
}
this.finish();
i--;
continue;
case TRUE1: // r
if (buffer[i] === 0x72) {
this.state = TRUE2;
continue;
}
this.charError(buffer, i);
case TRUE2: // u
if (buffer[i] === 0x75) {
this.state = TRUE3;
continue;
}
this.charError(buffer, i);
case TRUE3: // e
if (buffer[i] === 0x65) {
this.state = START;
this.callbacks.onBoolean(true);
continue;
}
this.charError(buffer, i);
case FALSE1: // a
if (buffer[i] === 0x61) {
this.state = FALSE2;
continue;
}
this.charError(buffer, i);
case FALSE2: // l
if (buffer[i] === 0x6c) {
this.state = FALSE3;
continue;
}
this.charError(buffer, i);
case FALSE3: // s
if (buffer[i] === 0x73) {
this.state = FALSE4;
continue;
}
this.charError(buffer, i);
case FALSE4: // e
if (buffer[i] === 0x65) {
this.state = START;
this.callbacks.onBoolean(false);
continue;
}
this.charError(buffer, i);
case NULL1: // u
if (buffer[i] === 0x75) {
this.state = NULL2;
continue;
}
this.charError(buffer, i);
case NULL2: // l
if (buffer[i] === 0x6c) {
this.state = NULL3;
continue;
}
this.charError(buffer, i);
case NULL3: // l
if (buffer[i] === 0x6c) {
this.state = START;
this.callbacks.onNull();
continue;
}
this.charError(buffer, i);
}
}
};
 
proto.finish = function () {
switch (this.state) {
case NUMBER2: // * After initial zero
this.callbacks.onNumber(0);
this.state = START;
this.magnatude = undefined;
this.negative = undefined;
break;
case NUMBER3: // * After digit (before period)
this.state = START;
if (this.negative) {
this.magnatude = -this.magnatude;
this.negative = undefined;
}
this.callbacks.onNumber(this.magnatude);
this.magnatude = undefined;
break;
case NUMBER5: // * After digit (after period)
this.state = START;
if (this.negative) {
this.magnatude = -this.magnatude;
this.negative = undefined;
}
this.callbacks.onNumber(this.negative ? -this.magnatude : this.magnatude);
this.magnatude = undefined;
this.position = undefined;
break;
case NUMBER8: // * After digit (after +/-)
if (this.negativeExponent) {
this.exponent = -this.exponent;
this.negativeExponent = undefined;
}
this.magnatude *= Math.pow(10, this.exponent);
this.exponent = undefined;
if (this.negative) {
this.magnatude = -this.magnatude;
this.negative = undefined;
}
this.state = START;
this.callbacks.onNumber(this.magnatude);
this.magnatude = undefined;
break;
}
if (this.state !== START) {
this.callbacks.onError(new Error("Unexpected end of input stream"));
}
}
 
exports.SaxParser = SaxParser;
test-sax.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
var SaxParser = require('./jsonparse').SaxParser;
 
var p = new SaxParser({
onNull: function () {
console.log("onNull");
},
onBoolean: function (value) {
console.log("onBoolean", value);
},
onNumber: function (value) {
console.log("onNumber", value);
},
onString: function (value) {
console.log("onString", value);
},
onStartObject: function () {
console.log("onStartObject");
},
onColon: function () {
console.log("onColon");
},
onComma: function () {
console.log("onComma");
},
onEndObject: function () {
console.log("onEndObject");
},
onStartArray: function () {
console.log("onStartArray");
},
onEndArray: function () {
console.log("onEndArray");
}
});
 
//var stream = require('fs').createReadStream(__dirname + "/tests/sample.json");
//var parse = p.parse.bind(p);
//stream.on('data', parse);
//p.parse(new Buffer("{}"));
//p.parse(new Buffer("[]"));
//p.parse(new Buffer('"Hello"'));
p.parse(new Buffer("[1,2,3]"));
p.parse(new Buffer("-0.1e-3"));
p.parse(new Buffer('"Hello"'));
p.finish()
//p.parse(new Buffer('{"name":"tim"}'))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.