json-writer
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -11,3 +11,3 @@ 'use strict'; | ||
} | ||
else if (s instanceof XMLWriter) { | ||
else if (s instanceof JSONWriter) { | ||
return s.toString(); | ||
@@ -24,3 +24,3 @@ } | ||
if (!(this instanceof JSONWriter)) { | ||
return new JSONLWriter(); | ||
return new JSONWriter(); | ||
} | ||
@@ -40,2 +40,3 @@ | ||
this.indentchar = ' '; | ||
this.finished = false; | ||
} | ||
@@ -163,3 +164,3 @@ | ||
this.stack.push(elt); | ||
this.finished = false; | ||
return this; | ||
@@ -171,4 +172,8 @@ } | ||
JSONWriter.prototype.endDocument = function () { | ||
this.writer(this.toString(), this.encoding); | ||
if (!this.finished) { | ||
this.writer(this.toString(), this.encoding); | ||
this.finished = true; | ||
} | ||
return this; | ||
} | ||
@@ -229,8 +234,10 @@ | ||
var elt2_name = elt2[0]; | ||
var enddoc = false; | ||
// test if document root | ||
if(this.stack.length == 0) { | ||
elt1.value[elt2.name] = elt2.value; | ||
enddoc = true; | ||
} else { // not the document root | ||
enddoc = false; | ||
// if element name already exists | ||
@@ -256,2 +263,4 @@ if (elt1.value[elt2.name]) { | ||
this.stack.push(elt1); | ||
if(enddoc) { this.endDocument() } | ||
return this; | ||
@@ -258,0 +267,0 @@ } |
{ | ||
"name" : "json-writer", | ||
"version" : "1.0.1", | ||
"version" : "1.0.2", | ||
"author" : "Nicolas Thouvenin <nthouvenin@gmail.com>", | ||
@@ -5,0 +5,0 @@ "contributors" : [ ], |
@@ -518,1 +518,32 @@ 'use strict'; | ||
exports['Tolerant'] = function (test) { | ||
// test.ok(this.jw.setIndent(true)); | ||
var fs = require('fs'); | ||
var ws = fs.createWriteStream('/tmp/foo.json'); | ||
ws.on('close', function() { | ||
// string should be JSON formated | ||
try { | ||
var r = JSON.parse(fs.readFileSync('/tmp/foo.json', 'UTF-8')); | ||
test.ok(r['root']); | ||
} catch(err) { | ||
test.equal(err, null, 'JSONWriter should send JSON to the callback'); | ||
} | ||
fs.unlinkSync('/tmp/foo.json'); // cleanup | ||
test.done(); | ||
}); | ||
var jw = new JSONWriter(false, function(string, encoding) { | ||
ws.write(string, encoding); | ||
}); | ||
jw.setIndent(true).startDocument('1.0', 'UTF-8').startElement(function() { | ||
return 'root'; | ||
}).text(function() { | ||
return 'Some content'; | ||
}).endElement(); | ||
ws.end(); | ||
} | ||
35287
900
2