From 9ce53fbd52b0d8e8cd87c11a5e3b67484c6cb37c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sat, 16 May 2015 21:54:17 -0400 Subject: [PATCH] lib/json_serialization: extract `serialize_to_pure_json` from `Array` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/json_serialization.nit | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/json_serialization.nit b/lib/json_serialization.nit index 8b4ee97..e78fb1f 100644 --- a/lib/json_serialization.nit +++ b/lib/json_serialization.nit @@ -219,45 +219,44 @@ redef class NativeString redef fun serialize_to_json(v) do to_s.serialize_to_json(v) end -redef class Array[E] - redef fun serialize_to_json(v) +redef class Collection[E] + # Utility to serialize a normal Json array + private fun serialize_to_pure_json(v: JsonSerializer) do - if class_name == "Array[nullable Serializable]" then - # Using class_name to the the exact type - # We do not want Array[Int] or anything else here v.stream.write "[" var is_first = true for e in self do if is_first then is_first = false - else v.stream.write(", ") + else v.stream.write ", " if not v.try_to_serialize(e) then v.warn("element of type {e.class_name} is not serializable.") end end v.stream.write "]" + end +end + +redef class Array[E] + redef fun serialize_to_json(v) + do + if class_name == "Array[nullable Serializable]" then + # Using class_name to get the exact type, + # we do not want Array[Int] or anything else here. + + serialize_to_pure_json v else # Register as pseudo object var id = v.ref_id_for(self) v.stream.write "\{\"__kind\": \"obj\", \"__id\": {id}, \"__class\": \"{class_name}\"" - v.stream.write """, "__length": {{{length}}}, "__items": [""" - var is_first = true - for e in self do - if is_first then - is_first = false - else v.stream.write(", ") - - if not v.try_to_serialize(e) then - v.warn("element of type {e.class_name} is not serializable.") - end - end - v.stream.write "]" + v.stream.write """, "__length": {{{length}}}, "__items": """ + serialize_to_pure_json v v.stream.write "\}" end end - # Instanciate a new `Array` from its serialized representation. + # Instantiate a new `Array` from its serialized representation. redef init from_deserializer(v: Deserializer) do if v isa JsonDeserializer then -- 1.7.9.5