mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-21 16:31:39 +00:00
Example for json.pretty (#10466)
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user