Stream: show and tell

Topic: Json Number Decoding


view this post on Zulip Luke Boswell (Mar 22 2023 at 08:33):

Thought I might share this gist which is my first(ish) attempt at a Json number parser that complies with IETF RFC 8259 (2017). Would love any feedback, particularly if there are any performance concerns!

I had to mess around a fair bit to get it to compile, I had a few bugs which really slowed me down. I re-remembered that we can't match on constants in a where block yet. Also I wasn't able to use a tuple instead of a Tag.

I'm pretty happy with the state transition and tests working out so well. It was easy to translate from a drawing I made of the states and transitions into code by using Tags. The expects made it super easy to do some TDD and confirm it was performing correctly.

The hardest part I found was to get through a few compiler bugs, and find the correct syntax that would compile. I don't mean that as a criticism, just I bumped into some confusing crashes which slowed me down.

Also note we can't quite fully support Json numbers att as we don't seem to fully support e or + for exponents. Related issue. I included the below snippet which shows a workaround which gets us slightly closer to ideal but add a couple of scans through the number bytes.

taken
|> List.dropIf \b -> b == '+' # TODO not needed if roc supports "1e+2"
|> List.map \b -> if b == 'E' then 'e' else b # TODO not needed if roc supports "1E2"

view this post on Zulip Richard Feldman (Mar 22 2023 at 11:43):

nice, thank you for reproducing those bugs! :muscle:


Last updated: Jul 05 2025 at 12:14 UTC