Example for json.pretty (#10466)

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

View File

@@ -130,9 +130,9 @@
## { "name": "Susan", "age": herAge }
## ]
##
## var j2 = %* {"name": "Isaac", "books": ["Robot Dreams"]}
## j2["details"] = %* {"age":35, "pi":3.1415}
## echo j2
## var j2 = %* {"name": "Isaac", "books": ["Robot Dreams"]}
## j2["details"] = %* {"age":35, "pi":3.1415}
## echo j2
runnableExamples:
## 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 =
## Returns a JSON Representation of `node`, with indentation and
## 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 = ""
toPretty(result, node, indent)