examples: add json example from rosetta code
authorAlexandre Terrasa <alexandre@moz-code.org>
Wed, 10 Jun 2015 20:07:16 +0000 (16:07 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 10 Jun 2015 20:07:16 +0000 (16:07 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

examples/rosettacode/json_output.nit [new file with mode: 0644]
tests/sav/json_output.res [new file with mode: 0644]

diff --git a/examples/rosettacode/json_output.nit b/examples/rosettacode/json_output.nit
new file mode 100644 (file)
index 0000000..59fb72b
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: JSON
+# SEE: <http://rosettacode.org/wiki/JSON>
+module json_output
+
+import json
+
+var str = """{
+       "blue": [1, 2],
+       "ocean": "water"
+}"""
+
+var json = str.parse_json
+
+# Print json object
+print json.to_json
+print json.class_name
+if not json isa JsonObject then return
+
+# Print json array
+var arr = json["blue"]
+print arr.to_json
+print arr.class_name
+
+# edit the object
+json["ocean"] = new JsonArray.from(["fishy", "salty"])
+print json.to_json
+print json.to_pretty_json
diff --git a/tests/sav/json_output.res b/tests/sav/json_output.res
new file mode 100644 (file)
index 0000000..4162e8d
--- /dev/null
@@ -0,0 +1,10 @@
+{"blue":[1,2],"ocean":"water"}
+JsonObject
+[1,2]
+JsonArray
+{"blue":[1,2],"ocean":["fishy","salty"]}
+{
+       "blue": [1, 2],
+       "ocean": ["fishy", "salty"]
+}
+