<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>json-sort WASM</title>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB"
crossorigin="anonymous"
/>
</head>
<body class="bg-body-tertiary">
<main class="container py-4 py-lg-5">
<header
class="d-flex flex-column flex-md-row align-items-md-end justify-content-between gap-3 mb-4"
>
<div>
<div class="d-flex flex-wrap align-items-center gap-3 mb-2">
<h1 class="display-3 fw-bold mb-0">json-sort</h1>
<span id="version" class="badge text-bg-secondary">v...</span>
</div>
<p class="lead text-body-secondary mb-0">
Sort JSON object keys in the browser with the Rust library compiled
to WebAssembly.
</p>
</div>
<div class="d-flex flex-wrap gap-2">
<a
class="btn btn-outline-secondary btn-lg"
href="https://github.com/drupol/json-sort"
rel="noopener noreferrer"
target="_blank"
>
GitHub
</a>
<button id="sort" class="btn btn-primary btn-lg" type="button">
Sort JSON
</button>
</div>
</header>
<section class="card mb-3">
<div class="card-body">
<label for="file" class="form-label fw-semibold"
>Load a local JSON file</label
>
<input
id="file"
class="form-control"
type="file"
accept=".json,application/json"
/>
<div class="form-text">
The file is read locally in your browser and is not uploaded.
</div>
</div>
</section>
<section class="row g-3" aria-label="JSON sorting workspace">
<div class="col-12 col-lg-6">
<label for="input" class="form-label fw-semibold">Input</label>
<textarea
id="input"
class="form-control font-monospace"
rows="22"
spellcheck="false"
>
{
"z": 10,
"a": {
"d": true,
"b": false
},
"items": [
{ "name": "second", "id": 2 },
{ "name": "first", "id": 1 }
]
}</textarea
>
</div>
<div class="col-12 col-lg-6">
<label for="output" class="form-label fw-semibold">Output</label>
<textarea
id="output"
class="form-control font-monospace bg-body-secondary"
rows="22"
spellcheck="false"
readonly
></textarea>
</div>
</section>
<div
id="status"
class="alert mt-3 mb-0"
role="status"
aria-live="polite"
></div>
</main>
<script type="module">
import init, {
json_sort_version,
sort_json_string,
} from "./pkg/json_sort.js";
const input = document.querySelector("#input");
const output = document.querySelector("#output");
const status = document.querySelector("#status");
const button = document.querySelector("#sort");
const file = document.querySelector("#file");
const version = document.querySelector("#version");
function setStatusOk(message) {
status.textContent = message;
status.className = "alert alert-success mt-3 mb-0";
}
function setStatusError(message) {
status.textContent = message;
status.className = "alert alert-danger mt-3 mb-0";
}
function sortInput() {
try {
output.value = sort_json_string(input.value);
setStatusOk("Sorted successfully.");
} catch (error) {
setStatusError(
error instanceof Error ? error.message : String(error),
);
}
}
async function loadFile() {
const selectedFile = file.files?.[0];
if (!selectedFile) {
return;
}
const isJsonFile =
selectedFile.type === "application/json" ||
selectedFile.name.toLowerCase().endsWith(".json");
if (!isJsonFile) {
setStatusError("Please choose a JSON file.");
return;
}
try {
input.value = await selectedFile.text();
sortInput();
setStatusOk(`Loaded and sorted ${selectedFile.name}.`);
} catch (error) {
setStatusError(
error instanceof Error ? error.message : String(error),
);
}
}
async function main() {
await init();
version.textContent = `v${json_sort_version()}`;
button.disabled = false;
button.addEventListener("click", sortInput);
file.addEventListener("change", loadFile);
sortInput();
}
button.disabled = true;
setStatusOk("Loading WebAssembly module...");
main().catch((error) => {
setStatusError(error instanceof Error ? error.message : String(error));
});
</script>
</body>
</html>