Object serialization

Object serialization
You are encouraged to solve this task according to the task description, using any language you may know.

Create a set of data types based upon inheritance. Each data type or class should have a print command that displays the contents of an instance of that class to standard output. Create instances of each class in your inheritance hierarchy and display them to standard output. Write each of the objects to a file named objects.dat in binary form using serialization or marshalling. Read the file objects.dat and print the contents of each serialized object.

The final section of code creates objects of the three message types and performs the printing, writing, and reading. The Ada attributes 'Class'Output serialize the object and write it to the specified stream. The 'Class'Input attributes call a function automatically provided by the compiler which reads from the specified stream file and returns the object read. The Display procedure takes an object in the inheritance tree rooted at Message and dispatches the correct print procedure.

with Messages; use Messages;with Ada.Streams.Stream_Io; use Ada.Streams.Stream_Io;with Ada.Calendar; use Ada.Calendar;with Ada.Text_Io;

flexible length arrays (including strings), and tagged-union
types are problematic as the lengths of the arrays, and the tag of the
union is not stored in the file. Sometimes a format can be manually created to handle
these lengths and tags. Also note that ALGOL 68 is strongly typed and
the type (mode) of the objects not stored, but compiled into the
code itself.

The 'exits' of the surgeon (so called because it cuts and joins object subgraphs) specify the points at which serialization should stop, instead replacing references to the objects with the specified names. On unserialization, the names are looked up and replaced with the corresponding objects. (The surgeon provides default exits for such things as false, true, null, and the list constructor.)

Our classes will be 'struct' objects, with custom #:tostring procedures, as required. The instances are saved/restored to/from local storage. Serialization is performed by JSONifying the objects. References between instances of struct's are kept in the process. Auto-references and circular references are correctly maintained since EchoLisp version 2.11.

The serialize vocabulary provides words for serializing and deserializing any Factor object other than continuations. This example demonstrates that objects may be serialized individually, one at a time. In practice, it's probably easier to serialize a collection of objects.

Go has a few choices for serialization. The method shown here is the native method, which is compact and type-aware.

A note on object oriented stuff, the object hierarchy required by the task description is implemented here with embedded structs. The polymorphism required by the task description is handled nicely with an interface. The functional polymorphism is orthogonal to the object hierarchy, not conflated with it.

Note also that J does not attempt to distinguish, at the language level, between an object reference and something that looks like an object reference but is not. This must be done at the application level, which in turn can create a variety of opportunities and/or burdens for the program designer.

(And this is more than adequate for the use case J's object system was designed for, which is to provide a mechanism where independent programmers can work on different modules of a program with well defined interfaces. Of course, if you try to make it complicated, or try to design something with incomprehensible interfaces, that will create problems.)

// classes must implement java.io.Serializable in order to be serializableclassEntityimplementsSerializable{// it is recommended to hard-code serialVersionUID so changes to class// will not invalidate previously serialized objectsstaticfinallong serialVersionUID = 3504465751164822571L;String name ="Entity";publicString toString(){return name;}}

Object readObject1 = in.readObject();// read two objects from "in"Object readObject2 = in.readObject();// you may want to cast them to the appropriate types in.close();System.out.println("Deserialized...");

About Cocoa, I can't test it, but I've used Apple's documentation to learn how to do it (see
here; serializing or marshalling is rather known in Obj-C world as archiving).

There exists also a way of serializing without the GNUstep/Cocoa framework, using the runtime of Objective-C (so it could be slightly implementation dependent, see Serialization on Wikipedia). (I will work on it and will put here a full working example compatible with the task).

// let us create a fantasy animal Animal *anAnimal =[[Animal alloc] initWithName:@"Eptohippos" andLegs:7];// for some reason an Eptohippos is not an horse with 7 legs,// and it is not a mammal, of course...

Objects which contain methods are difficult to serialize because it will want to serialize those methods too, but functions usually cannot be serialized. Instead, here we perform the task on non-object datatypes, with an outside function to print them.

# Object Serialization in Python# serialization in python is accomplished via the Pickle module.# Alternatively, one can use the cPickle module if speed is the key,# everything else in this example remains the same.

class Moonling < Beingdef to_s4Being"My name is Janneke Maan, and apparently some Earthlings will pay me a visit."endend

diverseCollection=[]diverseCollection <<(marsian=Being.new("I come from Mars and like playing hide and seek."))diverseCollection <<(me=Mammal.new(:human))diverseCollection <<(nemo=Fish.new(0.99))diverseCollection <<(jannakeMaan=Moonling.new)

zkl can serialize a "root class" (usually a file but any static (ie parentless) class) to bit bucket (such as File). This is done via reflection. The state of the class(es) are not stored so no image write/read. In the "normal" course of events, this isn't used, programs are compiled on the fly and run. However, there is extensive support to pre-compile and package files into applications or just pre-compile for faster load times (or to create an image that can be compiled into C code (which is done to package the compiler with the VM). When the compiler writes a class or app to a file, the preferred extension is ".zsc", which is what the Import method looks for. But no matter, we have ways ...