An implementation for encoding typed JavaScript objects, similar to BEncode
var JSEncoder = require('jsencode');
var Foo = require('foo');
var encoder = new JSEncoder({types:[Foo]}); // or, encoder.registerTypes(Foo, Bar, ...);
encoder.encode("foo"); // returns "3:foo"
encoder.encode({foo: "bar"}); // returns "{3:foo3:bar}"
encoder.encode(new Foo()); // returns "<3:Foo>"
encoder.decode("3:foo"); // returns "foo"
encoder.decode("{3:foo3:bar}"); // returns {foo: "bar"}
encoder.decode("<3:Foo>"); // returns Foo() instance
types
- specify a list of type constructors to register for deserializing Objects
includePrivateFields
- allows exclusion of fields that start with '_'
ignoreUnregisteredTypes
- ignore unregistered types, deserializing it as a null value instead
any := <string> | <number> | <boolean> | <dictionary> | <object> | <array> | <null>
undefined := 'u'
null := 'n'
string := <length> ':' <value>
length = any valid Number value that is an integer >= 0
value = any utf-8 encoded string value
number := '(' <value> ')'
value = any valid Number value
boolean := 't' | 'f'
array := '[' <values> ']'
values := (<any>)*
dictionary := '{' <string-value-pairs> '}'
string-value-pairs := (string-value-pair)*
string-value-pair := <string><any>
object := '<' <type> <string-value-pairs> '>'
type := <string>
string-value-pairs := (string-value-pair)*
string-value-pair := <string><any>