bcj

poster emeritus

  • they/them

Photography
bcj.pics/
Social Media
beocijies.com/
Just sort of Whatever
onegross.online/

One of the big complaints about JSON is that you can't add comments. What if you supplied JSON data as a markdown file where the JSON data was stored in code blocks labelled as JSON data. This way, not only can you comment on the data, you can provide reference parsers for the data within the data:

# My Cool JSON Data

Welcome to my JSON data.
If you like reading this data, considering supporting my [ko-fi](https://cohost.org/rc/user/settings#cohost-plus)

Anyway, here's the data:

```json
{
  "key": "value",
  "json": true,
  "stars": 5
}
```

And here's some python code that will parse this specific data:

```python3
import json
from argparse import ArgumentParser
from pathlib import Path

def parse(serialized: str):
    data = None

    is_json = False
    json_lines = []
    for line in serialized.splitlines():
        line = line.strip()
        if is_json:
            if line.strip() == "```":
                data = json.loads("\n".join(json_lines))
                is_json = False
                json_lines = []
            else:
                json_lines.append(line)
        elif line == "```json":
            is_json = True

    if is_json:
        raise ValueError("never-ending JSON data")

    return data


def main():
    parser = ArgumentParser(description="A literate JSON parser")
    parser.add_argument("file", type=Path)
    args = parser.parse_args()

    with args.file.open("r") as stream:
        print(parse(stream.read()))


if __name__ == "__main__":
    main()
```

But if the JSON is all within code blocks, you still can't add actual comments, can you?

Well, I feel like you're ignoring the obvious advantages of this new system

Wait, wait. What happens if there are multiple JSON blocks within the file?

What happens if you have duplicate keys in your JSON data, your parser probably silently overwrites the keys without any notice, masking errors in the most frustrating way possible

Fuck you

You too, buddy


You must log in to comment.

in reply to @bcj's post: