niti: extract attribute expr evaluation in its own method
[nit.git] / lib / json_serialization.nit
index b4b1062..64f3d5f 100644 (file)
@@ -17,7 +17,7 @@
 module json_serialization
 
 import serialization
-import simple_json_reader
+import json::static
 
 class JsonSerializer
        super Serializer
@@ -77,7 +77,7 @@ class JsonDeserializer
 
        var just_opened_id: nullable Int = null
 
-       init(text: String)
+       init(text: Text)
        do
                var root = text.json_to_nit_object
                if root isa HashMap[String, nullable Object] then path.add(root)
@@ -146,9 +146,18 @@ class JsonDeserializer
                                return value
                        end
 
-                       # char? TODO
+                       # char?
+                       if kind == "char" then
+                               assert object.keys.has("__val")
+                               var val = object["__val"]
+                               assert val isa String
 
-                       print "Malformed Json string: unexpected Json Object kind '{kind}'"
+                               if val.length != 1 then print "Error: expected a single char when deserializing '{val}'."
+                               
+                               return val.chars.first
+                       end
+
+                       print "Malformed Json string: unexpected Json Object kind '{kind or else "null"}'"
                        abort
                end
 
@@ -181,17 +190,18 @@ redef class Bool
 end
 
 redef class Char
-       redef fun serialize_to_json(v) do v.stream.write("'{to_s}'")
+       redef fun serialize_to_json(v) do v.stream.write "\{\"__kind\": \"char\", \"__val\": \"{to_s.to_json_s}\"\}"
 end
 
 redef class String
        redef fun serialize_to_json(v) do v.stream.write("\"{to_json_s}\"")
 
        private fun to_json_s: String do return self.replace("\\", "\\\\").
-               replace("\"", "\\\"").replace("\b", "\\b").replace("/", "\\/").
+               replace("\"", "\\\"").replace("/", "\\/").
                replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t")
                # FIXME add support for unicode char when supported by Nit strings
                # FIXME add support for \f! # .replace("\f", "\\f")
+               # FIXME add support for \b .replace("\b", "\\b")
 end
 
 redef class NativeString