diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 912cd68379a6a..a2501f63f05b1 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -1339,6 +1339,8 @@ else: proc parseJson*(buffer: string): JsonNode = return parseNativeJson(buffer).convertObject() +proc to*(s: string; dest: typedesc[JsonNode]): auto = parseJson(s) + # -- Json deserialiser macro. -- proc createJsonIndexer(jsonNode: NimNode, @@ -1979,6 +1981,7 @@ when isMainModule: let stringified = $testJson let parsedAgain = parseJson(stringified) doAssert(parsedAgain["b"].str == "asd") + doAssert parsedAgain == stringified.to(JsonNode) parsedAgain["abc"] = %5 doAssert parsedAgain["abc"].num == 5 diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index ddd0e9e620180..f7a61ace7bd56 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1078,7 +1078,7 @@ proc parseHexStr*(s: string): string {.noSideEffect, procvar, else: result[pos div 2] = chr(val + buf shl 4) -proc parseBool*(s: string): bool = +proc to*(s: string; dest: typedesc[bool]): bool = ## Parses a value into a `bool`. ## ## If ``s`` is one of the following values: ``y, yes, true, 1, on``, then @@ -1090,6 +1090,9 @@ proc parseBool*(s: string): bool = of "n", "no", "false", "0", "off": result = false else: raise newException(ValueError, "cannot interpret as a bool: " & s) +# TODO: deprecate +proc parseBool*(s: string): bool = s.to(bool) + proc parseEnum*[T: enum](s: string): T = ## Parses an enum ``T``. ## @@ -2498,6 +2501,8 @@ proc removePrefix*(s: var string, prefix: string) {. s.delete(0, prefix.len - 1) when isMainModule: + doAssert "yes".to(bool) == true + doAssert align("abc", 4) == " abc" doAssert align("a", 0) == "a" doAssert align("1232", 6) == " 1232" @@ -2734,3 +2739,4 @@ bar doAssert s.endsWith('\0') == false #echo("strutils tests passed") +