src/kdl/jik

Search:
Group by:
Source   Edit  

JiK

This modules implements the JSON-in-KDL (JiK) specification to encode and decode JSON in KDL.

Checkout the official specification: https://github.com/kdl-org/kdl/blob/main/JSON-IN-KDL.md.

Example:

import src/kdl/jik
import std/json
import kdl

let data = """
{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": {
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}""".parseJson()
assert data.toKdl() == parseKdl("""
(object)- {
    (object)widget {
        debug "on"
        (object)window {
            title "Sample Konfabulator Widget"
            name "main_window"
            width 500
            height 500
        }
        (object)image {
            src "Images/Sun.png"
            name "sun1"
            hOffset 250
            vOffset 250
            alignment "center"
        }
        (object)text {
            data "Click Here"
            size 36
            style "bold"
            name "text1"
            hOffset 250
            vOffset 100
            alignment "center"
            onMouseUp "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
    }
}""")[0]

assert data == data.toKdl().toJson()

Procs

proc toJson(node: KdlNode): JsonNode {....raises: [KdlError, Exception, ValueError],
                                       tags: [RootEffect], forbids: [].}
Source   Edit  
proc toKdl(node: JsonNode; name = "-"): KdlNode {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
Source   Edit