Skip to content

An extended implementation of BEncode for encoding typed JavaScript objects

License

Notifications You must be signed in to change notification settings

legendary-code/jsencode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsencode

build npm version

An implementation for encoding typed JavaScript objects, similar to BEncode

Example

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

Options

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

Encoding

Any

any := <string> | <number> | <boolean> | <dictionary> | <object> | <array> | <null>

Undefined

undefined := 'u'

Null

null := 'n'

String

string := <length> ':' <value>

length = any valid Number value that is an integer >= 0

value = any utf-8 encoded string value

Number

number := '(' <value> ')'

value = any valid Number value

Boolean

boolean := 't' | 'f'

Array

array := '[' <values> ']'

values := (<any>)*

Dictionary (Object with constructor Object)

dictionary := '{' <string-value-pairs> '}'

string-value-pairs := (string-value-pair)*

string-value-pair := <string><any>

Object (Object with registered constructor Function)

object := '<' <type> <string-value-pairs> '>'

type := <string>

string-value-pairs := (string-value-pair)*

string-value-pair := <string><any>

About

An extended implementation of BEncode for encoding typed JavaScript objects

Resources

License

Stars

Watchers

Forks

Packages

No packages published