Skip to content

Strict whitespace adherence option for JSON #164

@CoffeeVampir3

Description

@CoffeeVampir3

An example json grammar, E.G:

          start: json_object
          json_object: %json {
            "type": "object",
            "properties": {
              "health": { 
                "type": "integer", 
                "minimum": 1, 
                "maximum": 100 
              },
              "mana": { 
                "type": "integer", 
                "minimum": 1, 
                "maximum": 100 
              },
              "attack": { 
                "type": "integer", 
                "minimum": 1, 
                "maximum": 100 
              },
              "defense": { 
                "type": "integer", 
                "minimum": 1, 
                "maximum": 100 
              },
              "agility": { 
                "type": "integer", 
                "minimum": 1, 
                "maximum": 100 
              },
              "description": {
                "type": "string"
              },
              "current": {
                "type": "string"
              },
              "current_thoughts": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": ["health", "mana", "attack", "defense", "agility", "description", "current", "current_thoughts"]
          }

This does not strictly enforce white spacing, and it doesn't seem to be possible to use this style of grammar to do so.

Here's an example literally output from an llm:

{"health": 100, "mana": 100, "attack": 20, "defense": 10, "agility": 15, "description": "You stand on the windswept cliff, the ocean crashing against the rugged stone beneath your feet. The sun beats down upon your skin, warming your bones, and illuminating the vast expanse of sea and sky.", "current": "standing", "current_thoughts": ["The beauty of the ocean, the thrill of the wind", "The vastness of the sea, the mystery of the unknown"]}"

Though this is technically correct, I'd like to propose a strict whitespace mode that enforces what you might consider "human readable json"

This is a grammar that produces such json, but it's significantly less readable for humans:

          start: json_object
          json_object: "{" NEWLINE TAB first_prop comma_props NEWLINE "}"

          first_prop: "\"health\"" ": " INTEGER
          comma_props: numeric_props string_props array_prop
          numeric_props: comma_prop comma_prop comma_prop comma_prop
          string_props: comma_prop comma_prop

          comma_prop: "," NEWLINE TAB property
          array_prop: "," NEWLINE TAB "\"current_thoughts\"" ": " array

          property: "\"mana\"" ": " INTEGER
                  | "\"attack\"" ": " INTEGER
                  | "\"defense\"" ": " INTEGER
                  | "\"agility\"" ": " INTEGER
                  | "\"description\"" ": " STRING
                  | "\"current\"" ": " STRING

          array: "[" NEWLINE TAB TAB first_string array_items NEWLINE TAB "]"
          first_string: STRING
          array_items: comma_string comma_string
          comma_string: "," NEWLINE TAB TAB STRING

          INTEGER: /([1-9][0-9]?|100)/
          STRING: "\"" /[^"]*/ "\""

          NEWLINE: "\n"
          TAB: "\t"

Example output:

{
        "health": 100,
        "mana": 100,
        "attack": 20,
        "defense": 10,
        "agility": 15,
        "description": "You stand at the edge of a cliff, gazing out at the endless ocean. The wind carries the cries of the gulls, and the waves crash against the shore. You feel a sense of wonder and excitement, and your heart begins to pound with anticipation. What do you do next?",
        "current": "gazing out at the horizon",
        "current_thoughts": [
                "the vastness of the ocean, the wind, the gulls, the waves",
                "your heart pounding with anticipation, your senses on high alert",
                "the wind carries the cries of the gulls, and the waves crash against the shore"
        ]
}

Having a standard strict-whitespacing I think would be a great feature to have around.

Cheers, really love the library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions