Example for json.pretty (#10466)

This commit is contained in:
Mandeep Singh 2019-01-30 10:35:45 -05:00 • committed by Miran
commit 7118e1ca01

View file

@ -130,9 +130,9 @@
## { "name": "Susan", "age": herAge } ## { "name": "Susan", "age": herAge }
## ] ## ]
## ##
## var j2 = %* {"name": "Isaac", "books": ["Robot Dreams"]} ## var j2 = %* {"name": "Isaac", "books": ["Robot Dreams"]}
## j2["details"] = %* {"age":35, "pi":3.1415} ## j2["details"] = %* {"age":35, "pi":3.1415}
## echo j2 ## echo j2
runnableExamples: runnableExamples:
## Note: for JObject, key ordering is preserved, unlike in some languages, ## Note: for JObject, key ordering is preserved, unlike in some languages,
@ -708,6 +708,25 @@ proc toPretty(result: var string, node: JsonNode, indent = 2, ml = true,
proc pretty*(node: JsonNode, indent = 2): string = proc pretty*(node: JsonNode, indent = 2): string =
## Returns a JSON Representation of `node`, with indentation and ## Returns a JSON Representation of `node`, with indentation and
## on multiple lines. ## on multiple lines.
##
## Similar to prettyprint in Python.
##
## **Examples:**
##
## .. code-block:: Nim
## let j = %* {"name": "Isaac", "books": ["Robot Dreams"],
## "details": {"age":35, "pi":3.1415}}
## echo pretty(j)
## # {
## # "name": "Isaac",
## # "books": [
## # "Robot Dreams"
## # ],
## # "details": {
## # "age": 35,
## # "pi": 3.1415
## # }
## # }
result = "" result = ""
toPretty(result, node, indent) toPretty(result, node, indent)