This method can be refined to customize the serialization by either
writing pure JSON directly on the stream v.stream or
by using other services of JsonSerializer.
Most of the time, it is preferable to refine the method core_serialize_to
which is used by all the serialization engines, not just JSON.
# Refinable service to customize the serialization of this class to JSON
#
# This method can be refined to customize the serialization by either
# writing pure JSON directly on the stream `v.stream` or
# by using other services of `JsonSerializer`.
#
# Most of the time, it is preferable to refine the method `core_serialize_to`
# which is used by all the serialization engines, not just JSON.
protected fun accept_json_serializer(v: JsonSerializer)
do
v.stream.write "\{"
v.indent_level += 1
if not v.plain_json then
var id = v.cache.new_id_for(self)
v.new_line_and_indent
v.stream.write "\"__kind\": \"obj\", \"__id\": "
v.stream.write id.to_s
v.stream.write ", \"__class\": \""
v.stream.write class_name
v.stream.write "\""
end
v.serialize_core(self)
v.indent_level -= 1
v.new_line_and_indent
v.stream.write "\}"
end
lib/json/serialization_write.nit:235,2--261,4
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
lib/neo4j/graph/json_graph_store.nit:194,2--200,4
redef fun accept_json_serializer(v)
do
# Register as pseudo object
var id = v.cache.new_id_for(self)
v.stream.write "\{"
v.indent_level += 1
if v.plain_json then
var first = true
for key, val in self do
if not first then
v.stream.write ","
else first = false
v.new_line_and_indent
var k = key or else "null"
k.to_s.accept_json_serializer v
v.stream.write ":"
if v.pretty_json then v.stream.write " "
if not v.try_to_serialize(val) then
assert val != null # null would have been serialized
v.warn("element of type {val.class_name} is not serializable.")
v.stream.write "null"
end
end
else
v.new_line_and_indent
v.stream.write """"__kind": "obj", "__id": """
v.stream.write id.to_s
v.stream.write """, "__class": """"
v.stream.write class_name
v.stream.write """", "__length": """
v.stream.write length.to_s
v.stream.write ","
v.new_line_and_indent
v.stream.write """"__keys": """
keys.serialize_to_pure_json v
v.stream.write ","
v.new_line_and_indent
v.stream.write """"__values": """
values.serialize_to_pure_json v
core_serialize_to v
end
v.indent_level -= 1
v.new_line_and_indent
v.stream.write "\}"
end
lib/json/serialization_write.nit:361,2--412,4
redef fun accept_json_serializer(v) do
v.stream.write "\{\"labels\":["
var i = labels.iterator
if i.is_ok then
i.item.serialize_to v
i.next
for lab in i do
v.stream.write ","
lab.serialize_to v
end
end
v.stream.write "],\"properties\":"
properties.serialize_to v
v.stream.write "}"
end
lib/neo4j/graph/json_graph_store.nit:266,2--280,4
redef fun accept_json_serializer(v)
do
if v.plain_json then
serialize_to_pure_json v
else
# Register as pseudo object
var id = v.cache.new_id_for(self)
v.stream.write """{"""
v.indent_level += 1
v.new_line_and_indent
v.stream.write """"__kind": "obj", "__id": """
v.stream.write id.to_s
v.stream.write """, "__class": """"
v.stream.write class_name
v.stream.write """","""
v.new_line_and_indent
v.stream.write """"__items": """
serialize_to_pure_json v
core_serialize_to v
v.indent_level -= 1
v.new_line_and_indent
v.stream.write "\}"
end
end
lib/json/serialization_write.nit:331,2--357,4
redef fun accept_json_serializer(v) do to_s.accept_json_serializer(v)
lib/json/serialization_write.nit:303,2--70
redef fun accept_json_serializer(v)
do
if v.plain_json then
to_s.accept_json_serializer v
else
v.stream.write "\{\"__kind\": \"char\", \"__val\": "
to_s.accept_json_serializer v
v.stream.write "\}"
end
end
lib/json/serialization_write.nit:277,2--286,4
redef fun accept_json_serializer(v)
do
if v.plain_json then
to_i.accept_json_serializer v
else
v.stream.write "\{\"__kind\": \"byte\", \"__val\": "
to_i.accept_json_serializer v
v.stream.write "\}"
end
end
lib/json/serialization_write.nit:290,2--299,4
redef fun accept_json_serializer(v)
do
v.stream.write "\""
var start_i = 0
var escaped = null
for i in [0 .. self.length[ do
var char = self[i]
if char == '\\' then
escaped = "\\\\"
else if char == '\"' then
escaped = "\\\""
else if char < ' ' then
if char == '\n' then
escaped = "\\n"
else if char == '\r' then
escaped = "\\r"
else if char == '\t' then
escaped = "\\t"
else
escaped = char.escape_to_utf16
end
end
if escaped != null then
# Write open non-escaped string
if start_i <= i then
v.stream.write substring(start_i, i-start_i)
end
# Write escaped character
v.stream.write escaped
escaped = null
start_i = i+1
end
end
# Write remaining non-escaped string
if start_i < length then
if start_i == 0 then
v.stream.write self
else
v.stream.write substring(start_i, length-start_i)
end
end
v.stream.write "\""
end
lib/json/serialization_write.nit:149,2--196,4