s.im.pl serialization tutorial
objective-c polymorphic

Polymorphic Collections:
Introduction

In this tutorial we will show how to serialize/de-serialize polymorphic collections in objective-c. In the getting started objective-c tutorial, we generated a set of header and implementation files from java annotated classes. We will use the same procedure for polymorphic collections in java and generate header files from the annotated java GameData tutorial.

To view this tutorial you will need to download the entire ecologylabFundamental project. Access to the ecologylab fundamental project source is available through anonymous SVN access (user: anonymous, password: anonymous).

The source for this tutorial is the ecologylab fundamental Objective-C project located under /ecologylabFundamentalObjC/trunk/. The code specific to this tutorial is in the Classes/Library/Rouge/ group in the Xcode project.

Objective-C Header Files

Following are the generated header files from the Java Tutorial. For each class in Java we have generated an equivalent objective-c class.

Below the GameData objective-c class generated from Java version it also extends ElementeState class

@interface GameData : ElementState

{

long timestamp;

int cycRem;

bool paused;

bool loaded;

bool over;

bool running;

bool won;

NSMutableArray *threats;

double score;

}


@property (nonatomic,readwrite) long timestamp;

@property (nonatomic,readwrite) int cycRem;

@property (nonatomic,readwrite) bool paused;

@property (nonatomic,readwrite) bool loaded;

@property (nonatomic,readwrite) bool over;

@property (nonatomic,readwrite) bool running;

@property (nonatomic,readwrite) bool won;

@property (nonatomic,readwrite, retain) NSMutableArray *threats;

@property (nonatomic,readwrite) double score;


Below is the generated Entity class from Java. It extends ElementState and defines its attributes

@interface Entity : ElementState

{

NSString *m_id;

bool online;

bool m_in;

bool safe;

int ord;

}


@property (nonatomic,readwrite, retain) NSString *m_id;

@property (nonatomic,readwrite) bool online;

@property (nonatomic,readwrite) bool m_in;

@property (nonatomic,readwrite) bool safe;

@property (nonatomic,readwrite) int ord;


Below we have the Threat class which is also similar to the Java class. Note that it does not define any of its attributes and is only acting as a dummy base class. The generated objective-c version does not define any attributes which are not bound to any of its XML representation. Developers can add their own variables for programmatic use that are not bound XML representation.

@interface Threat : Entity

{

}

Below we have a sample SingleSeekerThreat class which extends the Threat class. It also defines its own attribute targetOrd which is serializable. Note that as in the Java version we do not have any annotations defined on the attributes, since objective-c does not support annotations. The translation semantics are defined in "gamedata_translationscope.xml"

@interface SingleSeekerThreat : Threat

{

int targetOrd;

}


@property (nonatomic,readwrite) int targetOrd;


Creating Translation Scope

We create the below translation as we did in the previous examples by simply reading the "gamedata_translationscope.xml" file. This will populate the internal data strucutres of the s.im.pl serialization to drive the serialzation process.

/*

* Create Translation Scope from XML file

*/

+ (TranslationScope *) getTranslationScope {

if(translationScope == nil){

NSString *path = [[[NSBundle mainBundle] resourcePath]

stringByAppendingPathComponent: @"gamedata_translationScope.xml"];

translationScope = [[TranslationScope alloc] initWithXMLFilePath: path];

}

return translationScope;

}

Serialization & De-Serialization

Just as we did in our previous example we will again read a sample XML file containing game data. First we will deserialize it and then serialize it back again.

NSString *gameDataInput = [[[NSBundle mainBundle] resourcePath]

stringByAppendingPathComponent: @"GameData.xml"];



GameData *testGameData = (GameData *) [[GameData getTranslationScope] deserialize: gameDataInput ];

NSMutableString *gamedataOutput = [NSMutableString string];

//Serializing the data back to XML into an ouptut MutableString buffer.

[testGameData serialize:gamedataOutput];

//Throwing output to console

NSLog(gamedataOutput);

GameData XML File

Below is the game data XML file we have used for this example. Notice that the tag names "nt", "ot" and "pt" are from the class definition @xml_tag annotation. The threat collection is polymorphic type and can contain different types of Threat subclass objects.

<game_data timestamp="1234399958508" cyc_rem="8078" loaded="true"
	running="true" score="-28.066665835678577">
	<threats>
		<nt m_id="_t0" />
		<nt m_id="_t1" ord="1" />
		<nt m_id="_t2" ord="2" />
		<nt m_id="_t3" ord="3" />
		<nt m_id="_t4" ord="4" />
		<nt m_id="_t5" ord="5" />
		<nt m_id="_t6" ord="6" />
		<nt m_id="_t7" ord="7" />
		<nt m_id="_t8" ord="8" />
		<nt m_id="_t9" ord="9" />
		<nt m_id="_t10" ord="10" />
		<nt m_id="_t11" ord="11" />
		<nt m_id="_t12" ord="12" />
		<ot m_id="_t13" ord="13" />
		<ot m_id="_t14" ord="14" />
		<ot m_id="_t15" ord="15" />
		<pt m_id="_t16" ord="16" />
		<pt m_id="_t17" ord="17" />
		<pt m_id="_t18" ord="18" />
		<pt m_id="_t19" ord="19" />
	</threats>
</game_data>
End notes

In this tutorial presented how to translate objective-c polymorphic types. We generated class definitions from Java polymorphic tutorial and used the same XML file to serialize/de-serialize in objective-c.







an interface ecology lab production