7

How can I store a JSON object in an ArrayBuffer? I tried the code below.

function stringToUint(string) {
    var string = btoa(unescape(encodeURIComponent(string))),
        charList = string.split(''),
        uintArray = [];
    for (var i = 0; i < charList.length; i++) {
        uintArray.push(charList[i].charCodeAt(0));
    }
    return new Uint8Array(uintArray);
}

function uintToString(uintArray) {
    var encodedString = String.fromCharCode.apply(null, uintArray),
        decodedString = decodeURIComponent(escape(atob(encodedString)));
    return decodedString;
}
var str = {'x':'aaaa'},
    enc = stringToUint(str),
    dec = uintToString(enc);

console.log(dec.x);

console.log(dec.x); is printing `undefined. Am I doing it wrongly ? here is jsfiddle http://jsfiddle.net/DQJyX/137/

4 Answers 4

6

Because stringToUint expects a string, and passing an object to it will convert the {'x':'aaaa'} to [Object object] and returns an array representing that object object, and hence on calling uintToString, the array is converted back to [Object object].

Simple solution is to use JSON.stringify to the object before passing it to function and then using JSON.parse to convert to original object.

function stringToUint(string) {
    var string = btoa(unescape(encodeURIComponent(string))),
        charList = string.split(''),
        uintArray = [];
    for (var i = 0; i < charList.length; i++) {
        uintArray.push(charList[i].charCodeAt(0));
    }
    return new Uint8Array(uintArray);
}

function uintToString(uintArray) {
    var encodedString = String.fromCharCode.apply(null, uintArray),
        decodedString = decodeURIComponent(escape(atob(encodedString)));
    return decodedString;
}
var str = {'x':'aaaa'},
    enc = stringToUint(JSON.stringify(str)),
    dec = JSON.parse(uintToString(enc));
    
document.write(dec.x);

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

3 Comments

How can I get back JSON from what we get from Utils.stringToUint(JSON.stringify(str))
using JSON.parse
This answer is 10 years old, use TextEncoder
1

in order for it to work you need, in the stringToUint function call to JSON.stringify.

function stringToUint(string) {
    var string = btoa(unescape(JSON.stringify(string))),
        charList = string.split(''),
        uintArray = [];
    for (var i = 0; i < charList.length; i++) {
        uintArray.push(charList[i].charCodeAt(0));
    }
    return new Uint8Array(uintArray);
}

function uintToString(uintArray) {
    var encodedString = String.fromCharCode.apply(null, uintArray),
        decodedString = decodeURIComponent(escape(atob(encodedString)));
    return decodedString;
}
var str = {'x':'aaaa'},
    enc = stringToUint(str),
    dec = uintToString(enc);

console.log(dec);

Comments

1

Since 2020, consider using TextEncoder:

const encode = (e => e.encode.bind(e))(new TextEncoder()) // reusable
const buffer = encode({ a:1, b:2, c:3 }).buffer
console.log('instanceOf:', buffer.constructor.name)
console.log('totalBytes:', buffer.byteLength)

Comments

-1

This code for stringToUint is ~40% faster, benchmark link

function stringToUint(string) {
  const str = btoa(unescape(encodeURIComponent(string)))
  const uintArray = []
  const len = str.length

  let i = -1

  while (++i < len) {
    uintArray[i] = str.charCodeAt(i)
  }

  return new Uint8Array(uintArray);
}

Comments

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.