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"