This is a simple JSON parser written in Mojo. It mimics the Python JSON module so you can copy in your existing code and it should work. This is a naive implementation and may not handle all edge cases and may not be the most efficient.
import json
x = json.loads('{"key": "value"}')
json.dumps(x)
>>> '{"key": "value"}'
Copy the json.📦
file into your project and it should be usable.
- ~~Fix parsing of floats.
- Refactor the parser to use slices of the tokens
- ~~Checking if the first token is a right brace twice. Should be able to do this once.
- Add streaming capabilities.
- ~~Better handle escaped characters in Strings.
- Moooore performance
- Add GitHub action to build package
{
"key1": "value1",
"key2": "value2",
}
Trailing commas after the last key-value pair are not allowed in JSON.
{
key1: "value1",
"key2": "value2"
}
Keys must always be strings enclosed in double quotes.
{
'key1': 'value1',
"key2": "value2"
}
JSON requires double quotes around strings, not single quotes.
{
123: "value"
}
Keys must be strings enclosed in double quotes.
{
"key": "value\uZZZZ"
}
\u
must be followed by four hexadecimal digits.
{
"key": "value\u0001"
}
Control characters must be properly escaped.
{
"key": "value"
"key2": "value2"
}
Missing commas between key-value pairs.
{
"number": NaN
}
NaN
, Infinity
, and -Infinity
are not valid values in JSON.
{
"nested": {
"nested2": {
"nested3": {
"nested4": {
"nested5": "value"
}
}
}
}
}
Extreme nesting could potentially break a parser if recursion depth is not handled properly.
{
"array": [1, "string", true, null, {"key": "value"}, [1, 2, 3]]
}
While this is valid JSON, it could cause issues in parsers that expect arrays to be homogenous.
{
"bigNumber": 1234567890123456789012345678901234567890
}
Large numbers can cause precision loss issues in parsers that don't handle them correctly.
{
"key": "value",
"self": { "$ref": "$" }
}
Circular references are not valid in JSON but if your parser encounters them, it could fail unless special handling is implemented.
{
"key": "value1",
"key": "value2"
}
JSON doesn’t technically disallow duplicate keys, but parsers should decide how to handle them (value2
will overwrite value1
in most parsers).
V0.0.1
- Working but slooooow. So long that I didn't wait to see when it finished parsing the canada.json file.
V0.0.2
- Addressed copy issues.
- Now parses the canada.json file in 700ms on my hardware. Still slow but actually usable now.