Skip to content

[FEAT] Add structured comments support for JSON writting #223

@CarterLi

Description

@CarterLi

Is your feature request related to a problem? Please describe.
People ask for documentation... Even if I have provided JSON schema support for my config file, they are still asking, especially those Linux fundamentalists. They don't use vscode or helix that supports JSON schema natively, nor do they want to install a plugin for their vim. They just use plain vim or nano, ignore the "$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json" line at the beginning of config file and complain on the Internet that fastfetch's configuration is overwhelmingly more complex than neofetch's.

Describe the solution you'd like
I'd like to add comments in the generated JSONC file, for example

{
  "$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json", // JSON schema definition, use vscode or helix to make it work
  "logo": { // Logo configuration
    "type": "auto", // Set logo type. "auto": auto detection based on source; "builtin": print built-in ASCII logo, ...
    "source": "" // Set the source file of the logo, or built-in ASCII art name
  }
}

Each comment are attached to one object key, and one key can attach only one comment. When printing:

  • If YYJSON_WRITE_PRETTY(_TWO_SPACES) is specified, a line comment (//) can be either printed at the end of the same line of the attached key ( see the code above ), or be printed at the line before the attached key:
{
  // This comment is attached to key `$schema`, so it's printed before key `$schema` with the key's indentation
  "$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
  // The comment attached to key `logo`
  "logo": {
    // The comment attached to key `logo.type`
    "type": "auto",
    // The comment attached to key `logo.source`
    "source": ""
  }
}
  • If YYJSON_WRITE_PRETTY(_TWO_SPACES) is not specified ( I think this is less useful when users want to print comments ), an inline comment (/* */) will be added right before / after the key, for example:
{"$schema"/*The comment attached to key `$schema`*/:"https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json","logo"/*The comment attached to key `logo`*/:{"type"/*The comment attached to key `logo.type`...*/:"auto"}}

Proposed API:

bool yyjson_mut_obj_add_val_with_comment(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, yyjson_mut_val *val, const char* str); // str is a dynamic string that should be copied into yyjson doc
// And other `yyjson_mut_obj_add_*_with_comment` friends

Describe alternatives you've considered
Comments can be attached to any yyjson_mut_val. It's more generic but more complex and I don't think it has many use cases. For now, I think attaching to object keys only is enough.

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions