Merge: Misc AST
[nit.git] / lib / json / static.nit
index 55b5a1e..12dbc7c 100644 (file)
@@ -30,18 +30,38 @@ private import json_lexer
 # Something that can be translated to JSON.
 interface Jsonable
        # Encode `self` in JSON.
+       #
+       # SEE: `append_json`
        fun to_json: String is abstract
+
+       # Use `append_json` to implement `to_json`.
+       #
+       # Therefore, one that redefine `append_json` may use the following
+       # redefinition to link `to_json` and `append_json`:
+       #
+       # ~~~nitish
+       # redef fun to_json do return to_json_by_append
+       # ~~~
+       #
+       # Note: This is not the default implementation of `to_json` in order to
+       # avoid cyclic references between `append_json` and `to_json` when none are
+       # implemented.
+       protected fun to_json_by_append: String do
+               var buffer = new RopeBuffer
+               append_json(buffer)
+               return buffer.write_to_string
+       end
+
+       # Append the JSON representation of `self` to the specified buffer.
+       #
+       # SEE: `to_json`
+       fun append_json(buffer: Buffer) do buffer.append(to_json)
 end
 
 redef class Text
        super Jsonable
 
-       # Encode `self` in JSON.
-       #
-       #     assert "\t\"http://example.com\"\r\n\0\\".to_json ==
-       #               "\"\\t\\\"http:\\/\\/example.com\\\"\\r\\n\\u0000\\\\\""
-       redef fun to_json do
-               var buffer = new FlatBuffer
+       redef fun append_json(buffer) do
                buffer.add '\"'
                for i in [0..self.length[ do
                        var char = self[i]
@@ -72,9 +92,14 @@ redef class Text
                        end
                end
                buffer.add '\"'
-               return buffer.write_to_string
        end
 
+       # Encode `self` in JSON.
+       #
+       #     assert "\t\"http://example.com\"\r\n\0\\".to_json ==
+       #               "\"\\t\\\"http:\\/\\/example.com\\\"\\r\\n\\u0000\\\\\""
+       redef fun to_json do return to_json_by_append
+
        # Parse `self` as JSON.
        #
        # If `self` is not a valid JSON document or contains an unsupported escape
@@ -176,16 +201,7 @@ interface JsonMapRead[K: String, V: nullable Jsonable]
        super MapRead[K, V]
        super Jsonable
 
-       # Encode `self` in JSON.
-       #
-       #     var obj = new JsonObject
-       #     obj["foo"] = "bar"
-       #     assert obj.to_json == "\{\"foo\":\"bar\"\}"
-       #     obj = new JsonObject
-       #     obj["baz"] = null
-       #     assert obj.to_json == "\{\"baz\":null\}"
-       redef fun to_json do
-               var buffer = new FlatBuffer
+       redef fun append_json(buffer) do
                buffer.append "\{"
                var it = iterator
                if it.is_ok then
@@ -197,9 +213,18 @@ interface JsonMapRead[K: String, V: nullable Jsonable]
                end
                it.finish
                buffer.append "\}"
-               return buffer.write_to_string
        end
 
+       # Encode `self` in JSON.
+       #
+       #     var obj = new JsonObject
+       #     obj["foo"] = "bar"
+       #     assert obj.to_json == "\{\"foo\":\"bar\"\}"
+       #     obj = new JsonObject
+       #     obj["baz"] = null
+       #     assert obj.to_json == "\{\"baz\":null\}"
+       redef fun to_json do return to_json_by_append
+
        private fun append_json_entry(iterator: MapIterator[String, nullable Jsonable],
                        buffer: Buffer) do
                buffer.append iterator.key.to_json
@@ -220,16 +245,7 @@ class JsonSequenceRead[E: nullable Jsonable]
        super Jsonable
        super SequenceRead[E]
 
-       # Encode `self` in JSON.
-       #
-       #     var arr = new JsonArray.with_items("foo", null)
-       #     assert arr.to_json == "[\"foo\",null]"
-       #     arr.pop
-       #     assert arr.to_json =="[\"foo\"]"
-       #     arr.pop
-       #     assert arr.to_json =="[]"
-       redef fun to_json do
-               var buffer = new FlatBuffer
+       redef fun append_json(buffer) do
                buffer.append "["
                var it = iterator
                if it.is_ok then
@@ -241,9 +257,18 @@ class JsonSequenceRead[E: nullable Jsonable]
                end
                it.finish
                buffer.append "]"
-               return buffer.write_to_string
        end
 
+       # Encode `self` in JSON.
+       #
+       #     var arr = new JsonArray.with_items("foo", null)
+       #     assert arr.to_json == "[\"foo\",null]"
+       #     arr.pop
+       #     assert arr.to_json =="[\"foo\"]"
+       #     arr.pop
+       #     assert arr.to_json =="[]"
+       redef fun to_json do return to_json_by_append
+
        private fun append_json_entry(iterator: Iterator[nullable Jsonable],
                        buffer: Buffer) do
                buffer.append_json_of(iterator.item)
@@ -298,7 +323,8 @@ end
 # Redef parser
 
 redef class Nvalue
-       fun to_nit_object: nullable Jsonable is abstract
+       # The represented value.
+       private fun to_nit_object: nullable Jsonable is abstract
 end
 
 redef class Nvalue_number
@@ -327,7 +353,8 @@ redef class Nvalue_null
 end
 
 redef class Nstring
-       fun to_nit_string: String do
+       # The represented string.
+       private fun to_nit_string: String do
                var res = new FlatBuffer
                var i = 1
                while i < text.length - 1 do
@@ -377,7 +404,8 @@ redef class Nvalue_object
 end
 
 redef class Nmembers
-       fun pairs: Array[Npair] is abstract
+       # All the key-value pairs.
+       private fun pairs: Array[Npair] is abstract
 end
 
 redef class Nmembers_tail
@@ -394,8 +422,11 @@ redef class Nmembers_head
 end
 
 redef class Npair
-       fun name: String do return n_string.to_nit_string
-       fun value: nullable Jsonable do return n_value.to_nit_object
+       # The represented key.
+       private fun name: String do return n_string.to_nit_string
+
+       # The represented value.
+       private fun value: nullable Jsonable do return n_value.to_nit_object
 end
 
 redef class Nvalue_array
@@ -412,7 +443,8 @@ redef class Nvalue_array
 end
 
 redef class Nelements
-       fun items: Array[Nvalue] is abstract
+       # All the items.
+       private fun items: Array[Nvalue] is abstract
 end
 
 redef class Nelements_tail