1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Offers services to serialize a Nit objects to different persistent formats
20 # Abstract serialization service to be sub-classed by specialized services.
22 # Main method of this service, serialize the `object`
23 fun serialize
(object
: nullable Serializable) is abstract
25 # Serialize an object as a "possible" reference, depending of the service
26 fun serialize_reference
(object
: Serializable) is abstract
28 # Serialize an attribute, used by `Serializable::core_serialize_to`
29 fun serialize_attribute
(name
: String, value
: nullable Object)
31 if not try_to_serialize
(value
) then
32 warn
("argument {value.class_name}::{name} is not serializable.")
36 # Serialize `value` is possie, i.e. it is `Serializable` or `null`
37 fun try_to_serialize
(value
: nullable Object): Bool
39 if value
isa Serializable then
40 value
.serialize_to_or_delay
(self)
41 else if value
== null then
47 # Warn of problems and potential errors (such as if an attribute
48 # is not serializable)
49 fun warn
(msg
: String) do print
"Serialization warning: {msg}"
52 # Abstract deserialization service
54 # After initialization of one of its sub-classes, call `deserialize`
55 interface Deserializer
56 # Main method of this class, returns a Nit object
57 fun deserialize
: nullable Object is abstract
59 # Internal method to be implemented by sub-classes
60 fun deserialize_attribute
(name
: String): nullable Object is abstract
62 # Internal method called by objects in creation,
63 # to be implemented by sub-classes
64 fun notify_of_creation
(new_object
: Object) is abstract
66 # Mainly generated method to return the next instance of the givent
68 fun deserialize_class
(class_name
: String): Object do
69 print
"Error: doesn't know how to deserialize class \"{class_name}\
""
74 # Instances of this class can be passed to `Serializer::serialize`
75 interface Serializable
76 # Full or true serialization
77 fun serialize_to
(v
: Serializer) do v
.serialize
(self)
79 # Body of the serialization of this class
80 # Can be redefed in sub classes and refinements
81 fun core_serialize_to
(v
: Serializer) do end
83 # Whether full serialization (calls `serialize_to`) or place only references
84 fun serialize_to_or_delay
(v
: Serializer) do v
.serialize_reference
(self)
87 # Instances of this class are not delayed and instead serialized immediately
88 # This applies mainly to `universal` types
89 interface DirectSerializable
92 redef fun serialize_to_or_delay
(v
) do serialize_to
(v
)
95 redef class Bool super DirectSerializable end
96 redef class Char super DirectSerializable end
97 redef class Int super DirectSerializable end
98 redef class Float super DirectSerializable end
99 redef class NativeString super DirectSerializable end
100 redef class String super DirectSerializable end
101 redef class Array[E
] super Serializable end