tjson.nim
1 # NimYAML - YAML implementation in Nim 2 # (c) Copyright 2015-2023 Felix Krause 3 # 4 # See the file "copying.txt", included in this 5 # distribution, for details about the copyright. 6 7 import ../yaml/[ parser, stream, tojson] 8 9 import unittest, json 10 11 proc ensureEqual(yamlIn, jsonIn: string) = 12 try: 13 var 14 yamlParser = initYamlParser(true) 15 s = yamlParser.parse(yamlIn) 16 yamlResult = constructJson(s) 17 jsonResult = parseJson(jsonIn) 18 assert yamlResult.len == 1 19 assert(jsonResult == yamlResult[0], "Expected: " & $jsonResult & ", got: " & 20 $yamlResult[0]) 21 except YamlStreamError as se: 22 let e = (ref YamlParserError)(se.parent) 23 echo "error occurred: " & e.msg 24 echo "line: ", e.mark.line, ", column: ", e.mark.column 25 echo e.lineContent 26 raise e 27 28 suite "Constructing JSON": 29 test "Simple Sequence": 30 ensureEqual("- 1\n- 2\n- 3", "[1, 2, 3]") 31 32 test "Simple Map": 33 ensureEqual("a: b\nc: d", """{"a": "b", "c": "d"}""") 34 35 test "Complex Structure": 36 ensureEqual(""" 37 %YAML 1.2 38 --- 39 Foo: 40 - - a 41 - b 42 - c 43 - bla: blubb 44 Numbers, bools, special values: 45 - 1 46 - true 47 - ~ 48 - 42.23 49 - FALSE 50 """, """{ 51 "Foo": [ 52 [ "a", "b", "c"], 53 { "bla": "blubb"} 54 ], 55 "Numbers, bools, special values": [ 56 1, true, null, 42.23, false 57 ] 58 }""")