Provide boiler plate implementation of all object serializable to json.
id
is used as a primary key for find_by_id
.
Subclassing RepoObject makes it easy to create a serializable class:
import popcorn::pop_repos
class Album
super RepoObject
serialize
var title: String
var price: Float
end
Do not forget the serialize
annotation else the fields will not be serialized.
It is also possible to redefine the id
primary key to use your own:
import popcorn::pop_repos
class Order
super RepoObject
serialize
redef var id = "order-{get_time}"
# ...
end
popcorn $ RepoObject :: SELF
Type of this instance, automatically specialized in every classpopcorn $ RepoObject :: core_serialize_to
Actual serialization ofself
to serializer
popcorn $ RepoObject :: from_deserializer
Create an instance of this class from thedeserializer
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Object :: defaultinit
popcorn :: RepoObject :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
Serializer::serialize
# Base serializable entity that can go into a JsonRepository
#
# Provide boiler plate implementation of all object serializable to json.
#
# `id` is used as a primary key for `find_by_id`.
#
# Subclassing RepoObject makes it easy to create a serializable class:
# ~~~
# import popcorn::pop_repos
#
# class Album
# super RepoObject
# serialize
#
# var title: String
# var price: Float
# end
# ~~~
#
# Do not forget the `serialize` annotation else the fields will not be serialized.
#
# It is also possible to redefine the `id` primary key to use your own:
# ~~~
# import popcorn::pop_repos
#
# class Order
# super RepoObject
# serialize
#
# redef var id = "order-{get_time}"
#
# # ...
#
# end
# ~~~
abstract class RepoObject
serialize
# `self` unique id.
#
# This attribute is serialized under the key `_id` to be used
# as primary key by MongoDb
var id: String = (new MongoObjectId).id is writable, serialize_as "_id"
# Base object comparison on ID
#
# Because multiple deserialization can exists of the same instance,
# we use the ID to determine if two object are the same.
redef fun ==(o) do return o isa SELF and id == o.id
redef fun hash do return id.hash
redef fun to_s do return id
end
lib/popcorn/pop_repos.nit:375,1--427,3