json-writer
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -26,12 +26,11 @@ 'use strict'; | ||
this.n = null; | ||
this.r = []; | ||
this.h = null; | ||
this.b = ''; | ||
this.i = indent; | ||
this.is = "\t"; | ||
if (typeof callback == 'function') { | ||
this.writer = callback; | ||
} else { | ||
this.writer = function (s, e) {}; | ||
} | ||
this.name_regex = /[_:A-Za-z][-._:A-Za-z0-9]*/; | ||
this.stack = []; | ||
this.document = {}; | ||
this.encoding = 'utf-8'; | ||
@@ -47,3 +46,3 @@ this.indent = indent ? true : false; | ||
this.indent = withindent; | ||
return true; | ||
return this; | ||
} | ||
@@ -56,3 +55,3 @@ | ||
this.indentchar = indentchar; | ||
return true; | ||
return this; | ||
} | ||
@@ -72,3 +71,3 @@ | ||
return true; | ||
return this; | ||
} | ||
@@ -153,6 +152,7 @@ /** | ||
standalone = standalone ? standalone : true; | ||
assert.equal(typeof version, 'string'); | ||
assert.equal(typeof encoding, 'string'); | ||
this.encoding = encoding; | ||
// document root is the first element in the stack | ||
@@ -172,2 +172,3 @@ var elt = { type: 'element', | ||
JSONWriter.prototype.endDocument = function () { | ||
this.writer(this.toString(), this.encoding); | ||
return this; | ||
@@ -375,3 +376,7 @@ } | ||
JSONWriter.prototype.text = function(content) { | ||
assert.equal(typeof content, 'string') | ||
// control parameter syntax | ||
content = strval(content); | ||
assert.equal(typeof content, 'string'); | ||
var elt = this.stack.pop(); | ||
@@ -378,0 +383,0 @@ if ( elt.type == 'attribute' || elt.type == 'cdata' ) { |
{ | ||
"name" : "json-writer", | ||
"version" : "1.0.0", | ||
"version" : "1.0.1", | ||
"author" : "Nicolas Thouvenin <nthouvenin@gmail.com>", | ||
@@ -5,0 +5,0 @@ "contributors" : [ ], |
201
README.md
node-json-writer | ||
================ | ||
================ | ||
It's native and full javascript implementation of JSONWriter class. | ||
The API is complete and flexible. | ||
JSON is still valid. | ||
## Contributors | ||
* [Nicolas Thouvenin](https://github.com/touv) | ||
* [Agostinho Quintela](https://github.com/agoqui) | ||
* [Stéphane Gully](https://github.com/kerphi) | ||
# Installation | ||
With [npm](http://npmjs.org) do: | ||
$ npm install json-writer | ||
# Examples | ||
## Basic | ||
```javascript | ||
var JSONWriter = require('json-writer'); | ||
jw = new JSONWriter; | ||
jw.setIndent(true); | ||
jw.startDocument(); | ||
jw.startElement('root'); | ||
jw.writeAttribute('foo', 'value'); | ||
jw.text('Some content'); | ||
jw.endElement(); | ||
jw.endDocument(); | ||
console.log(jw.toString()); | ||
``` | ||
Output: | ||
```javascript | ||
{ | ||
"version": "1.0", | ||
"encoding": "utf-8", | ||
"root": { | ||
"foo": "value", | ||
"$t": "Some content" | ||
} | ||
} | ||
``` | ||
## Chaining | ||
```javascript | ||
var JSONWriter = require('json-writer'); | ||
jw = new JSONWriter; | ||
jw.startDocument().startElement('root').writeAttribute('foo', 'value').writeElement('tag', 'Some content').endAttribute().endElement().endDocument(); | ||
console.log(jw.toString()); | ||
``` | ||
Output: | ||
```javascript | ||
{ | ||
"version": "1.0", | ||
"encoding": "utf-8", | ||
"root": { | ||
"foo": "value", | ||
"tag": { | ||
"$t": "Some content" | ||
} | ||
} | ||
} | ||
``` | ||
## Extensible | ||
```javascript | ||
var JSONWriter = require('json-writer'), | ||
fs = require('fs'); | ||
var ws = fs.createWriteStream('/tmp/foo.json'); | ||
ws.on('close', function() { | ||
console.log(fs.readFileSync('/tmp/foo.json', 'UTF-8')); | ||
}); | ||
jw = new JSONWriter(false, function(string, encoding) { | ||
ws.write(string, encoding); | ||
}); | ||
jw.startDocument('1.0', 'UTF-8').startElement(function() { | ||
return 'root'; | ||
}).text(function() { | ||
return 'Some content'; | ||
}).endElement().endDocument(); | ||
ws.end(); | ||
``` | ||
Output: | ||
```javascript | ||
{ | ||
"version": "1.0", | ||
"encoding": "UTF-8", | ||
"root": { | ||
"$t": "Some content" | ||
} | ||
} | ||
``` | ||
# Tests | ||
Use [nodeunit](https://github.com/caolan/nodeunit) to run the tests. | ||
$ npm install nodeunit | ||
$ nodeunit test | ||
# API Documentation | ||
## Generic | ||
### constructor JSONWriter(Boolean indent, Function writer(string, encoding)) | ||
Create an new writer | ||
### text(String content) | ||
Write text | ||
## Document | ||
### startDocument(String version = '1.0', String encoding = NULL, Boolean standalone = false) | ||
Create document tag | ||
### endDocument() | ||
End current document | ||
## Element | ||
### writeElement(String name, String content) | ||
Write full element tag | ||
### writeElementNS | ||
Write full namespaced element tag | ||
### startElementNS | ||
Create start namespaced element tag | ||
### startElement(String name) | ||
Create start element tag | ||
### endElement() | ||
End current element | ||
## Attributes | ||
### writeAttribute(String name, String value) | ||
Write full attribute | ||
### writeAttributeNS | ||
Write full namespaced attribute | ||
### startAttributeNS | ||
Create start namespaced attribute | ||
### startAttribute(String name) | ||
Create start attribute | ||
### endAttribute() | ||
End attribute | ||
## Processing Instruction | ||
### writePI(String name, String content) | ||
Writes a PI | ||
### startPI(String name) | ||
Create start PI tag | ||
### endPI() | ||
End current PI | ||
## CData | ||
### writeCData(String name, String content) | ||
Write full CDATA tag | ||
### startCData(String name) | ||
Create start CDATA tag | ||
### endCData() | ||
End current CDATA | ||
## Comment | ||
### writeComment(String content) | ||
Do nothing (just here for compatibility with XMLWriter) | ||
### startComment() | ||
Do nothing (just here for compatibility with XMLWriter) | ||
### endComment() | ||
Do nothing (just here for compatibility with XMLWriter) | ||
# Also | ||
* https://github.com/lindory-project/node-xml-writer | ||
# License | ||
[MIT/X11](./LICENSE) |
@@ -490,1 +490,29 @@ 'use strict'; | ||
} | ||
exports['Callback'] = function (test) { | ||
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().endDocument(); | ||
ws.end(); | ||
} | ||
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
34298
866
202
1