Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dselman opened this issue Apr 9, 2024 · 5 comments
Assignees

Comments

@dselman
Copy link

dselman commented Apr 9, 2024

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
Copy link
Owner

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

@JairusSW
Copy link
Owner

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

@JairusSW JairusSW self-assigned this Apr 12, 2024
@mattjohnsonpint
Copy link
Contributor

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

@JairusSW
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants