Skip to content

Commit

Permalink
lib/chunk2json.js: Fixed issues with escaped quotes inside strings va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
jahnestacado authored and Ioannis Tzanellis committed Apr 25, 2016
1 parent c369ac3 commit 7264231
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/chunk2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ var rightCurlyCode = "}".charCodeAt(0);
var quoteCode = '"'.charCodeAt(0);
var leftSquareBracketCode = "[".charCodeAt(0);
var rightSquareBracketCode = "]".charCodeAt(0);
var backslashCode = '\\'.charCodeAt(0);

var Parser = function(){
this.curlyCounter = 0;
this.packet = [];
this.inQuoteMode = false;
this.isArray = false;
this.previousByte;
};

Parser.prototype.consume = function(buffer){
Expand All @@ -33,7 +35,9 @@ Parser.prototype.consume = function(buffer){
!parser.inQuoteMode && parser.curlyCounter--;
break;
case quoteCode:
parser.inQuoteMode = !parser.inQuoteMode;
if(parser.previousByte !== backslashCode){
parser.inQuoteMode = !parser.inQuoteMode;
}
break;
}
parser.packet.push(byte);
Expand All @@ -57,6 +61,7 @@ Parser.prototype.consume = function(buffer){
}
parser.packet.push(byte);
}
parser.previousByte = byte;
}

function extract(packet){
Expand Down
12 changes: 6 additions & 6 deletions test/lib/chunk2json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ describe("when testing chunk2json module", function(){
new Buffer(' "popup": {'),
new Buffer(' "menuitem": ['),
new Buffer(' {"value": "Ope{{{n", "oncl}}}}ick": "OpenDoc()"},'),
new Buffer(' {"va[lue": "Cl]ose", "onclick": "CloseDoc()"}'),
new Buffer(' {"va[lue": "Cl]ose", "onclick": "CloseD \\" inner quotes \\"oc()"}'),
new Buffer(' ]'),
new Buffer(' }'),
new Buffer('}}'),
new Buffer('{"name": "json-packet-parser","repository": {"type": "git","url": "https://github.com/jahnestacado/json-packet-parser"}}'),
new Buffer('[{"name": "json-packet-parser","repository": {"type": "git","url": "https://github.com/jahnestacado/json-packet-parser"}}]'),
new Buffer('{"name": "chunk2json","repository": {"type": "git","url": "https://github.com/jahnestacado/chunk2json"}}'),
new Buffer('[{"name": "chunk2json","repository \\" inner quotes \\"": {"type": "git","url": "https://github.com/jahnestacado/chunk2json"}}]'),
new Buffer('[ {"menu": {'),
new Buffer(' "id": "file",'),
new Buffer(' "value": "File",'),
new Buffer(' "popup": {'),
new Buffer(' "menuitem": ['),
new Buffer(' {"value": "Ope{{{n", "oncl}}}}ick": "OpenDoc()"},'),
new Buffer(' {"va[lue": "Cl]ose", "onclick": "CloseDoc()"}'),
new Buffer(' {"va[lue": "Cl]ose", "onclic\\" inner quotes \\"k": "CloseDoc()"}'),
new Buffer(' ]'),
new Buffer(' }'),
new Buffer('}}]')
Expand Down Expand Up @@ -59,11 +59,11 @@ describe("when testing chunk2json module", function(){
});

it("should have in packets[1] the expected JSON object", function(){
assert.equal(JSON.parse(packets[1]).name, "json-packet-parser");
assert.equal(JSON.parse(packets[1]).name, "chunk2json");
});

it("should have in packets[2] the expected JSON object", function(){
assert.equal(JSON.parse(packets[2])[0].name, "json-packet-parser");
assert.equal(JSON.parse(packets[2])[0].name, "chunk2json");
});

it("should have in packets[3] the expected JSON object", function(){
Expand Down

0 comments on commit 7264231

Please sign in to comment.