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

Please add a more complex example #38

Open
mariusa opened this issue Nov 15, 2022 · 2 comments
Open

Please add a more complex example #38

mariusa opened this issue Nov 15, 2022 · 2 comments

Comments

@mariusa
Copy link
Contributor

mariusa commented Nov 15, 2022

How could one parse this into a JSON object? Does each property need a class declared?

{
  "name": "Public parking",
  "cars": [
    {
      "plate": "ABC",
      "owner": {
        "name": "John",
        "dob": "2002-01-01"
      }
    },
    {
      "plate": "XYZ",
      "owner": {
        "name": "Mary",
        "dob": "1998-01-01"
      }
    }
  ]
}

dob is of type Date.

Thanks!

@willemneal
Copy link
Collaborator

You need to add @serializable to each class you want to parse into.

@serializable
class Parking {
  name: string,
  cars: Array<Car>
}

@serializable
class Car {
  plate: string,
  owner: Person,
}

@serializable
class Person {
  name: string,
  dob: string,
}

...
import * as JSON from "@serial-as/json";

let parkingLot: Parking = JSON.parse(`{...}`);

@mariusa
Copy link
Contributor Author

mariusa commented Nov 15, 2022

Thank you! Hope the author will add this to README

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

2 participants