Using FileReference to save and load an object graph

How to use FileRefence to save and load an object graph

There are plenty of examples on the web about how to save and load text, but how about something a bit more real world ? Like a collection of complex objects ?

Here we have an ArrayColleciton of 2 Trades, t1 and t2. Each Trade object has one Settlement object. So if we serialise the arrayCollection, it should write to a file a total of 4 objects.

You can then read that file back in by pressing the “Load Text” button and see it load.
For the sake of completeness, the Trade and Settlement classes are below.
LoadSave.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
   xmlns:bonds="com.tl.bonds.*"
   creationComplete="init()">
<fx:Declarations>
<bonds:Settlement id="s1"
  amount="100"
  commodity="wheat"
  endDate="12 May 2010"
  startDate="13 May 2009"
  interest="0.45"
  unitType="Tons"/>
<bonds:TradeDetails id="t1"
settlement="{s1}"
tradeDate="11 May 2010"
nativeCurrency="EUR"/>
<bonds:Settlement id="s2"
  amount="200"
  commodity="cocoa"
  endDate="1 Nov 2008"
  startDate="1 Mar 2008"
  interest="0.34"
  unitType="Tons"/>
<bonds:TradeDetails id="t2"
settlement="{s2}"
tradeDate="11 May 2008"
nativeCurrency="SEK"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.net.registerClassAlias;
import mx.collections.ArrayCollection;
private var dataList:ArrayCollection = new ArrayCollection();
private function init() : void
{
registerClassAlias("com.tl.bonds.Settlement",com.tl.bonds.Settlement);
registerClassAlias("com.tl.bonds.TradeDetails",com.tl.bonds.TradeDetails);
dataList.addItem(t1);
dataList.addItem(t2);
}
private var fileReference:FileReference = new FileReference();
private function onSaveText():void
{
var tempCollection:ByteArray=new ByteArray();
tempCollection.writeObject(dataList);
tempCollection.position=0;

fileReference.save(tempCollection,"myText.txt");
trace("saved array collection");
}
private function onLoadText():void
{
var f:FileFilter = new FileFilter("Text","*.txt");
fileReference.browse([f]);
fileReference.addEventListener(Event.SELECT,onFileSelect);
fileReference.addEventListener(Event.COMPLETE,onFileComplete);
}
private function onFileSelect(event:Event):void
{
fileReference.load();
}
private function onFileComplete(event:Event):void
{
dataList = fileReference.data.readObject() as ArrayCollection;

trace("loaded array collection");
}
]]>
</fx:Script>
<s:Panel id="myPanel" width="400" height="450" title="Text Pad">
<s:HGroup>
<s:Button label="Save Text" click="onSaveText()"/>
<s:Button label="Load Text" click="onLoadText()"/>
</s:HGroup>
</s:Panel>
</s:Application>
The real work is done in the lines:

var tempCollection:ByteArray=new ByteArray();
tempCollection.writeObject(dataList);
tempCollection.position=0;
which is how we save an ArrayCollection to bytes.
Notice the use of:
registerClassAlias("com.tl.bonds.Settlement",com.tl.bonds.Settlement);
which is how the compiler knows what the structure of the Complex classes are for serialisation purposes.
Settlement.as
package com.tl.bonds
{
public class Settlement
{
public var startDate:String;
public var endDate:String;
public var interest:Number;
public var amount:int;
public var unitType:String;
public var commodity:String;
}
}
------------------------------------------------------------------------
TradeDetails.as
package com.tl.bonds
{
public class TradeDetails
{
public var tradeDate:String;
public var valueUsd:int;
public var nativeCurrency:String;
public var settlement:Settlement;
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s