I'm just starting to explore webassembly and I am attempting to manipulate JSON data that I fetch from either a file or URL within a Webassembly compiled module. My goal is to access this JSON in JavaScript so that I can effectively read and manipulate it there.
Despite my efforts, the only example I came across was on fetching data from a file and saving it to IndexedDB using . However, the output turned out to be unreadable bytes instead of the expected JSON format.
In the fetch.c file:
void downloadSucceeded(emscripten_fetch_t *fetch) {
printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);
// The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
emscripten_fetch_close(fetch); // Free data associated with the fetch.
}
void downloadFailed(emscripten_fetch_t *fetch) {
printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status);
emscripten_fetch_close(fetch); // Also free data on failure.
}
int main() {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_PERSIST_FILE;
attr.onsuccess = downloadSucceeded;
attr.onerror = downloadFailed;
emscripten_fetch(&attr, "./json/bol_list1.json");
}
Post compiling this to .js, the console confirms that the download was successful, fetching 16507 bytes from the JSON file, and it is stored into IndexedDB. However, the issue lies in the fact that the result is not easily readable since it doesn't resemble standard JSON formatting.
JSON file : https://pastebin.com/WBJrYgPR
IndexedDB result: https://i.sstatic.net/2LmBv.png