KSON (Kson Structured Object Notation)

repository·main·Indexed 41 days ago

https://github.com/kson-org/kson

An RFC 8259-compliant superset of JSON that combines YAML-like readability with JSON robustness. KSON features embed blocks for multi-line code, flexible string handling, and greedy parsing for objects and lists. It provides a Kotlin Multiplatform interface, a schema-aware KsonTooling API, and Python bindings via kson-lang 0.4.0.dev0 for transpilation to JSON and YAML.

Tokens
28.5K
Snippets
110
Records
142
Agent score
51%

What's inside KSON

  1. What is Stdlibx?

    main
    Stdlibx is a collection of reusable, stdlib-type extensions and utilities designed for the KSON ecosystem. It provides language and library-level enhancements that are intended to be generally applicable. Note that elements within this package are considered experimental or provisional; proven components may eventually graduate to a dedicated shared project or library.
  2. Overview of Kson Language Server

    main
    The Kson Language Server is a TypeScript-based implementation of the Language Server Protocol (LSP) for the Kson language. It provides standardized language support that can be integrated into any LSP-compatible editor, such as Visual Studio Code, Neovim, or Sublime Text, without requiring editor-specific extensions.
  3. What is KSON?

    main

    KSON (Kson Structured Object Notation) is a data format that combines the robustness and efficiency of JSON with the cleanliness and readability of YAML.

    Key features include:

    • JSON Compatibility: It is a verified superset of JSON.
    • JSON Schema Support: Includes native support for JSON Schema.
    • YAML Transpilation: Can reliably transpile to YAML while preserving comments.
    • Multi-platform Support: Available for JS, Python, Rust, JVM, and Kotlin Multiplatform.
    • Tooling: Supported in VS Code, JetBrains IDEs, and via the Language Server Protocol (LSP).
  4. Use the KSON VS Code Extension

    main

    The KSON VS Code Extension provides full language support for .kson files within Visual Studio Code. It enables a complete development experience including visual feedback and automated tooling.

    Key Features:

    • Syntax Highlighting: Visual color coding for .kson files to improve readability.
    • Language Server Integration: Provides advanced intelligence and features via the Kson Language Server.
    • Auto-formatting: Automatically cleans up and structures your KSON code.
    • Diagnostics and Error Reporting: Real-time feedback on syntax errors and structural issues within your KSON files.
  5. Use the KsonTooling API for building KSON tools

    main

    The KsonTooling.kt interface provides a Kotlin Multiplatform API for building schema-aware tools for KSON. It allows you to perform deep analysis of KSON documents by leveraging the underlying schema to extract information at any specific location within a document.

    Key capabilities include:

    • Schema Information Retrieval: Extract type information, constraints, metadata, and schema documentation for any position in a KSON document.
    • Completion Suggestions: Retrieve valid property names, enum values, and type-appropriate suggestions based on the schema.
    • Document Path Navigation: Navigate through KSON documents using schema-aware path building.

    This API is suitable for developing IDE extensions, linters, documentation generators, CLI tools, or web-based editors that require schema-aware document analysis.

    // The public interface is defined in:
    // src/commonMain/kotlin/org/kson/KsonTooling.kt
  6. What is KSON and how does it relate to JSON?

    main
    KSON (Kson Structured Object Notation) is an RFC 8259-compliant superset of JSON. This means any valid JSON document is also a valid KSON document. KSON extends JSON by adding features like YAML-like plain syntax for objects and lists (without relying on significant whitespace), embed blocks for code, and more flexible string handling (unquoted simple strings and single-quote support).
  7. How KSON schema matching works

    main

    When a KSON file is opened, the language server follows this resolution logic:

    1. Searches for .kson-schema.kson in the workspace root.
    2. Matches the file path against the fileMatch patterns provided in the configuration.
    3. Loads the schema file specified by the schema path.
    4. Applies the schema for completions and hover information.

    Note on multiple matches: If a file matches multiple fileMatch patterns, the language server uses the first matching schema found in the configuration array.

    Fallback: If no configuration is found or no patterns match, the server falls back to a built-in test schema.

  8. How embed blocks work in KSON

    main

    KSON supports embed blocks for embedding raw content (like SQL or HTML) using a syntax like $tag\ncontent\n$$.

    Parsing Embed Blocks

    When using Kson.analyze(), embed blocks are represented as KsonValue.KsonEmbed objects. You can access the metadata via .tag() and the raw content via .content().

    Transpilation Behavior

    When converting to JSON or YAML:

    • retain_embed_tags=True: Preserves the embed metadata.
    • retain_embed_tags=False: Flattens the embed block into a plain string.
    from kson import Kson, KsonValue
    
    analysis = Kson.analyze("query: $sql\nSELECT * FROM users\n$$", None, None)
    value = analysis.kson_value()
    
    if isinstance(value, KsonValue.KsonObject):
        embed = value.properties()["query"]
        if isinstance(embed, KsonValue.KsonEmbed):
            print(f"Tag: {embed.tag()}")        # "sql"
            print(f"Content: {embed.content()}")  # "SELECT * FROM users"
  9. How plain structures end in KSON

    main

    Because KSON does not use significant whitespace (unlike YAML), plain structures (Plain Dash Lists and Plain Objects) rely on greedy parsing.

    • Plain Dash Lists absorb every - element they can, ending at the first non-dash element or an explicit end-dash =.
    • Plain Objects absorb every key: value property they can, ending at the first non-property element or an explicit end-dot ..

    When to use terminators: You only need a terminator (= for lists, . for objects) when the greedy parser would otherwise incorrectly absorb an element/property that belongs to a parent structure. If a structure ends naturally (e.g., a list ends because the next line is a new key), no terminator is required.

    Explicitly using terminators is always legal; the KSON formatter will remove them if they are unnecessary.

    # Natural end: the list ends because 'lucky_color' is a key, not a list element
    lucky_numbers: 
      - 7
      - 13
    lucky_color: blue
    
    # Required end-dot: without '.', 'favorite_movie' would be absorbed into the book object
    favorite_books:
      - title: Elements
        author: Euclid
      - title: Metaphysics
        author: Aristotle
        .
    favorite_movie: 'The Rock'
  10. KSON JSON Compatibility and Schema Support

    main

    KSON is designed to be highly compatible with the JSON ecosystem:

    • Superset of JSON: KSON is a verified superset of JSON.
    • JSON Schema: KSON is compatible with JsonSchema (Draft 7).
    • Embed Block Equivalence: An Embed Block is syntactically equivalent to a JSON object with exactly two string properties: embedTag: and embedContent:.
    • Transpilation: When transpiling KSON to JSON or YAML, you can choose to 'preserve' embed tags (keeping them as objects) or transpile them down to their embedContent: string value for compatibility with legacy systems.
  11. Handle dynamic linking for KSON Rust bindings

    main

    Because the KSON bindings use dynamic linking, the operating system must be able to locate the KSON shared library at runtime. A common way to ensure this is to use the KSON_COPY_SHARED_LIBRARY_TO_DIR environment variable during development to place the library in the same directory as your compiled binary (e.g., target/debug).

    export KSON_COPY_SHARED_LIBRARY_TO_DIR=target/debug