ygoe / msgpack.js Goto Github PK
View Code? Open in Web Editor NEWA minimal yet complete MessagePack implementation for JavaScript. msgpack.org[JavaScript]
License: MIT License
A minimal yet complete MessagePack implementation for JavaScript. msgpack.org[JavaScript]
License: MIT License
let obj = serializeMsgPack([100, 100, "ok", serializeMsgPack([[100, 1000.01]])]
let obj2 = deserializeMsgPack(obj)
console.log(deserializeMsgPack(obj2[3])[0][1]);// print:5.262076770253218e+228
The encoder's append
function does not have a default handler. This can result in broken or incomplete objects (e.g. when an encoded object contains a function).
Also, would you consider teaching the encoder to call an object's "toJSON" method (if it has one), and encode its result instead of the original object?
This library unfortunately doesn't handle 64 bit integers particularly well. It could be because it's not using BigInt where appropriate
It seems there are options for both serialize()
and unserialize()
, but I do not know what they do. I have read the following comment but I don't know what it means.
Indicates whether multiple values in data are concatenated to multiple MessagePack arrays.
Perhaps an example would help?
What is happening: during encoding binary data is handled like a plain object and during decoding it wrongly returns the array which represents the length of the binary data.
What was expected: typed arrays being encoded using the bin format family and this format family being decoded back to typed arrays.
Examples:
serializeMsgPack(new Uint8Array([1, 2, 3]))
// Expected: new Uint8Array([0xC4, 0x03, 0x01, 0x02, 0x03])
// Got: new Uint8Array([0x83, 0xA1, 0x30, 0x01, 0xA1, 0x31, 0x02, 0xA1, 0x32, 0x03])
deserializeMsgPack(new Uint8Array([0xC4, 0x03, 0x01, 0x02, 0x03]))
// Expected: new Uint8Array([1, 2, 3])
// Got: new Uint8Array([3])
Nice work, and suggest to add package.json
and publish to npm as a module for better dependencies management.
I am using msgpack.js with Message Pack implementation in C# for ASP.NET Core. But I am getting an error like this.
Here is the code.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddMvcOptions(option =>
{
option.OutputFormatters.Clear();
option.OutputFormatters.Add(new MessagePackOutputFormatter(StandardResolver.Instance));
option.InputFormatters.Clear();
option.InputFormatters.Add(new MessagePackInputFormatter(StandardResolver.Instance));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
And here is the client code.
<script src="jquery-3.3.1.min.js"></script>
<script src="msgpack.js"></script>
<script>
$(function () {
$.get("/api/person", function (data) {
console.log(deserializeMsgPack(data));
});
});
</script>
I am using it in a Web API project, and returning default values controller.
Hello
First of all great work. Im using .Net Core as backend, and im interested knowing if is possible to have decimal values in C# class or is mandatory to use double.
Thanks in advance
Looks like a for loop is used for iterating the properties in the objects
for (let key in data) {
if (data[key] !== undefined) {
length++;
}
}
This also iterates the properties defined in prototypes which I don't think is an expected behaviour.
The current typescript version of msgpack is using Object.keys
that does not returns those properties.
What do you think?
Is it possible to decode an encoded message from python in javascript using this library? And if yes, how?
The encoded message I created in python looks as follow: b'\x83\xa2id\x01\xa4test\x17\xa3hey*'
When I retrieve it in my javascript code and try to deserialize it I get an error message about invalid type, undefined. The encoded message I created in the python script seems to be a Blob{} type when it arrives in the javascript code. I have tried to convert the Blob type to a string by using a fileReader, but with small success.
Any ideas regarding this?
Cheers
function append(data) {
data == null || data === NaN || data === Infinity ? appendByte(0xc0) :
(
data.constructor === String ? appendString :
data.constructor === Number ? appendNumber :
data.constructor === Boolean ? appendBoolean :
Array.isArray(data) ? appendArray :
data instanceof Date ? appendDate :
appendObject
)(data)
}
Similar to JSON.stringify
behavior, properties whose value is undefined
should not be serialized as null
in msgpack. Undefined means: undefined, which is decidedly different from a defined value of null
.
Observe the following node interactive:
> msgpack.deserialize(msgpack.serialize({a:'a',b:undefined}))
{ a: 'a', b: null }
> JSON.stringify({a:'a',b:undefined})
'{"a":"a"}'
It's convenient to use undefined
as a value to suppress its encoding, but this library ignores that technique.
If you can't change the default behavior, what if we added an option under which undefined
properties would be skipped?
I am willing to send a PR.
First of all, thank you for your work.
Currently only float is being converted using DataView, why not use DataView to convert Integer uniformly?
If we all use DataView, the code will be shorter and easier to read, also less prone to errors.
Recently I needed to use the little-endian version of msgpack.
I modified a python library and all I had to do to modify the endianness was to change the symbol ">" to "<".
To modify the javascript library, I only had to change the DataView functions parameter from false to true, but since you don't use DataView, it's a bit confusing: https://github.com/dukelec/msgpackel
Modification for the python library:
Modification for the javascript library:
Can you provide an example of msgpack.js + MessagePack-CSharp? I tried to use msgpack.js to send data to the server via WebSocket. Deserialize bytes using MessagePack-CSharp, but not working. Thank you.
Can you please add a .d.ts typings file to this package?
At first, thanks for your library. It is small without Node.js dependencies, what is the unicorn today.
I have suggestion for the small enhancement: Please add a callback for ext format decoding to the deserialize function
A compilation exception occurred while trying to copy to a TypeScript file.
The number of parameters of some functions does not correspond.
When the library is used in a web worker there is an issue with undefined window
so i suggest adding Web Workers detection such as :
// Environment detection
if (typeof module === "object" && module && typeof module.exports === "object") {
// Node.js
module.exports = msgpack;
}
else if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
// Global object
self[self.msgpackJsName || "msgpack"] = msgpack;
} else {
// Global object
window[window.msgpackJsName || "msgpack"] = msgpack;
}
When deserializing from an Uint8Array which has a non-zero byteOffset, float values go wrong.
(I am sending an MsgPack object together with some type information over websocket, so only the second part of the websocket frame contains the data to deserialize)
MWE:
const buf = new ArrayBuffer(10);
const arr0 = new Uint8Array(buf);
const arr1 = new Uint8Array(buf, 1);
for (let i = 0; i < arr0.length; i++) {
arr0[i] = 255;
}
for (let i = 0; i < arr1.length; i++) {
arr1[i] = 0;
}
arr1[0] = 0xcb;
arr1
should then deserialize to 0, but it does not. (I have found the cause and will issue a pull request soon)
If you serialise a date which is pre-epoch, e.g. new Date(-1)
is 1969-12-13 23:59:59.999
, and then attempt to deserialize it, the deserialsiation fails return the same date.
Hex representation:
From a quick play around in the debugger this appears to be because the sign bit and 2's compliment is not respected for negative numbers (which are required for pre-epoch times). My guess is that this is due to the bit shifting required to parse the dates.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.