stream-stringify
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -90,7 +90,10 @@ // Generated by CoffeeScript 1.9.2 | ||
StreamStringify._formatIfString = function(data) { | ||
if (typeof data === 'string' && data.length) { | ||
return "\"" + data + "\""; | ||
} else { | ||
return data; | ||
StreamStringify._formatEntity = function(data) { | ||
switch (typeof data) { | ||
case 'string' && data.length: | ||
return "\"" + data + "\""; | ||
case 'object': | ||
return JSON.stringify(data); | ||
default: | ||
return data; | ||
} | ||
@@ -113,3 +116,3 @@ }; | ||
} | ||
return this.write(this._applySeparator("" + (this._keyPrefix(key)) + (StreamStringify._formatIfString(value)))); | ||
return this.write(this._applySeparator("" + (this._keyPrefix(key)) + (StreamStringify._formatEntity(value)))); | ||
}; | ||
@@ -122,3 +125,3 @@ | ||
} | ||
return this.write(this._applySeparator(StreamStringify._formatIfString(data))); | ||
return this.write(this._applySeparator(StreamStringify._formatEntity(data))); | ||
}; | ||
@@ -125,0 +128,0 @@ |
{ | ||
"name": "stream-stringify", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Stringify nested JSON through streams.", | ||
@@ -5,0 +5,0 @@ "main": "lib/stream-stringify.js", |
@@ -1,5 +0,7 @@ | ||
Create streams for nested JSON structures of which you don't know about on beforehand. Cheaper than `JSON.stringify` which always will buffer the entire object. | ||
Create streams for nested JSON structures of which you don't know about on beforehand. | ||
## Example | ||
Cheaper than `JSON.stringify` which always will buffer the entire object. | ||
# Example | ||
``` | ||
@@ -12,3 +14,3 @@ jsonString = '' | ||
innerArray.writeElement 1 | ||
innerArray.writeElement 2 | ||
innerArray.writeElement [2,3] | ||
innerArray.stop() | ||
@@ -20,12 +22,13 @@ nestedObject.writeKeyValue "anotherInnerKey", "anotherInnerValue" | ||
`result` is now: | ||
`jsonString` is now: | ||
``` | ||
{ | ||
"firstKey": "firstValue", | ||
"firstKey": firstValue, | ||
"nestedObject": { | ||
"innerArray": [ | ||
1 | ||
1, | ||
[2,3] | ||
], | ||
"anotherInnerKey": "anotherInnerValue" | ||
"anotherInnerKey": anotherInnerValue | ||
} | ||
@@ -35,3 +38,3 @@ } | ||
## Functionality | ||
# Functionality | ||
@@ -41,3 +44,3 @@ This stream supports functionality found in most streams, such as `write()`, `end()`, or ` | ||
### sstringify(options) | ||
## sstringify(options) | ||
@@ -53,24 +56,24 @@ Constructs the root of the JSON structure. When specified, the following options change the behaviour of the stream: | ||
### startNewObject(key) | ||
## startNewObject(key) | ||
Starts a new object stream which is connected to the specfied `key`. Returns the stream. When using an array, do not specify `key`. | ||
### startNewArray(key) | ||
## startNewArray(key) | ||
Starts a new array stream which is connected to the specfied `key`. Returns the stream. When using an array, do not specify `key`. | ||
### stop() | ||
## stop() | ||
Notifies the stream that there won't be more information. This will close the bracket. The stream will automatically be ended and destroyed when not used by other streams (unless `autoDestroy=false`). | ||
### writeKeyValue(key,value) | ||
## writeKeyValue(key,value) | ||
Writes a key and a value to the stream. Does not work when working with an array. | ||
### writeElement(element) | ||
## writeElement(element) | ||
Writes an element to the stream. Does not work when working with an object. | ||
## Set it off! | ||
# Set it off! | ||
```npm install stream-stringify``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28029
253
75