as-json
as-json copied to clipboard
Feature request: add an option to throw error when additional properties are present in JSON
Currently additional properties in the incoming JSON are ignored. It would be useful to throw an error when additional properties are encountered.
For example:
// The entry file of your WebAssembly module.
import { JSON } from "json-as/assembly";
@json
class Animal {
name!: string;
type!: string;
}
@json
class Zoo {
animals!: Animal[]
}
export function parse(stringified: string) : string {
const zoo = JSON.parse<Zoo>(stringified, true);
const out = JSON.stringify<Zoo>(zoo);
return out;
}
<!DOCTYPE html>
<html lang="en">
<head>
<script type="module">
import { parse } from "./build/release.js";
const zoo = {
animals: [
{
name: "Fido",
type: "Dog",
age: 10,
},
{
name: "Tiddles",
type: "Cat",
isVerified: false,
},
],
};
try {
document.body.innerText = parse(JSON.stringify(zoo));
} catch (err) {
alert(err);
}
</script>
</head>
<body></body>
</html>
Output:
{"animals":[{"name":"Fido","type":"Dog"},{"name":"Tiddles","type":"Cat"}]}
Sure, I'll add that to my to-do list
I'm building in a lot more customization to the lib like returning a Result<T> for error handling, formatting modes, and the option to parse without a schema
If only AssemblyScript had support for try/catch, or a standard error handling mechanism...
If only AssemblyScript had support for
try/catch, or a standard error handling mechanism...
Oh for real! Its too bad nobody is really working on it at all
@dselman, coming down the pipeline in the #67 branch