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

Improve JSON parser demo #314

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

oskarek
Copy link
Contributor

@oskarek oskarek commented Sep 10, 2023

There are a couple issues in the JSON parser demo (recently discussed in this pointfree Slack thread):

  • Parsing characters built up of more than one utf-8 code point, such as , fails.
  • The literal unicode code point format is faulty, lacking a literal u character before the numbers.

This PR fixes those issues, as well as extends the JSON parser to fully conform to the json standard. This means adding support for handling UTF-16 surrogate pairs, and exactly match the JSON specification's number format, instead of relying on Double.parser() which isn't exactly the same.

Copy link
Member

@stephencelis stephencelis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvements here! Just one small thing to address before merging, I think.

Comment on lines +14 to -16
case float(Double)
case integer(Int)
case null
case number(Double)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change? I was under the impression that JSON/JavaScript only has the concept of a "number" and doesn't distinguish integer and float values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yes, true. JSON only has the concept of a number, which is a sequence of digits, optionally followed by a decimal point and a fractional part. Now, if we model this as just a Double in Swift, we lose information about the number. 1 and 1.0, for example, becomes indistinguishable. By modeling numbers that does contain a fractional part as a Double, and numbers that doesn't as an Int, we at least keep that information, which I think is often desirable in Swift.

To not lose any information we could model the numbers as a String (case number(String)), but that seems less convenient in practice. I think just separating numbers with and without any fractional part strikes a good balance of what information to keep about the numbers. This is at least how I'm thinking about this :)

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.

None yet

2 participants