mnit_moles: add git version to moles version
[nit.git] / lib / serialization.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Offers services to serialize a Nit objects to different persistent formats
18 module serialization
19
20 # Abstract serialization service to be sub-classed by specialized services.
21 interface Serializer
22 # Main method of this service, serialize the `object`
23 fun serialize(object: nullable Serializable) is abstract
24
25 # Serialize an object as a "possible" reference, depending of the service
26 fun serialize_reference(object: Serializable) is abstract
27
28 # Serialize an attribute, used by `Serializable::core_serialize_to`
29 fun serialize_attribute(name: String, value: nullable Object)
30 do
31 if not try_to_serialize(value) then
32 warn("argument {value.class_name}::{name} is not serializable.")
33 end
34 end
35
36 # Serialize `value` is possie, i.e. it is `Serializable` or `null`
37 fun try_to_serialize(value: nullable Object): Bool
38 do
39 if value isa Serializable then
40 value.serialize_to_or_delay(self)
41 else if value == null then
42 serialize value
43 else return false
44 return true
45 end
46
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}"
50 end
51
52 # Abstract deserialization service
53 #
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
58
59 # Internal method to be implemented by sub-classes
60 fun deserialize_attribute(name: String): nullable Object is abstract
61
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
65
66 # Mainly generated method to return the next instance of the givent
67 # class by name
68 fun deserialize_class(class_name: String): Object do
69 print "Error: doesn't know how to deserialize class \"{class_name}\""
70 abort
71 end
72 end
73
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)
78
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
82
83 # Whether full serialization (calls `serialize_to`) or place only references
84 fun serialize_to_or_delay(v: Serializer) do v.serialize_reference(self)
85 end
86
87 # Instances of this class are not delayed and instead serialized immediately
88 # This applies mainly to `universal` types
89 interface DirectSerializable
90 super Serializable
91
92 redef fun serialize_to_or_delay(v) do serialize_to(v)
93 end
94
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