lib/core/stream: LineIterator use CachedIterator
[nit.git] / lib / neo4j / graph / json_graph_store.nit
index 84e34cb..16ad037 100644 (file)
@@ -20,14 +20,14 @@ import graph
 #
 # * `"nodes"`: An array with all nodes. Each node is an object with the
 # following properties:
-#      * `"labels"`: An array of all applied labels.
-#      * `"properties"`: An object mapping each defined property to its value.
+#      * `"labels"`: An array of all applied labels.
+#      * `"properties"`: An object mapping each defined property to its value.
 # * `"edges"`: An array with all relationships. Each relationship is an object
 # with the following properties:
-#      * `"type"`: The type (`String`) of the relationship.
-#      * `"properties"`: An object mapping each defined property to its value.
-#      * `"from"`: The local ID of the source node.
-#      * `"to"`: The local ID of the destination node.
+#      * `"type"`: The type (`String`) of the relationship.
+#      * `"properties"`: An object mapping each defined property to its value.
+#      * `"from"`: The local ID of the source node.
+#      * `"to"`: The local ID of the destination node.
 #
 # ~~~nit
 # import neo4j::graph::sequential_id
@@ -37,7 +37,7 @@ import graph
 # a.labels.add "Foo"
 # a["answer"] = 42
 # a["Ultimate question of"] = new JsonArray.from(["life",
-#              "the Universe", "and Everything."])
+#              "the Universe", "and Everything."])
 # graph.nodes.register a
 # var b = graph.create_node
 # b.labels.add "Foo"
@@ -111,7 +111,7 @@ class JsonGraphStore
 end
 
 redef class NeoGraph
-       super Jsonable
+       super Serializable
 
        # Retrieve the graph from the specified JSON document.
        #
@@ -125,7 +125,7 @@ redef class NeoGraph
        # a.labels.add "Foo"
        # a["answer"] = 42
        # a["Ultimate question of"] = new JsonArray.from(["life",
-       #               "the Universe", "and Everything."])
+       #               "the Universe", "and Everything."])
        # graph.nodes.register a
        # var b = graph.create_node
        # b.labels.add "Foo"
@@ -133,7 +133,7 @@ redef class NeoGraph
        # graph.edges.add new NeoEdge(a, "BAZ", b)
        #
        # graph = new NeoGraph.from_json(
-       #               new SequentialNodeCollection("node_id"), graph.to_json)
+       #               new SequentialNodeCollection("node_id"), graph.to_json)
        # assert 1 == graph.edges.length
        # for edge in graph.edges do
        #       assert "BAZ" == edge.rel_type
@@ -186,40 +186,30 @@ redef class NeoGraph
                        var rel_type = json_edge["type"].as(String)
                        var json_properties = json_edge["properties"].as(JsonObject)
                        var edge = new NeoEdge(from, rel_type, to)
-                       edge.properties.recover_with(json_properties)
+                       edge.properties.add_all(json_properties)
                        edges.add edge
                end
        end
 
-       redef fun to_json do return to_json_by_append
-
-       # Append the JSON representation of `self` to the specified buffer.
-       #
-       # For a description of the format, see `JsonGraphStore`.
-       #
-       # SEE: `to_json`
-       redef fun append_json(b) do
-               b.append "\{\"nodes\":["
-               append_entities_json(nodes, b)
-               b.append "],\"edges\":["
-               append_entities_json(edges, b)
-               b.append "]\}"
+       redef fun accept_json_serializer(v) do
+               v.stream.write "\{\"nodes\":["
+               append_entities_json(nodes, v)
+               v.stream.write "],\"edges\":["
+               append_entities_json(edges, v)
+               v.stream.write "]\}"
        end
 
        # Encode `self` in JSON.
        #
        # For a description of the format, see `JsonGraphStore`.
-       #
-       # SEE: `append_json`
-       private fun append_entities_json(entities: Collection[NeoEntity],
-                       b: Buffer) do
+       private fun append_entities_json(entities: Collection[NeoEntity], v: JsonSerializer) do
                var i = entities.iterator
                if i.is_ok then
-                       i.item.append_json_for(self, b)
+                       i.item.append_json_for(self, v)
                        i.next
                        for entity in i do
-                               b.add ','
-                               entity.append_json_for(self, b)
+                               v.stream.write ","
+                               entity.append_json_for(self, v)
                        end
                end
        end
@@ -227,18 +217,18 @@ end
 
 redef class NeoNodeCollection
        # Convert the specified JSON value into a local ID.
-       fun id_from_jsonable(id: nullable Jsonable): ID_TYPE do return id.as(ID_TYPE)
+       fun id_from_jsonable(id: nullable Serializable): ID_TYPE do return id.as(ID_TYPE)
 end
 
 redef class NeoEntity
 
        # Append the JSON representation of the entity to the specified buffer.
-       fun append_json_for(graph: NeoGraph, buffer: Buffer) is abstract
+       fun append_json_for(graph: NeoGraph, v: JsonSerializer) is abstract
 end
 
-# Make `NeoNode` `Jsonable`.
+# Make `NeoNode` `Serializable`.
 redef class NeoNode
-       super Jsonable
+       super Serializable
 
        # Retrieve the node from the specified JSON value.
        #
@@ -248,10 +238,10 @@ redef class NeoNode
        #
        #     var node = new NeoNode.from_json("""
        #     {
-       #       "labels": ["foo", "Bar"],
-       #       "properties": {
-       #               "baz": 42
-       #       }
+       #       "labels": ["foo", "Bar"],
+       #       "properties": {
+       #               "baz": 42
+       #       }
        #     }
        #     """)
        #     assert ["foo", "Bar"] == node.labels
@@ -270,35 +260,30 @@ redef class NeoNode
                var labels = o["labels"].as(JsonArray)
                for lab in labels do self.labels.add(lab.as(String))
                var json_properties = o["properties"].as(JsonObject)
-               properties.recover_with(json_properties)
+               properties.add_all(json_properties)
        end
 
-       redef fun to_json do return to_json_by_append
-
-       # Append the JSON representation of the node to the specified buffer.
-       #
-       # SEE: `JsonGraph`
-       redef fun append_json(b) do
-               b.append "\{\"labels\":["
+       redef fun accept_json_serializer(v) do
+               v.stream.write "\{\"labels\":["
                var i = labels.iterator
                if i.is_ok then
-                       i.item.append_json(b)
+                       i.item.serialize_to v
                        i.next
                        for lab in i do
-                               b.add ','
-                               lab.append_json(b)
+                               v.stream.write ","
+                               lab.serialize_to v
                        end
                end
-               b.append "],\"properties\":"
-               properties.append_json(b)
-               b.add '}'
+               v.stream.write "],\"properties\":"
+               properties.serialize_to v
+               v.stream.write "}"
        end
 
        redef fun to_s do return to_json
 
        # Append the JSON representation of the node to the specified buffer.
-       redef fun append_json_for(graph, buffer) do
-               append_json(buffer)
+       redef fun append_json_for(graph, v) do
+               accept_json_serializer v
        end
 end
 
@@ -307,15 +292,15 @@ redef class NeoEdge
        # Append the JSON representation of the relationship to the specified buffer.
        #
        # Use the IDs specfied by `graph.nodes`.
-       redef fun append_json_for(graph, buffer) do
-               buffer.append "\{\"type\":"
-               rel_type.append_json(buffer)
-               buffer.append ",\"properties\":"
-               properties.append_json(buffer)
-               buffer.append ",\"from\":"
-               graph.nodes.id_of(from).append_json(buffer)
-               buffer.append ",\"to\":"
-               graph.nodes.id_of(to).append_json(buffer)
-               buffer.append "}"
+       redef fun append_json_for(graph, v) do
+               v.stream.write "\{\"type\":"
+               rel_type.as(not null).serialize_to(v)
+               v.stream.write ",\"properties\":"
+               properties.serialize_to(v)
+               v.stream.write ",\"from\":"
+               graph.nodes.id_of(from).serialize_to(v)
+               v.stream.write ",\"to\":"
+               graph.nodes.id_of(to).serialize_to(v)
+               v.stream.write "}"
        end
 end