Skip to content

Instantly share code, notes, and snippets.

@peterpeterparker
Created August 13, 2021 08:12
Show Gist options
  • Save peterpeterparker/d7c49c21bf6939885557fcd5b8984106 to your computer and use it in GitHub Desktop.
Save peterpeterparker/d7c49c21bf6939885557fcd5b8984106 to your computer and use it in GitHub Desktop.
JSON <-> Blob <-> Array number
const toArray = async <T>(data: T): Promise<Array<number>> => {
const blob: Blob = new Blob([JSON.stringify(data)], {type: 'application/json; charset=utf-8'});
return [...new Uint8Array(await blob.arrayBuffer())];
};
const fromArray = async <T>(data: Array<number>): Promise<T> => {
const blob: Blob = new Blob([new Uint8Array(data)], {type: 'application/json; charset=utf-8'});
return JSON.parse(await blob.text());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment