From 5a6cc0d88294d88ca08df1ce531409a425ea6572 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sat, 1 Feb 2014 13:49:13 -0500 Subject: [PATCH] lib: intro the abstract serialization module MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/serialization.nit | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 lib/serialization.nit diff --git a/lib/serialization.nit b/lib/serialization.nit new file mode 100644 index 0000000..cd80d3d --- /dev/null +++ b/lib/serialization.nit @@ -0,0 +1,71 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2014 Alexis Laferrière +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Offers services to serialize a Nit objects to different persistent formats +module serialization + +# Abstract serialization service to be sub-classed by specialized services. +interface Serializer + # Main method of this service, serialize the `object` + fun serialize(object: nullable Serializable) is abstract + + # Serialize an object as a "possible" reference, depending of the service + fun serialize_reference(object: Serializable) is abstract + + # Serialize an attribute, used by `Serializable::core_serialize_to` + fun serialize_attribute(name: String, value: nullable Object) + do + if value isa Serializable then + value.serialize_to_or_delay(self) + else if value == null then + serialize value + else + warn("argument {value.class_name}::{name} is not serializable.") + end + end + + # Warn of problems and potential errors (such as if an attribute + # is not serializable) + fun warn(msg: String) do print "Serialization warning: {msg}" +end + +# Instances of this class can be passed to `Serializer::serialize` +interface Serializable + # Full or true serialization + fun serialize_to(v: Serializer) do v.serialize(self) + + # Body of the serialization of this class + # Can be redefed in sub classes and refinements + fun core_serialize_to(v: Serializer) do end + + # Whether full serialization (calls `serialize_to`) or place only references + fun serialize_to_or_delay(v: Serializer) do v.serialize_reference(self) +end + +# Instances of this class are not delayed and instead serialized immediately +# This applies mainly to `universal` types +interface DirectSerializable + super Serializable + + redef fun serialize_to_or_delay(v) do serialize_to(v) +end + +redef class Bool super DirectSerializable end +redef class Char super DirectSerializable end +redef class Int super DirectSerializable end +redef class Float super DirectSerializable end +redef class NativeString super DirectSerializable end +redef class String super DirectSerializable end -- 1.7.9.5