JairusSW / as-json

The only JSON library you'll need for AssemblyScript. SIMD enabled
MIT License
67 stars 12 forks source link

Feature request: add an option to throw error when additional properties are present in JSON #66

Open dselman opened 3 months ago

dselman commented 3 months ago

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"}]}
JairusSW commented 2 months ago

Sure, I'll add that to my to-do list

JairusSW commented 2 months ago

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

mattjohnsonpint commented 2 months ago

If only AssemblyScript had support for try/catch, or a standard error handling mechanism...

JairusSW commented 2 months ago

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

JairusSW commented 2 months ago

@dselman, coming down the pipeline in the #67 branch