node-json-writer
It's native and full javascript implementation of JSONWriter class. The API is complete and flexible. JSON is still valid.
Contributors
Installation
With npm do:
$ npm install json-writer
Examples
Basic
var JSONWriter = ; jw = ; jw; jw; jw; jw; jwtext'Some content'; jw; jw; console;
Output:
"version": "1.0" "encoding": "utf-8" "root": "foo": "value" "$t": "Some content"
Chaining
var JSONWriter = ; jw = ; jw; console;
Output:
"version": "1.0" "encoding": "utf-8" "root": "foo": "value" "tag": "$t": "Some content"
Extensible
var JSONWriter = fs = ; var ws = fs; ws; jw = false { ws; }; jwtext { return 'Some content'; }; ws;
Output:
"version": "1.0" "encoding": "UTF-8" "root": "$t": "Some content"
Tests
Use 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)