/*
Authors: Alexander Elyasov, Arie Middelkoop, Wishnu Prasetya
Copyright 2011 Utrecht University
The use of this sofware is free under the Modified BSD License.
*/
package UU.Serialization {
import UU.Logging.*;
import UU.ISerialization.* ;
import flash.utils.* ;
public class DefaultSerializer extends SerializerBase {
// a log stream to write the log messages to:
private static var log : LogStream = LoggingConf.getLogStream() ;
private var counter : int;
// Used to keep track of whether we are in paragraph or not.
// Paragraph is a formatting concept.
private var inParagraph : Boolean ;
public function DefaultSerializer() {
super();
}
override protected function actualReset() : void {
counter = 0;
}
// Question: why do we pass the type explicitly? I think I can ask AS what the runtime
// type of an object is...
override protected function actualBeginObject(obj : Object, objType : String) : Object {
counter ++;
var uid : int = counter;
inParagraph = false ;
log.write("%P") ;
return uid; // unique id for this object
}
override protected function actualEndObject() : void {
closeParagraph() ;
// Printing UID here is not good for hashing/compression; we drop it.
// Unlike sectioning a function, sectioning an object should be less
// risky (in the sense that the sections will be well balanced).
// log.write("%>S \"O:" + currentToken + ":" + currentObjectType + "\"");
// + currentObjectType()
//
log.write("%>S \"O:" + currentObjectType + "\"");
}
private function openParagraph() : void {
inParagraph = true ;
log.write("%
P") ; } private function writeSentence(s : String) : void { if (!inParagraph) openParagraph() ; log.write("%%{ " + s + " %%}") ; } // TODO: this still doesn't put an entire array on the memo list; so // if pointer to the same array occur, it will be serialized 2x. // NOTE: or maybe it is good as it is... // override protected function storeFieldUnSer(name : QName, val : Object) : void { if (val is Array) { writeSentence(name + "=>") ; closeParagraph() ; serializeArray(val as Array) ; return ; } var vt : String = formatPrimitive(val) ; if (vt != null) { writeSentence(name + "=" + vt) ; } // else val is an object that I don't know how to serialize: else writeSentence(name + "=??:" + getQualifiedClassName(val)) ; } private function formatPrimitive(val : *) : String { if (val is int) return (val + ":int") ; if (val is Number) return (val + ":Number") ; if (val is Boolean) return (val + ":Boolean") ; if (val is String) return ("\"" + val + "\":String") ; if (val === null) return ("null:") ; // (val is void) is rejected by AS3.. not sure if this way of checking // void-type will work. if (val === undefined) return ("undefined:void") ; // Any other primitive type?? return null ; } override protected function storeFieldRec(name : QName, val : Object, prev : *) : void { if (prev == undefined) { if (val == null) { // Is this branch actually possible? writeSentence(name + "=null:"); closeParagraph() ; return ; } // val is not null writeSentence(name + "=>") ; closeParagraph() ; if (val is Serializable) val.serializeSelf(this) ; // recursive call else { // then val is serializable by looking up in the // Serialization Delegates-dictionary var delegateSerializationFunction : Function = Delegates.getDelegate(val) ; delegateSerializationFunction(val,this) ; } } else { writeSentence(name + "=^"+ prev); // already serialized closeParagraph() ; } } // To serialize an arbitrary object. This implies that the object may not // implement Serializable. override public function serialize(obj : Object) : void { if (obj is Serializable) { obj.serializeSelf(this) ; return ; } if (obj is Array) { serializeArray(obj as Array) ; return ; } var delegateSerializationFunction : Function = Delegates.getDelegate(obj) ; if (delegateSerializationFunction != null) { delegateSerializationFunction(obj,this) ; return ; } // Else this it must be a primitive object: var vt : String = formatPrimitive(obj) ; log.write("%
P") ;
}
private function serializeArray(a : Array) : void {
log.write("%S \"O:Array\"") ;
}
}
}