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 MsgPackSerializer
.
Most of the time, it is better to refine the method core_serialize_to
which is used by all the serialization engines, not just MessagePack.
# Hook to customize the serialization of this class to MessagePack
#
# 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 `MsgPackSerializer`.
#
# Most of the time, it is better to refine the method `core_serialize_to`
# which is used by all the serialization engines, not just MessagePack.
protected fun accept_msgpack_serializer(v: MsgPackSerializer)
do
# Count the number of attributes
var attribute_counter = new AttributeCounter
accept_msgpack_attribute_counter attribute_counter
var n_attributes = attribute_counter.count
if not v.plain_msgpack then
var n_meta_items = 2
if n_attributes > 0 then n_meta_items += 1
n_meta_items += msgpack_extra_array_items # obj+id, class_name, attributes
# Metadata
var id = v.cache.new_id_for(self)
v.stream.write_msgpack_array n_meta_items
v.stream.write_msgpack_ext(v.ext_typ_obj, id.to_bytes)
v.serialize_meta_string class_name
if n_attributes > 0 then v.stream.write_msgpack_map n_attributes
else
v.stream.write_msgpack_map n_attributes
end
v.serialize_core self
end
lib/msgpack/serialization_write.nit:189,2--223,4
redef fun accept_msgpack_serializer(v) do v.stream.write_msgpack_ext(typ, data)
lib/msgpack/serialization_write.nit:241,2--80
redef fun accept_msgpack_serializer(v) do v.stream.write_msgpack_bool self
lib/msgpack/serialization_write.nit:257,2--75
redef fun accept_msgpack_serializer(v)
do
if not v.plain_msgpack then
# Add metadata and other attributes
super
end
# Header
v.stream.write_msgpack_map keys.length
# Key / values, alternating
for key, val in self do
if not v.try_to_serialize(key) then
assert val != null # null would have been serialized
v.warn "element of type {val.class_name} is not serializable."
v.stream.write_msgpack_null
end
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_msgpack_null
end
end
end
lib/msgpack/serialization_write.nit:322,2--346,4
redef fun accept_msgpack_serializer(v)
do
if not v.plain_msgpack then
# Add metadata and other attributes
super
end
# Header
v.stream.write_msgpack_array length
# Items
for e in self do
if not v.try_to_serialize(e) then
assert e != null # null would have been serialized
v.warn "element of type {e.class_name} is not serializable."
v.stream.write_msgpack_null
end
end
end
lib/msgpack/serialization_write.nit:298,2--316,4
redef fun accept_msgpack_serializer(v) do to_s.accept_msgpack_serializer(v)
lib/msgpack/serialization_write.nit:294,2--76
redef fun accept_msgpack_serializer(v)
do
if v.plain_msgpack then
# Write as a string
v.stream.write_msgpack_fixstr to_s
else
# Write as ext
var bytes = to_s.to_bytes
v.stream.write_msgpack_ext(v.ext_typ_char, bytes)
end
end
lib/msgpack/serialization_write.nit:276,2--286,4
redef fun accept_msgpack_serializer(v)
do
if v.plain_msgpack then
# Write as a string
v.stream.write_msgpack_int to_i
else
# Write as ext
var bytes = new Bytes.with_capacity(1)
bytes.add self.to_i
v.stream.write_msgpack_ext(v.ext_typ_byte, bytes)
end
end
lib/msgpack/serialization_write.nit:261,2--272,4
redef fun accept_msgpack_serializer(v) do v.stream.write_msgpack_double self
lib/msgpack/serialization_write.nit:253,2--77
redef fun accept_msgpack_serializer(v) do v.stream.write_msgpack_int self
lib/msgpack/serialization_write.nit:249,2--74
redef fun accept_msgpack_serializer(v) do v.stream.write_msgpack_str self
lib/msgpack/serialization_write.nit:245,2--74
redef fun accept_msgpack_serializer(v) do v.stream.write_msgpack_bin self
lib/msgpack/serialization_write.nit:290,2--74