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.

Property definitions

json :: serialization_write $ Serializable :: accept_json_serializer
	# 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

json :: serialization_write $ Bool :: accept_json_serializer
	redef fun accept_json_serializer(v) do v.stream.write to_s
lib/json/serialization_write.nit:273,2--59

json :: serialization_write $ CString :: accept_json_serializer
	redef fun accept_json_serializer(v) do to_s.accept_json_serializer(v)
lib/json/serialization_write.nit:303,2--70

json :: serialization_write $ Float :: accept_json_serializer
	redef fun accept_json_serializer(v) do v.stream.write to_s
lib/json/serialization_write.nit:269,2--59

json :: serialization_write $ Int :: accept_json_serializer
	redef fun accept_json_serializer(v) do v.stream.write to_s
lib/json/serialization_write.nit:265,2--59

json :: serialization_write $ Text :: accept_json_serializer
	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