|
1 | 1 | # API Reference — JSON-stat WebAssembly |
2 | 2 |
|
3 | 3 | > This is the API reference for the **JSON-stat WebAssembly** library (`jsonstat-wasm`). |
4 | | -> For the original JSON-stat JavaScript Toolkit API, see [toolkit-api.md](toolkit-api.md). |
| 4 | +> For the original JSON-stat JavaScript Toolkit API, see the [upstream toolkit API docs](https://github.com/jsonstat/toolkit/blob/master/docs/API.md). |
5 | 5 |
|
6 | 6 | ## Overview |
7 | 7 |
|
8 | 8 | The library exposes a single class, `JSONstat`, which parses a [JSON-stat 2.0](https://json-stat.org/format/) string into a WebAssembly-managed object. It supports three response classes: **dataset**, **collection**, and **dimension**. |
9 | 9 |
|
| 10 | +> 🧪 **Live examples:** runnable `jsonstat-wasm` snippets live on |
| 11 | +> [jsonstat.com/examples?lib=wasm](https://jsonstat.com/examples/?lib=wasm). |
| 12 | +
|
| 13 | +There are two ways to obtain a `JSONstat` instance — the **high-level facade** |
| 14 | +([`jsonstat.js`](../jsonstat.js), recommended) and the **raw glue** |
| 15 | +(`jsonstat_wasm.js`, advanced). Both return the same class, so the methods and |
| 16 | +properties documented below apply identically to either. See [Entry points](#entry-points). |
| 17 | + |
10 | 18 | ```js |
11 | | -import init, { JSONstat, init_panic_hook } from './pkg/jsonstat_wasm.js'; |
| 19 | +// Facade (recommended): no init(), no `new` |
| 20 | +import { JSONstat } from 'https://cdn.jsdelivr.net/npm/jsonstat-wasm@0.4.1/jsonstat.js'; |
12 | 21 |
|
13 | | -await init(); |
14 | | -init_panic_hook(); // Optional: better error messages in console |
| 22 | +const ds = await JSONstat('https://json-stat.org/samples/oecd.json'); |
| 23 | +``` |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Entry points |
| 28 | + |
| 29 | +### The facade (`jsonstat.js`) — recommended |
| 30 | + |
| 31 | +The high-level facade exposes a single toolkit-style function, `JSONstat(input, |
| 32 | +options)`. It initializes the WASM module exactly once on first import and gates |
| 33 | +every call behind that shared promise, so you never call `init()` yourself and |
| 34 | +never use `new`. |
| 35 | + |
| 36 | +From a CDN (no build step): |
| 37 | + |
| 38 | +```js |
| 39 | +import { JSONstat } |
| 40 | + from 'https://cdn.jsdelivr.net/npm/jsonstat-wasm@0.4.1/jsonstat.js'; |
| 41 | +// …or unpkg: |
| 42 | +// import { JSONstat } from 'https://unpkg.com/jsonstat-wasm@0.4.1/jsonstat.js'; |
| 43 | +``` |
| 44 | + |
| 45 | +Or as an npm / bundler import: |
| 46 | + |
| 47 | +```js |
| 48 | +import { JSONstat } from 'jsonstat-wasm'; |
| 49 | +``` |
15 | 50 |
|
| 51 | +The `input` argument is overloaded: |
| 52 | + |
| 53 | +| `input` | Behavior | Returns | |
| 54 | +|---------|----------|---------| |
| 55 | +| `"version"` | Package version (baked into the WASM binary) | `Promise<string>` | |
| 56 | +| URL string | Fetches the URL, then parses the body | `Promise<JSONstat>` | |
| 57 | +| Inline JSON-stat string (leading `{`) | Parsed in a single Rust `serde_json` pass (no double `JSON.parse`) | `Promise<JSONstat>` | |
| 58 | +| Object | Parses an in-memory JSON-stat object | `Promise<JSONstat>` | |
| 59 | + |
| 60 | +`options` (optional, `RequestInit`) is forwarded to `fetch()` and only used when |
| 61 | +`input` is a URL. |
| 62 | + |
| 63 | +```js |
| 64 | +const ds = await JSONstat('https://json-stat.org/samples/oecd.json'); // URL |
| 65 | +const ds2 = await JSONstat(jsonStr); // inline JSON-stat string |
| 66 | +const ds3 = await JSONstat(parsedObj); // already-parsed object |
| 67 | +console.log(await JSONstat('version')); // "0.4.1" |
| 68 | +``` |
| 69 | + |
| 70 | +> **`"version"` returns a Promise:** the version is baked into the WASM binary |
| 71 | +> via `env!("CARGO_PKG_VERSION")`, so it must await the init gate — unlike the |
| 72 | +> plain-JS toolkit, where it is synchronous. |
| 73 | +> |
| 74 | +> **Do not use `new`:** call `JSONstat(...)` as a plain function. The facade |
| 75 | +> wraps the WASM class internally. |
| 76 | +
|
| 77 | +The facade wraps the returned dataset in a transparent `Proxy` that routes |
| 78 | +`Transform({ type: "arrobj" })` (without `by` / `meta`) through a columnar fast |
| 79 | +path and memoizes `value`; every other access passes through unchanged, so the |
| 80 | +rest of this reference applies without modification. |
| 81 | + |
| 82 | +### The raw glue (`jsonstat_wasm.js`) — advanced |
| 83 | + |
| 84 | +Use this only when you need the WASM class directly — for example, an explicit |
| 85 | +`init(url)` on CDNs that rewrite `import.meta.url`. You must call `init()` |
| 86 | +yourself and instantiate with `new`: |
| 87 | + |
| 88 | +```js |
| 89 | +import init, { JSONstat, init_panic_hook } from './pkg/jsonstat_wasm.js'; |
| 90 | + |
| 91 | +await init(); // required |
| 92 | +init_panic_hook(); // optional, better error messages in the console |
16 | 93 | const ds = new JSONstat(jsonStr); |
17 | 94 | ``` |
18 | 95 |
|
| 96 | +See [Constructor](#constructor) for the class constructor, and the |
| 97 | +[Installation → CDN](./INSTALL.md#cdn-no-build-step-no-install) section for the |
| 98 | +explicit `init(url)` form. |
| 99 | + |
19 | 100 | --- |
20 | 101 |
|
21 | 102 | ## Methods |
@@ -710,3 +791,11 @@ Common error cases: |
710 | 791 | | Missing dimension | `"Dimension '...' not found"` | |
711 | 792 | | Missing category | `"Category '...' not found in dimension '...'"` | |
712 | 793 | | Invalid query | `"Query is missing category for non-constant dimension '...'"` | |
| 794 | + |
| 795 | +--- |
| 796 | + |
| 797 | +## See also |
| 798 | + |
| 799 | +- 🧪 [Live examples](https://jsonstat.com/examples/?lib=wasm) — runnable `jsonstat-wasm` snippets on jsonstat.com. |
| 800 | +- 📖 [Installation guide](./INSTALL.md) — building from source, npm/bundlers, CDN usage, and the Rust library API. |
| 801 | +- 🔗 [Upstream toolkit API](https://github.com/jsonstat/toolkit/blob/master/docs/API.md) — the original JSON-stat JavaScript Toolkit reference. |
0 commit comments