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

Add basic parsing JSON example #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

leejarvis
Copy link
Contributor

@leejarvis leejarvis commented Nov 11, 2024

Hello! I'd like to contribute a basic JSON parser example to the cookbook.

Not sure how canonical the example is so would welcome feedback on that or how complex a decoding you would like to see covered.

@lpil
Copy link
Member

lpil commented Nov 11, 2024

decode/zero is likely to be promoted into the stdlib, so let's use that. It has a series of examples in its documentation. How about we port those over to here? We could have one cookbook example for each one.

https://hexdocs.pm/decode/decode/zero.html

@leejarvis
Copy link
Contributor Author

Added some examples to use decode/zero — haven't used it before now so open to feedback. I couldn't get a dict parsing example to work. This for example:

  let decoder = {
    use fields <- zero.field("fields", zero.dict(zero.string, zero.int))
    zero.success(fields)
  }
  "{\"fields\": {\"a\": 1, \"b\": 2}}"
  |> json.decode(zero.run(_, decoder))
  |> should.equal(Ok(dict.from_list([#("a", 1), #("b", 2)])))

returns Error(UnexpectedFormat([DecodeError(expected: "Dict", found: "Object", path: ["fields"])]))

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

Successfully merging this pull request may close these issues.

2 participants