From: Alexis Laferrière Date: Tue, 19 May 2015 17:36:37 +0000 (-0400) Subject: tests: extract JSON logic from test_deserialization X-Git-Tag: v0.7.5~38^2~4 X-Git-Url: http://nitlanguage.org tests: extract JSON logic from test_deserialization Signed-off-by: Alexis Laferrière --- diff --git a/lib/json/serialization.nit b/lib/json/serialization.nit index 845879c..98a8e87 100644 --- a/lib/json/serialization.nit +++ b/lib/json/serialization.nit @@ -320,16 +320,22 @@ redef class SimpleCollection[E] redef fun serialize_to_json(v) do # Register as pseudo object - var id = v.ref_id_for(self) - v.stream.write """{"__kind": "obj", "__id": """ - v.stream.write id.to_s - v.stream.write """, "__class": """" - v.stream.write class_name - v.stream.write """", "__length": """ - v.stream.write length.to_s - v.stream.write """, "__items": """ + if not v.plain_json then + var id = v.ref_id_for(self) + v.stream.write """{"__kind": "obj", "__id": """ + v.stream.write id.to_s + v.stream.write """, "__class": """" + v.stream.write class_name + v.stream.write """", "__length": """ + v.stream.write length.to_s + v.stream.write """, "__items": """ + end + serialize_to_pure_json v - v.stream.write "\}" + + if not v.plain_json then + v.stream.write "\}" + end end redef init from_deserializer(v: Deserializer) @@ -368,11 +374,11 @@ redef class Map[K, V] if v.plain_json then v.stream.write "\{" - var first = false + var first = true for key, val in self do if not first then v.stream.write ", " - else first = true + else first = false if key == null then key = "null" diff --git a/tests/sav/nitg-e/test_deserialization_serial.res b/tests/sav/nitg-e/test_deserialization_serial.res deleted file mode 100644 index 90e0d60..0000000 --- a/tests/sav/nitg-e/test_deserialization_serial.res +++ /dev/null @@ -1,40 +0,0 @@ -Runtime error: Aborted (../lib/serialization/serialization.nit:109) -# Nit: - - -# Json: -{"__kind": "obj", "__id": 0, "__class": "A", "b": true, "c": {"__kind": "char", "__val": "a"}, "f": 0.123, "i": 1234, "s": "asdf", "n": null} - -# Back in Nit: - - -# Nit: - 1111 qwer> - -# Json: -{"__kind": "obj", "__id": 0, "__class": "B", "b": false, "c": {"__kind": "char", "__val": "b"}, "f": 123.123, "i": 2345, "s": "hjkl", "n": null, "ii": 1111, "ss": "qwer"} - -# Back in Nit: - 1111 qwer> - -# Nit: - 1111 qwer>> - -# Json: -{"__kind": "obj", "__id": 0, "__class": "C", "a": {"__kind": "obj", "__id": 1, "__class": "A", "b": true, "c": {"__kind": "char", "__val": "a"}, "f": 0.123, "i": 1234, "s": "asdf", "n": null}, "b": {"__kind": "obj", "__id": 2, "__class": "B", "b": false, "c": {"__kind": "char", "__val": "b"}, "f": 123.123, "i": 2345, "s": "hjkl", "n": null, "ii": 1111, "ss": "qwer"}, "aa": {"__kind": "ref", "__id": 1}} - -# Back in Nit: - 1111 qwer>> - -# Nit: - -<- false> 1111 f" \/> true> - -# Json: -{"__kind": "obj", "__id": 0, "__class": "D", "b": false, "c": {"__kind": "char", "__val": "b"}, "f": 123.123, "i": 2345, "s": "new line ->\n<-", "n": null, "ii": 1111, "ss": "\tf\"\r\\\/", "d": {"__kind": "ref", "__id": 0}} - -# Back in Nit: - -<- false> 1111 f" \/> true> - -Error: doesn't know how to deserialize class "Array" diff --git a/tests/sav/nitg-e/test_deserialization.res b/tests/sav/nitg-e/test_json_deserialization_alt1.res similarity index 100% rename from tests/sav/nitg-e/test_deserialization.res rename to tests/sav/nitg-e/test_json_deserialization_alt1.res diff --git a/tests/sav/test_deserialization.res b/tests/sav/test_deserialization.res deleted file mode 100644 index 13f9a72..0000000 --- a/tests/sav/test_deserialization.res +++ /dev/null @@ -1,40 +0,0 @@ -Runtime error: Aborted (../lib/serialization/serialization.nit:109) -# Nit: - - -# Json: -{"__kind": "obj", "__id": 0, "__class": "A", "b": true, "c": {"__kind": "char", "__val": "a"}, "f": 0.123, "i": 1234, "s": "asdf", "n": null} - -# Back in Nit: - - -# Nit: - 1111 qwer> - -# Json: -{"__kind": "obj", "__id": 0, "__class": "B", "b": false, "c": {"__kind": "char", "__val": "b"}, "f": 123.123, "i": 2345, "s": "hjkl", "n": null, "ii": 1111, "ss": "qwer"} - -# Back in Nit: - 1111 qwer> - -# Nit: - 1111 qwer>> - -# Json: -{"__kind": "obj", "__id": 0, "__class": "C", "a": {"__kind": "obj", "__id": 1, "__class": "A", "b": true, "c": {"__kind": "char", "__val": "a"}, "f": 0.123, "i": 1234, "s": "asdf", "n": null}, "b": {"__kind": "obj", "__id": 2, "__class": "B", "b": false, "c": {"__kind": "char", "__val": "b"}, "f": 123.123, "i": 2345, "s": "hjkl", "n": null, "ii": 1111, "ss": "qwer"}, "aa": {"__kind": "ref", "__id": 1}} - -# Back in Nit: - 1111 qwer>> - -# Nit: - -<- false> 1111 f" \/> true> - -# Json: -{"__kind": "obj", "__id": 0, "__class": "D", "b": false, "c": {"__kind": "char", "__val": "b"}, "f": 123.123, "i": 2345, "s": "new line ->\n<-", "n": null, "ii": 1111, "ss": "\tf\"\r\\\/", "d": {"__kind": "ref", "__id": 0}} - -# Back in Nit: - -<- false> 1111 f" \/> true> - -Error: doesn't know how to deserialize class "Array[Object]" diff --git a/tests/sav/test_deserialization_alt1.res b/tests/sav/test_json_deserialization.res similarity index 100% rename from tests/sav/test_deserialization_alt1.res rename to tests/sav/test_json_deserialization.res diff --git a/tests/sav/test_deserialization_serial.res b/tests/sav/test_json_deserialization_alt1.res similarity index 100% rename from tests/sav/test_deserialization_serial.res rename to tests/sav/test_json_deserialization_alt1.res diff --git a/tests/test_deserialization.nit b/tests/test_deserialization.nit index f26bcbf..1360bf2 100644 --- a/tests/test_deserialization.nit +++ b/tests/test_deserialization.nit @@ -15,7 +15,6 @@ # limitations under the License. import serialization -import json_serialization # Simple class class A @@ -93,7 +92,6 @@ class E var a = new Array[Object].with_items("hello", 1234, 123.4) var b = new Array[nullable Serializable].with_items("hella", 2345, 234.5) - init do end redef fun to_s do return "" end @@ -103,7 +101,6 @@ class F[N: Numeric] auto_serializable var n: N - init(n: N) do self.n = n redef fun to_s do return "" end @@ -133,31 +130,23 @@ class G "hm: {hm.join(", ", ". ")}; am: {am.join(", ", ". ")}>" end -var a = new A(true, 'a', 0.1234, 1234, "asdf", null) -var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer") -var c = new C(a, b) -var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/") -d.d = d -var e = new E -var fi = new F[Int](2222) -var ff = new F[Float](33.33) -var g = new G - -# Default works only with Nit serial -var tests = [a, b, c, d, e, fi, ff, g: Serializable] - -# Alt1 should work without nitserial -#alt1# tests = new Array[Serializable].with_items(a, b, c, d) - -for o in tests do - var stream = new StringWriter - var serializer = new JsonSerializer(stream) - serializer.serialize(o) - - var deserializer = new JsonDeserializer(stream.to_s) - var deserialized = deserializer.deserialize - - print "# Nit:\n{o}\n" - print "# Json:\n{stream}\n" - print "# Back in Nit:\n{deserialized or else "null"}\n" +class TestEntities + var a = new A(true, 'a', 0.1234, 1234, "asdf", null) + var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer") + var c = new C(a, b) + var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/") + init do d.d = d + var e = new E + var fi = new F[Int](2222) + var ff = new F[Float](33.33) + var g = new G + + # should work without nitserial + var without_generics: Array[Serializable] = [a, b, c, d: Serializable] + + # Default works only with Nit serial + var with_generics: Array[Serializable] = [a, b, c, d, e, fi, ff, g: Serializable] end + +# We instanciate it here so that `nitserial` detects generic types as being alive +var entities = new TestEntities diff --git a/tests/test_deserialization_serial.nit b/tests/test_deserialization_serial.nit index 7d1d443..b0be7fa 100644 --- a/tests/test_deserialization_serial.nit +++ b/tests/test_deserialization_serial.nit @@ -33,13 +33,7 @@ redef class Deserializer if name == "ArrayMap[String, String]" then return new ArrayMap[String, String].from_deserializer(self) if name == "Array[Serializable]" then return new Array[Serializable].from_deserializer(self) if name == "Array[String]" then return new Array[String].from_deserializer(self) - if name == "HashMap[Serializable, Int]" then return new HashMap[Serializable, Int].from_deserializer(self) - if name == "Array[JsonObject]" then return new Array[JsonObject].from_deserializer(self) - if name == "HashMap[Int, Object]" then return new HashMap[Int, Object].from_deserializer(self) - if name == "Array[Node]" then return new Array[Node].from_deserializer(self) - if name == "Array[LRState]" then return new Array[LRState].from_deserializer(self) if name == "Array[Couple[String, String]]" then return new Array[Couple[String, String]].from_deserializer(self) - if name == "Array[nullable Jsonable]" then return new Array[nullable Jsonable].from_deserializer(self) return super end end diff --git a/tests/test_json_deserialization.nit b/tests/test_json_deserialization.nit new file mode 100644 index 0000000..64bff64 --- /dev/null +++ b/tests/test_json_deserialization.nit @@ -0,0 +1,35 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import test_deserialization +import json::serialization +#alt1# import test_deserialization_serial + +var entities = new TestEntities + +var tests = entities.without_generics#alt1# +#alt1#var tests = entities.with_generics + +for o in tests do + var stream = new StringWriter + var serializer = new JsonSerializer(stream) + serializer.serialize(o) + + var deserializer = new JsonDeserializer(stream.to_s) + var deserialized = deserializer.deserialize + + print "# Nit:\n{o}\n" + print "# Json:\n{stream}\n" + print "# Back in Nit:\n{deserialized or else "null"}\n" +end