|
7 年 前 | |
---|---|---|
.. | ||
build | 7 年 前 | |
cpp | 7 年 前 | |
ex | 7 年 前 | |
js | 7 年 前 | |
py | 7 年 前 | |
vendor | 7 年 前 | |
COPYING | 7 年 前 | |
LICENSE | 7 年 前 | |
MANIFEST.in | 7 年 前 | |
README.md | 7 年 前 | |
binding.gyp | 7 年 前 | |
mix.exs | 7 年 前 | |
package.json | 7 年 前 | |
pytest.ini | 7 年 前 | |
setup.cfg | 7 年 前 | |
setup.py | 7 年 前 |
Erlpack is a fast encoder and decoder for the Erlang Term Format (version 131) for Python and Javascript.
let erlpack = require("erlpack");
packed = erlpack.pack({'a': true, 'list': ['of', 3, 'things', 'to', 'pack']});
Note: Unpacking requires the binary data be a Uint8Array or Buffer. For those using electron/libchromium see the gotcha below.
let erlpack = require("erlpack");
let unpacked = null;
let packed = new Buffer('', 'binary');
try {
unpacked = erlpack.unpack(packed);
}
catch (e) {
// got an exception parsing
}
Some versions of libchromium replace the native data type backing TypedArrays with a custom data type called blink::WebArrayBuffer. To keep erlpack' dependencies simple this data type is not supported directly. If you're using Electron / Libchromium you need to convert the blink::WebArrayBuffer into a node::Buffer before passing to erlpack. You will need to add this code into your native package somewhere:
v8::Local<v8::Value> ConvertToNodeBuffer(const v8::Local<v8::Object>& blinkArray)
{
if (node::Buffer::HasInstance(blinkArray)) {
return blinkArray;
}
else if (blinkArray->IsArrayBufferView()) {
auto byteArray = v8::ArrayBufferView::Cast(*blinkArray);
return node::Buffer::Copy(v8::Isolate::GetCurrent(), (const char*)byteArray->Buffer()->GetContents().Data(), byteArray->ByteLength()).ToLocalChecked();
}
return v8::Local<v8::Primitive>(v8::Null(v8::Isolate::GetCurrent()));
}
Then in Javascript something like:
let packed = NativeUtils.convertToNodeBuffer(new Uint8Array(binaryPayload));
// unpack now using erlpack.unpack(packed)
from erlpack import pack
packed = pack(["thing", "to", "pack"])
from erlpack import unpack
unpacked = unpack(packed)
from erlpack import Atom, pack
packed = pack(Atom('hello'))
from erlpack import ErlangTermEncoder
def encode_hook(obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
encoder = ErlangTermEncoder(encode_hook=encode_hook)
packed = encoder.pack(datetime.datetime(2015, 12, 25, 12, 23, 55))
from erlpack import pack, Atom
class User(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __erlpack__(self):
return {
Atom('name'): self.name,
Atom('age'): self.age
}
u = User(name='Jake', age=23)
packed = pack(u)