Talk:Serialization

From Wikipedia, the free encyclopedia

I am having problems serializing Objects using GNU Objective-c, your example shows a serialization using a single integer. So I modified the example I created an object Fraction, instantiated a fraction object and then serialize the object, using "@" instead of "i" for the read and write methods, for the receiver portion when I print the object is 0/0 but i am able to see the address that contains the serialize values. Could anyone help me solving this I have been putting a considerable amount of time in this and I cannot get it to work.

Below I included a copy of the modified code:

______________
sender.h

  1. import <objc/Object.h>
  2. import <time.h>
  3. import "fraction.h"
//@interface Sender : Object
@interface Sender : Fraction
{
//time_t timeNow;
Fraction *timeNow;
}
- ( id ) setTime;
// replace (time_t) time
- ( int )timenum;
- ( int )timeden;
- ( id ) send;
- ( id ) read: (TypedStream *) s;
- ( id ) write: (TypedStream *) s;
- ( void ) printS;
@end
______________
sender.m

  1. import "sender.h"

@implementation Sender;
- ( id ) setTime
{
timeNow =[Fraction new];//timeNow =time(NULL);
[timeNow num:5 den:7];
return self;
}

- ( int ) timenum
{ return [self num];
}
- ( int ) timeden
{ return [self den];
}
- ( id ) write: ( TypedStream * ) stream
{
// Write the superclass to the stream.
// It is done so we have the complete object hierarchy,
// no just the object itself.

[ super write:stream ];
// Write timeNow out to the stream.
// time_t is typedef for an integer.
// The second argument , the string "i", specifies the types to write
// as per the @encode directive.
// changed "i" by "@"
objc_write_types(stream,"@",&timeNow);
return self;
}
- (id) read: (TypedStream *)stream
{
// Reverse to write - reconstruct the superclass
[super read:stream];
// Reconstruct the instance variables from the stream
// changed "i" by "@"
objc_read_types(stream,"@" ,&timeNow);
return self;
}
- ( id ) send
{ // Convenience method to do the writing. Open stdoutas our byte stream
TypedStream *s = objc_open_typed_stream(stdout, OBJC_WRITEONLY);
// Write the object to the stream.
[self write:s];

// Finish up - close the stream.
objc_close_typed_stream(s);
}

//prints a fraction
- ( void ) printS
{
[timeNow printFR];
}
______________
receiver.h

// Serialization - Sender
  1. import <objc/Object.h>
  2. import <time.h>
  3. import "fraction.h"
  4. import "sender.h"
@interface Receiver : Sender
{
//time_t t;
Sender *t;
}
- ( id ) receive;
- ( void ) printRec;
@end
__________________________
receiver.m
  1. import "receiver.h"
@implementation Receiver;
- (id) receive
{
//Open stdin as our stream for reading.
TypedStream *s = objc_open_typed_stream(stdin, OBJC_READONLY);
//Allocate memory for, and instantiate the object from reading the stream.
t=[[Sender new] read:s];
objc_close_typed_stream(s);
}
- (void) printRec
{
int num = [t timenum];
int den = [t timeden];
//prints the address the object serialized;
fprintf(stderr, "received serialized %d\n", t);
fprintf(stderr, "received serialized %d/%d\n",num, den);
}
@end






any chance some one in the know could add someting about serializable objects in .NET (direct competition of Java?).

i would be much obliged.

jaspio

What about solutions in Perl (e.g. FreezeThaw) and/or using YAML (YAML)?


isn't the term "marshaling" (with one 'l') instead of "marshalling"? the version with the double-l is only a noun, while the former has verb forms. therefore, all references to "marshall" should really be "marshal". for example, "marshaling" or "unmarshaled".

Either is acceptable. Personally I like the double-l form better.

We could add something about cloning using serialization. I wrote a Java example here : http://www.krunch.be/vrac/cours/04-05-2cinf/poo/CloneWithSerialization.java


Writing out data structures in plain text ASCII is itself a serialization and any programming language that can handle text can do this. For example Java's toString() function is often used to serialize file formats rather than using the Serializable interface. The advantage of this primitive form of serialization is that it is simple. For example in C++ serialization is also implemented using streams. However streams have a lower level of data perspective. From an data objects perspective the end of file character is no important so implementing serialization while avoiding the pitfalls of streams is dangerous. However if serialization is implemented with C++ string then the string manages the memory buffer itself and the programmer is free to focus on just writing and reading the data structure. Or when nastly low level events occure they can be managed much better. In this case the primitive technology is better.

This kind of serialization I believe is a byte stream encoding. However it uses some universal character format such as ASCII so all kinds of programms and languages can understand it. For many file formats in applications space is not the issue, for example saving a users preferences in each file is often not that costly.

Also I belief Microsofts registary is a form of serialization. Qt a Gui framework also supports the registary concept accross multiple platforms.

Could someone please consider writing this perspective in the Serialization documentation. I am not confident about how to integrate this into the existing doc. As a programmer I came to this conclusion the hard way by writing with C++ streams, and then went back to the basic text files after much grief. So I am looking to separate serialization with low level implementation and managing text files is a valid way to do it.

[edit] Serialization using Qt

Can someone please give information on serialization using Trolltech's Qt (toolkit) library? The qt designer can save/restore the GUI in xml format, is there a tutorial how it is done? Thanks!