src/nimjson

Procs

proc toTypeString(jsonString: string; objName = "Object"; publicField = false;
                  quoteField = false; jsonSchema = false;
                  disableOptionType = false): string {....raises: [JsonError,
    ValueError, Exception, UnsupportedRefError, UnsupportedTypeError, IOError,
    OSError, JsonParsingError], tags: [RootEffect, ReadIOEffect, WriteIOEffect],
    forbids: [].}
Generates nim object definitions string from string. Returns a public field string if publicField was true. Handles jsonString as JSON Schema format when jsonSchema is true. disableOptionType is available only when jsonSchema is true.

Example:

let typeStr = """
{
  "type": "object",
  "required": [
    "type",
    "id",
    "timestamp",
    "stream",
    "consumer",
    "consumer_seq",
    "stream_seq",
    "deliveries"
  ],
  "additionalProperties": false,
  "properties": {
    "type": {
      "type": "string",
      "const": "io.nats.jetstream.advisory.v1.nak"
    },
    "id": {
      "type": "string",
      "description": "Unique correlation ID for this event"
    },
    "timestamp": {
      "type": "string",
      "description": "The time this event was created in RFC3339 format"
    },
    "stream": {
      "type": "string",
      "description": "The name of the stream where the message is stored"
    },
    "consumer": {
      "type": "string",
      "description": "The name of the consumer where the message was naked"
    },
    "consumer_seq": {
      "type": "string",
      "minimum": 1,
      "description": "The sequence of the message in the consumer that was naked"
    },
    "stream_seq": {
      "type": "string",
      "minimum": 1,
      "description": "The sequence of the message in the stream that was naked"
    },
    "deliveries": {
      "type": "integer",
      "minimum": 1,
      "description": "The number of deliveries that were attempted"
    },
    "domain": {
      "type": "string",
      "minimum": 1,
      "description": "The domain of the JetStreamServer"
    }
  }
}
""".toTypeString(jsonSchema = true)
doAssert typeStr == """type
  Object = ref object
    `type`: string
    id: string
    timestamp: string
    stream: string
    consumer: string
    consumer_seq: string
    stream_seq: string
    deliveries: int64
    domain: Option[string]"""
proc toTypeString(self: JsonNode; objName = "Object"; publicField = false;
                  quoteField = false): string {....raises: [ValueError], tags: [],
    forbids: [].}

Generates nim object definitions string from JsonNode. Returns a public field string if publicField was true.

This procedure is left for backward compatibility. Please use toTypeString proc.

Japanese:

JsonNode ใ‚’NimใฎObjectๅฎš็พฉใฎๆ–‡ๅญ—ๅˆ—ใซๅค‰ๆ›ใ—ใฆ่ฟ”ๅดใ™ใ‚‹ใ€‚ objName ใŒๅฎš็พฉใ™ใ‚‹Objectใฎๅๅ‰ใซใชใ‚‹ใ€‚ publicField ใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใจใ€ๅ…ฌ้–‹ใƒ•ใ‚ฃใƒผใƒซใƒ‰ใจใ—ใฆๆ–‡ๅญ—ๅˆ—ใ‚’่ฟ”ๅดใ™ใ‚‹ใ€‚

Note:

  • ๅ€คใŒ null ใ‚ใ‚‹ใ„ใฏ้…ๅˆ—ใฎๆœ€ๅˆใฎ่ฆ็ด ใŒ null ใ‚„ๅ€คใŒ็ฉบ้…ๅˆ—ใฎๅ ดๅˆใฏใ€ ๅž‹ใŒ nilType ใซใชใ‚‹ใ€‚

Example:

import std/json
from std/strutils import split

let typeStr = """{"keyStr":"str",
                      "keyInt":1,
                      "keyFloat":1.1,
                      "keyBool":true}""".parseJson().toTypeString()
let typeLines = typeStr.split("\n")
doAssert typeLines[0] == "type"
doAssert typeLines[1] == "  NilType = ref object"
doAssert typeLines[2] == "  Object = ref object"
doAssert typeLines[3] == "    keyStr: string"
doAssert typeLines[4] == "    keyInt: int64"
doAssert typeLines[5] == "    keyFloat: float64"
doAssert typeLines[6] == "    keyBool: bool"