Removed sockets from 'debugger.nit' to put them in 'debugger_socket.nit
[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 abort
69 end
70
71 # Instances of this class can be passed to `Serializer::serialize`
72 interface Serializable
73 # Full or true serialization
74 fun serialize_to(v: Serializer) do v.serialize(self)
75
76 # Body of the serialization of this class
77 # Can be redefed in sub classes and refinements
78 fun core_serialize_to(v: Serializer) do end
79
80 # Whether full serialization (calls `serialize_to`) or place only references
81 fun serialize_to_or_delay(v: Serializer) do v.serialize_reference(self)
82 end
83
84 # Instances of this class are not delayed and instead serialized immediately
85 # This applies mainly to `universal` types
86 interface DirectSerializable
87 super Serializable
88
89 redef fun serialize_to_or_delay(v) do serialize_to(v)
90 end
91
92 redef class Bool super DirectSerializable end
93 redef class Char super DirectSerializable end
94 redef class Int super DirectSerializable end
95 redef class Float super DirectSerializable end
96 redef class NativeString super DirectSerializable end
97 redef class String super DirectSerializable end
98 redef class Array[E] super Serializable end