Skip to content

pmuellr/sloppy_json_parse

Repository files navigation

sloppy_json_parse - a parser for a sloppier kind of JSON

The function sloppy_json_parse() is a fork of the json_parse() function available at http://www.json.org/json_parse.js. The API for the two functions is the same, besides the format of the text that they parse.

sloppy_json_parse() parses a language similar to JSON, but with these differences:

  • commas are optional

  • comments start with the '#' character and end with the end of the line

  • strings which match the regex /[$_A-Z][$\w]*/i don't need to be quoted, except that the strings "null", "true", and "false" must always be quoted. The regex is the syntax of JavaScript identifiers.

Re-inspired by CoffeeScript after thinking about it for a short while.

examples


sloppy JSON

    [1 2 3 dogs cats]

equivalent JSON

    [1, 2, 3, "dogs", "cats"]

sloppy JSON

    {x:y z:a}

equivalent JSON

    {"x":"y", "z":"a"}

sloppy JSON

    {
        # some properties of the object
        a: z                             # z is a string!
        b: y                             # so is y
        c: [1 2 3]
        d: [                             # d is an array
            {
                abc: true
                def: false
            }                            # end of this inner object
            
            {
                xyz: null
                uvw: [101 202 303]       # weird numbers
            }
            
            [4 5 6]                      # last element
        ]
        # end of the properties
    }

equivalent JSON

    {
        "a": "z",
        "b": "y",
        "c": [1, 2, 3],
        "d": [
            {
                "abc": true,
                "def": false
            },
            
            {
                "xyz": null,
                "uvw": [101, 202, 303]
            },
            
            [4, 5, 6]
        ]
    }

About

a parser for a sloppier kind of JSON

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published