@cio/oboe
Use Oboe.js to parse a stream of JSON objects.
An addon for the cio module which uses Oboe.
Although this library is capable of using all Oboe's features, it focuses on using it to read a series of root level objects and provide those objects one at a time without building up data in memory.
Other JSON communications separate JSON objects with newline characters. That prevents sending "pretty" JSON. Oboe, on the other hand, is capable of reading "pretty" JSON as well as minified JSON. This allows debugging to monitor communications and display the content in pretty mode for developers. In production, the JSON can be produced in its minified style.
Install
npm install @cio/oboe --save
Usage: Use in CIO
See cio
var buildCio = require('cio')
, cio = buildCio();
cio.onClient('@cio/oboe');
cio.onServerClient('@cio/oboe');
var cioOptions = {
oboe: {
root: function(object) {
},
top: {
header: function(header) { },
thing : function(thing) { },
footer: function(footer) { }
}
}
};
var client = cio.client(cioOptions)
, server = cio.server(cioOptions);
cioOptions = {
oboe: function(root) {
}
};
Usage: Client
Receive JSON from a Server.
var cio = require('cio')();
var options = {
port: 12345
, host: 'localhost'
, oboe: function (rootObject) { }
};
var client = cio.client(options);
client.on('oboe', function(oboe, client) {
});
Usage: Server
Receive JSON from a Client.
var cio = require('cio')();
var options = {
oboe: function (rootObject) { }
};
var server = cio.server(options);
server.on('oboe', function(oboe, serverClient) {
});
server.listen();
Usage: Configure Oboe
var oboeOptions = ;
var buildCio = require('cio')
, cio = buildCio({ oboe: oboeOptions });
cio.onClient('@cio/oboe');
cio.onServerClient('@cio/oboe');
var client = cio.client({
port: 1357
, host: 'localhost'
, oboe: oboeOptions
});
client.on('oboe', function(oboe, client) {
});
Usage: Configuration Options
Use the below options to control how this library configures an Oboe
instance.
These are a mix of Oboe's API and a few convenience things to help ease its use.
Property | Oboe? | Purpose |
---|
root | No | Specify a function to call for each root object |
top | No | Convenience for matching top level nodes with a specific property. Shorthand for node keys by prepending '!.' to them. See Example Configuration |
node | Yes | Add patterns to match keys and retrieve their values. |
path | Yes | Listen for parsing errors. This library does that for |
fail | Yes | Listen for parsing errors. This library does that for you and emits it as an 'error' event. Feel free to listen to 'fail' as well... |
done | Yes | Normally used to get the entire JS object at the end, which this library is intending to avoid. |
Below are examples of objects and how to configure functions for processing them.
Simple Objects
A simple object doesn't have a hierarchy, it doesn't "contain" another object. It's a 'root' object. It's not a 'top level' object.
Basically, use 'root' to process these.
{ simple: 'object' }
Labeled Objects
This object is what I'll call "labeled". It is an object with one property which contains another object. The label here is 'label'.
Or course, this is also a 'root' object with a 'label' property.
Use 'root' to process the entire object and use 'top' with label 'label' to process the object value of the label.
{ label: { labeled: 'object' } }
This has a 'top level' object labeled 'person'.
Use 'root' to process the entire object with the label.
Use 'top' to process the object value of the label.
{
person: {
name: 'John',
address: {
street: '123 Somewhere St.',
city: 'Hometown',
state: 'ZZ',
zip: '12345'
}
}
}
Multiple Labels
Process multiple top level properties.
{
header: { some: 'header stuff' },
body : { some: 'body content' },
footer: { some: 'footer' }
}
Example Configuration
Here are options to work with the above objects.
var options = {
oboe: {
root: function(object) { console.log('root:',object); },
top: {
label: function(object) { console.log('top/label:',object); },
person: function(person) { console.log('top/person:',person); },
header: function(header) { }
body : function(header) { }
footer: function(footer) { }
},
node: {
'pattern': function(arg) { },
'pattern2': function(arg) { },
'!.label': function(object) { console.log('node/label',object); },
'!.person': function(object) { console.log('node/person',object); }
},
path: {
}
}
};
Here's a small example without all the comments in it.
var objects = [
{ data: { _id:'8286781819263' time: 111, some: 'value' } }
{ data: { _id:'2067101738684' time: 222, some: 'stuff' } }
{ data: { _id:'9828223482106' time: 135, some: 'data' } }
{ data: { _id:'9907672385621' time: 246, some: 'object' } }
{ data: { _id:'8838283568863' time: 975, some: 'example' } }
{ data: { _id:'2282082825680' time: 321, some: 'test' } }
];
var client = cio.client({
port: 12345
, host: 'localhost'
, oboe: {
top: {
data: function (data) {
console.log('data provides some',data.some);
}
}
}
});
MIT License