nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
model: add missing service to retrieve a AClassDef from a MClassDef.
[nit.git]
/
lib
/
json_serialization.nit
diff --git
a/lib/json_serialization.nit
b/lib/json_serialization.nit
index
f090b24
..
e8c76b4
100644
(file)
--- a/
lib/json_serialization.nit
+++ b/
lib/json_serialization.nit
@@
-14,18
+14,18
@@
# See the License for the specific language governing permissions and
# limitations under the License.
# See the License for the specific language governing permissions and
# limitations under the License.
+# Handles serialization and deserialization of objects to/from Json.
module json_serialization
import serialization
import json::static
module json_serialization
import serialization
import json::static
+# Serializer of Nit objects to Json string.
class JsonSerializer
super Serializer
# Target writing stream
class JsonSerializer
super Serializer
# Target writing stream
- var stream: OStream
-
- init(stream: OStream) do self.stream = stream
+ var stream: Writer
redef fun serialize(object)
do
redef fun serialize(object)
do
@@
-52,9
+52,10
@@
class JsonSerializer
end
end
end
end
- # Map of references to already serialized objects
+ # Map of references to already serialized objects.
var refs_map = new HashMap[Serializable,Int]
var refs_map = new HashMap[Serializable,Int]
+ # Get the internal serialized reference for this `object`.
private fun ref_id_for(object: Serializable): Int
do
if refs_map.keys.has(object) then
private fun ref_id_for(object: Serializable): Int
do
if refs_map.keys.has(object) then
@@
-67,18
+68,28
@@
class JsonSerializer
end
end
end
end
-# Deserializer from a Json string
+# Deserializer from a Json string.
class JsonDeserializer
super Deserializer
class JsonDeserializer
super Deserializer
- var root: nullable Jsonable
+ # Json text to deserialize from.
+ private var text: Text
+
+ # Root json object parsed from input text.
+ var root: nullable Jsonable is noinit
+
+ # Depth-first path in the serialized object tree.
var path = new Array[JsonObject]
var path = new Array[JsonObject]
+
+ # Map of refenrences to already deserialized objects.
var id_to_object = new HashMap[Int, Object]
var id_to_object = new HashMap[Int, Object]
+ # Last encountered object reference id.
+ #
+ # See `id_to_object`.
var just_opened_id: nullable Int = null
var just_opened_id: nullable Int = null
- init(text: Text)
- do
+ init do
var root = text.parse_json
if root isa JsonObject then path.add(root)
self.root = root
var root = text.parse_json
if root isa JsonObject then path.add(root)
self.root = root
@@
-91,7
+102,7
@@
class JsonDeserializer
assert current.keys.has(name)
var value = current[name]
assert current.keys.has(name)
var value = current[name]
-
+
return convert_object(value)
end
return convert_object(value)
end
@@
-153,7
+164,7
@@
class JsonDeserializer
assert val isa String
if val.length != 1 then print "Error: expected a single char when deserializing '{val}'."
assert val isa String
if val.length != 1 then print "Error: expected a single char when deserializing '{val}'."
-
+
return val.chars.first
end
return val.chars.first
end
@@
-246,6
+257,7
@@
redef class Array[E]
end
end
end
end
+ # Instanciate a new `Array` from its serialized representation.
init from_deserializer(v: Deserializer)
do
if v isa JsonDeserializer then
init from_deserializer(v: Deserializer)
do
if v isa JsonDeserializer then