Coder Social home page Coder Social logo

mojo-json's Introduction

Json Mojo

Overview

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.

Usage

import json

x = json.loads('{"key": "value"}')
json.dumps(x)
>>> '{"key": "value"}'

Installation

Copy the json.📦 file into your project and it should be usable.

TODO

  1. ~~Fix parsing of floats.
  2. Refactor the parser to use slices of the tokens
  3. ~~Checking if the first token is a right brace twice. Should be able to do this once.
  4. Add streaming capabilities.
  5. ~~Better handle escaped characters in Strings.
  6. Moooore performance
  7. Add GitHub action to build package

Edge Cases to Consider

1. Trailing Comma

{
  "key1": "value1",
  "key2": "value2",
}

Trailing commas after the last key-value pair are not allowed in JSON.

2. Missing Quotes around Keys

{
  key1: "value1",
  "key2": "value2"
}

Keys must always be strings enclosed in double quotes.

3. Single Quotes Instead of Double Quotes

{
  'key1': 'value1',
  "key2": "value2"
}

JSON requires double quotes around strings, not single quotes.

4. Non-string Key

{
  123: "value"
}

Keys must be strings enclosed in double quotes.

5. Invalid Unicode Characters

{
  "key": "value\uZZZZ"
}

\u must be followed by four hexadecimal digits.

6. Unescaped Control Characters

{
  "key": "value\u0001"
}

Control characters must be properly escaped.

7. Dangling Quotes

{
  "key": "value"
"key2": "value2"
}

Missing commas between key-value pairs.

8. Special Numbers

{
  "number": NaN
}

NaN, Infinity, and -Infinity are not valid values in JSON.

9. Nested Structures

{
  "nested": {
    "nested2": {
      "nested3": {
        "nested4": {
          "nested5": "value"
        }
      }
    }
  }
}

Extreme nesting could potentially break a parser if recursion depth is not handled properly.

10. Mixed Array Types

{
  "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.

11. Big Numbers

{
  "bigNumber": 1234567890123456789012345678901234567890
}

Large numbers can cause precision loss issues in parsers that don't handle them correctly.

12. Circular References (though not valid JSON)

{
  "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.

13. Duplicate Keys

{
  "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).

Progress

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.

mojo-json's People

Contributors

zachooper avatar

Stargazers

 avatar  avatar Brendan Duke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.