Created
August 13, 2021 08:12
-
-
Save peterpeterparker/d7c49c21bf6939885557fcd5b8984106 to your computer and use it in GitHub Desktop.
JSON <-> Blob <-> Array number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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