0

I must be googleing this wrong. I'm being given a feed in Universal Binary JSON format via a Websocket.

How does one parse/unpack universal binary json data to a useable form in pure javascript?

I've looked at ubjson.org and there appear to be two implementations of ubjson parsers in javascript. However, one is for node.js and the other for asm.js. Isn't ubjson supposed to parse/serialize to/from JSON 1-for-1?

Using the ubjson.js one without asm.js, I tried:

ubjson.bytesU8 = ubjsondata;
var value = ubjson.decode();
console.log(JSON.stringify(value, null, 2));

But it only logs a few numbers and then triggers the browser's print mode!

1 Answer 1

1

Ahhhh, sorry for the confusion (curator of UBJSON here) - UBJSON is a binary spec, you are getting a binary payload that looks like a collection of this - you can't turn it into a string and print it. You have to parse it, as bytes.

If you parsed a String for example you would parse the marker byte 'S', then the numeric size marker (probably an 'i' unless you are working with bigger values) and then the size, let's say '8' for 8-character string, then you can grab the next 8 bytes and turn THOSE into a string.

When the spec says 1:1 with JSON, it's logically compatible, it isn't the same text-based format.

Sign up to request clarification or add additional context in comments.

1 Comment

I was rushed, so I ended up solving this by building a forwarder that used the python library to convert ubjson back into JSON locally before forwarding it to the browser. I know that, technically, JSON isn't really JavaScript (but close) but at least it's almost representable as JavaScript. Is there a way to get back to a console-presentable JSON easily?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.