Example for json.pretty (#10466)
This commit is contained in:
parent
fa7b757823
commit
7118e1ca01
1 changed files with 22 additions and 3 deletions
|
|
@ -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)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue