Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-writer

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-writer

Build JSON like XML

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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
  • Agostinho Quintela
  • Stéphane Gully

Installation

With npm do:

$ npm install json-writer

Examples

Basic

  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:

{
  "version": "1.0",
  "encoding": "utf-8",
  "root": {
    "foo": "value",
    "$t": "Some content"
  }
}

Chaining

  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:

 {
  "version": "1.0",
  "encoding": "utf-8",
  "root": {
    "foo": "value",
    "tag": {
      "$t": "Some content"
    }
  }
}

Extensible

  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:

{
  "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)

Also

License

MIT/X11

FAQs

Package last updated on 09 Aug 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc