Giter Club home page Giter Club logo

bert-js's Introduction

BERT-JS

What is BERT?

BERT (Binary ERlang Term) is a format created by the Erlang development team for serializing Erlang terms, and promoted by Tom Preston-Werner as a way for different languages to communicate in a simple and efficient manner.

Read Tom's Slides

What is BERT JS?

BERT-JS is a first cut Javascript implementation of the BERT protocol. In other words, using BERT-JS, you can serialize data into a binary format that can then be de-serialized by Erlang directly into an Erlang term. (Or, by Ruby, as Tom has written a BERT library for Ruby.)

Limitations

  • Decoding floats is not yet supported.

Interface

  • Bert.encode(Object) - Encode a Javascript object into BERT, return a String. The object can be a Boolean, Integer, Float, String, Array, Associative Array, or an Atom, Binary, or Tuple. (with the help of Bert.atom(), Bert.binary(), or Bert.tuple(), respectively).
  • Bert.decode(String) - Decode a BERT string into a Javascript object. Atoms, Binaries, and Tuples are special objects. See code for structure.
  • Bert.atom(String) - Create a Javascript object that will be encoded to an Atom.
  • Bert.binary(String) - Create a Javascript object that will be encoded to an Binary.
  • Bert.tuple(Element1, Element2, Element3, ...) - Create a Javascript object that will be encoded to a Tuple.

Examples

Note, below the return value is given in the form of an Erlang binary which can be fed into Erlang's binary_to_term/1. In reality, this returns a Javascript String with the ASCII values of the binary.

Bert.encode(Bert.atom("hello"));
Returns: <<131,100,0,5,104,101,108,108,111>>
Erlang: hello

Bert.encode(Bert.binary("hello"));
Returns: <<131,109,0,0,0,5,104,101,108,108,111>>
Erlang: <<"hello">>

Bert.encode(true);
Returns: <<131,100,0,4,116,114,117,101>>
Erlang: true

Bert.encode(42);
Returns: <<131,97,42>>
Erlang: 42

Bert.encode(5000);
Returns: <<131,98,0,0,19,136>>
Erlang: 5000

Bert.encode(-5000);
Returns: <<131,98,255,255,236,120>>
Erlang: -5000

Bert.encode(987654321);
Returns: <<131,110,4,0,177,104,222,58>>
Erlang: 987654321

Bert.encode(-987654321);
Returns: <<131,110,4,1,177,104,222,58>>
Erlang: -987654321

Bert.encode(3.14159);
Returns: <<131,99,51,46,49,52,49,53,57,101,43,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>
Erlang: 3.14159

Bert.encode(-3.14159);
Returns: <<131,99,45,51,46,49,52,49,53,57,101,43,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>
Erlang: -3.14159

Bert.encode([1, 2, 3]);
Returns: <<131,108,0,0,0,3,97,1,97,2,97,3,106>>
Erlang: [1,2,3]

Bert.encode({a:1, b:2, c:3});
Returns: <<131,108,0,0,0,3,104,2,100,0,1,97,97,1,104,2,100,0,1,98,97,2,104,2,100,0,1,99,97,3,106>>
Erlang: [{a,1},{b,2},{c,3}]

Bert.encode(Bert.tuple("Hello", 1));
Returns: <<131,104,2,107,0,5,72,101,108,108,111,97,1>>
Erlang: {"Hello",1}

Bert.encode({
	a : Bert.tuple(1, 2, 3),
	b : [4,5,6]
});
Returns: <<131,108,0,0,0,2,104,2,100,0,1,97,104,3,97,1,97,2,97,3,104,2,100,0,1,98,108,0,0,0,3,97,4,97,5,97,6,106,106>>
Erlang: [{a,{1,2,3}},{b,[4,5,6]}]

var S = Bert.bytes_to_string([131,108,0,0,0,3,104,2,100,0,4,97,116,111,109,100,0,6,109,121,65,116,111,109,
    104,2,100,0,6,98,105,110,97,114,121,109,0,0,0,9,77,121,32,66,105,110,97,114,
    121,104,2,100,0,4,98,111,111,108,100,0,4,116,114,117,101,106]);
var Obj = Bert.decode(S);
Object is equiv to: [{atom,myAtom},{binary,<<"My Binary">>},{bool,true},{string,"Hello there"}]

var S = Bert.bytes_to_string([131,108,0,0,0,5,104,2,100,0,13,115,109,97,108,108,95,105,110,116,101,103,101,
    114,97,42,104,2,100,0,8,105,110,116,101,103,101,114,49,98,0,0,19,136,104,2, 100,0,8,105,110,116,101,103,
    101,114,50,98,255,255,236,120,104,2,100,0,8,98,105,103,95,105,110,116,49,110,4,0,177,104,222,58,104,2,
    100,0,8,98,105,103,95,105,110,116,50,110,4,1,177,104,222,58,106]);
var Obj = Bert.decode(S);
Object is equiv to: [{small_integer,42},{integer1,5000},{integer2,-5000},{big_int1,987654321},{big_int2,-987654321}]

bert-js's People

Contributors

airportyh avatar bbrowning avatar borisfaure avatar rbarreiro avatar rustyio avatar stjepano avatar voxpelli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bert-js's Issues

String.charCodeAt only work on firefox

I changed String.charCodeAt(S[i]) into S.charCodeAt(i) then it work under Safari and
Chrome as well.
diff --git a/bert.js b/bert.js
index c76c7d3..b003b68 100644
--- a/bert.js
+++ b/bert.js
@@ -253,7 +253,7 @@ BertClass.prototype.decode_inner = function (S) {
case this.NIL:
return this.decode_nil(S);
default:

  •   throw ("Unexpected BERT type: " + String.charCodeAt(Type));
    
  •   throw ("Unexpected BERT type: " + S.charCodeAt(0));
    
    }
    };

@@ -395,9 +395,9 @@ BertClass.prototype.int_to_bytes = function (Int, Length) {
// of the supplied string.
BertClass.prototype.bytes_to_int = function (S, Length) {
var isNegative, i, n, Num = 0;

  • isNegative = (String.charCodeAt(S[0]) > 128);
  • isNegative = (S.charCodeAt(0) > 128);
    for (i = 0; i < Length; i++) {
  •   n = String.charCodeAt(S[i]);
    
  •   n = S.charCodeAt(i);
    if (isNegative) {
        n = 255 - n;
    }
    
    @@ -440,10 +440,10 @@ BertClass.prototype.bignum_to_bytes = function (Int) {
    // Encode a list of bytes into an Erlang bignum.
    BertClass.prototype.bytes_to_bignum = function (S, Count) {
    var isNegative, i, n, Num = 0;
  • isNegative = (String.charCodeAt(S[0]) === 1);
  • isNegative = (S.charCodeAt(0) === 1);
    S = S.substring(1);
    for (i = Count - 1; i >= 0; i--) {
  •   n = String.charCodeAt(S[i]);
    
  •   n = S.charCodeAt(i);
    if (Num === 0) {
        Num = n;
    }
    
    @@ -475,7 +475,7 @@ BertClass.prototype.pp_bytes = function (Bin) {
    if (s !== "") {
    s += ",";
    }
  •   s += "" + String.charCodeAt(Bin[i]);
    
  •   s += "" + Bin.charCodeAt(i);
    
    }
    return "<<" + s + ">>";
    };

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.