# be deserialized to their original form using `JsonDeserializer`.
# * Use references when an object has already been serialized so to not duplicate it.
# * Support cycles in references.
- # * Preserve the Nit `Char` type as an object because it does not exist in JSON.
+ # * Preserve the Nit `Char` and `Byte` types as special objects.
# * The generated JSON is standard and can be read by non-Nit programs.
# However, some Nit types are not represented by the simplest possible JSON representation.
# With the added metadata, it can be complex to read.
end
end
+redef class Byte
+ 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
+end
+
redef class CString
redef fun accept_json_serializer(v) do to_s.accept_json_serializer(v)
end
redef class SimpleCollection[E]
redef fun accept_json_serializer(v)
do
- # Register as pseudo object
- if not v.plain_json then
+ 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.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
- else
- serialize_to_pure_json v
- end
- if not v.plain_json then
v.indent_level -= 1
v.new_line_and_indent
v.stream.write "\}"