create a JSON file and save into disk - actionscript-3

how to create a JSON file for any object and save it on disk. after some days retrieve it back?

To write to a shared object
var so:SharedObject = SharedObject.getLocal("mySharedObject");
so.data.storedJSON = myJSON;
so.flush();
To retrieve it back elsewhere
var so:SharedObject = SharedObject.getLocal("mySharedObject");
myJSON = so.data.storedJSON;
convert to ByteArray
save
read a this documentation: registerClassAlias
registerClassAlias("com.myDomain", MyClass);
var myClass:MyClass = new MyClass();
var ba:ByteArray = new ByteArray();
ba.writeObject(myClass);
so.data.byteArray = ba;
ba.position = 0;
read
myClass = so.data.byteArray.readObject();

Use SharedObjects. Read up on them here.

Related

Why does (myMC.myArray = myOtherMC.myOtherArray;) cause any changes to myMC.myArray to also change myOtherMC.myOtherArray?

var myArray:Array = new Array();
var myMC:MovieClip = new MovieClip();
myMC.myArray = myArray;
trace(myMC.myArray[10]); //Output: undefined
var newMC:MovieClip = new MovieClip();
newMC.myOtherArray = myMC.myArray;
newMC.myOtherArray[10] = [];
newMC.myOtherArray[10][0] = 100;
trace(myMC.myArray[10]); //Output: 100
Why does that happen, and is there any way to avoid it?
EDIT:
Found a function that can clone associative arrays here.
Here is the function (from the above link):
function clone(source:Object):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}
Does making the function return type "*" mean that it can be any type? or is it something specific to objects/arrays?
It's because you have passed a reference of the original array to the new clip. Arrays, as objects, are not primitives and therefore will always be referenced.
If you would like to clone an array, keeping the original intact, use this method:
var b:Array = a.concat();
b will be a new array. a can modified without changing b.

loading saved bitmap

i had saved a bitmap and i want to load it in runtime.
here is my codes:
var saveDataTxt:SharedObject = SharedObject.getLocal("File");
var textName:String; var textClass:Class;
textName = "Text0" + 1;
textClass = getDefinitionByName(textName) as
Class;
var tx:BitmapData = new textClass(); txtP[1] = new
Bitmap(tx);
saveDataTxt.data.txtArray[1] = txtP[1];
addChild(saveDataTxt.data.txtArray[n]);
but it gives me an error :
**TypeError: Error #1034: Type Coercion failed: cannot convert Object#384c2b1 to flash.display.DisplayObject.**
whats the solution?
To store a bitmap in a shared object, you would need to serialize it to a byte array first (see Is it possible to store images in the SharedObject of Flash?)
What you can do, is just store your custom BitmapData subclass in the shared object (if you don't want to bother with byte arrays)
//you need to register every class/subclass in your shared object
registerClassAlias("flash.display.BitmapData", BitmapData);
var saveDataTxt:SharedObject = SharedObject.getLocal("File");
var textName:String; var textClass:Class;
textName = "Text0" + 1; textClass = getDefinitionByName(textName) as Class;
registerClassAlias(textName,textClass); //need to register the custom class
var tx:BitmapData = new textClass(); txtP[1] = new Bitmap(tx);
saveDataTxt.data.txtArray[1] = tx; //just store the bitmap data
addChild(new Bitmap(saveDataTxt.data.txtArray[n] as BitmapData)); //you have to cast the object as bitmap data

Parsing JSON returned by URLLoader

I'm using a URLLoader and URLRequest to make a call to youtube's api. The return is formatted as json and looks like the following: http://pastebin.com/WxPS9NCB.
I'm trying to capture the "href" value located on line 42 in the above pastebin. But the code I have isn't working.
var urlLoader:URLLoader = new URLLoader(new URLRequest(apiURL));
urlLoader.addEventListener(Event.COMPLETE, function(e:Event) {
var json:Object = e.target.data;
var href:String = json.link[0].href;
trace(href);
});
Any ideas?
Flash does not parse JSON automatically. Use AS3 core libs JSON parser (https://github.com/mikechambers/as3corelib)
And replace var json:Object = e.target.data; with var json:Object = JSON.decode(e.target.data);
EDIT:
After a cursory glance at the JSON file you should use json.feed.link[0].href to access the data you are looking for.
try this:
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(new URLRequest(apiURL));
urlLoader.addEventListener(Event.COMPLETE, function(e:Event) {
var json:Object = e.target.data;
var href:String = json.link[0].href;
trace(href);
});

Object Data Type in AS3 and Flash Builder 4.5.1

I am trying to save a Sprite object as a file on the device I'm working on and it seems to work. the problem I'm having is reading the saved file back and placing it back on stage as a sprite. Below is the code I have so far, could someone tell me what it is I'm doing wrong? I have a suspicion that the saved isn't what I expect it to be since the file sizes have been under a kilobyte.
public function save_album(e:Event):void
{
var outFile:File = File.documentsDirectory; // dest folder is desktop
outFile = outFile.resolvePath("canvas3.bin");
var fs:FileStream = new FileStream();
var bytes:ByteArray = new ByteArray();
//trace (File.documentsDirectory.url + "/canvas2.bin");
fs.open(outFile, FileMode.WRITE);
bytes.writeObject(graffitiContainer) //graffitiContainer is a Sprite
bytes.position = 0;
fs.writeBytes(bytes, 0, bytes.length);
fs.close();
}
public function open_album(e:Event):void
{
var inBytes:ByteArray = new ByteArray();
var inFile:File = File.documentsDirectory;
inFile = inFile.resolvePath("canvas3.bin"); // name of file to read
var inStream:FileStream = new FileStream();
inStream.open(inFile, FileMode.READ);
inStream.readBytes(inBytes, 0, inBytes.length);
inStream.close();
inBytes.position = 0;
ui.removeChild(graffitiContainer);
var obj:Sprite = inBytes.readObject() as Sprite; //returns a null
graffitiContainer = obj;
ui = new UIComponent();
graffitiContainer.x = 0;
graffitiContainer.y = 100;
ui.addChild(graffitiContainer);
}
Not fully sure I understand what you're trying to accomplish; however, this implementation doesn't do what you're thinking - writeObject could only serialize general public properties, and not the graphics member.
You could render it to a Bitmap.
Saw a blog post about this:
http://jacwright.com/201/serializing-display-objects/

How do you combine two bytearrays in AS3?

I'm trying to combine two ByteArrays to send it out as post data using URLRequest.. Whenever I try to simply add them up, the request becomes GET instead of POST and the data for some reason doesn't get included.
create a total ByteArray by adding other ByteArray objects to it via the writeBytes() public method of the ByteArray class.
more info here: Reading and writing a ByteArray
Combining / Concatination Two Byte Arrays
Var Data:ByteArray = new ByteArray();
Var Salt:ByteArray = new ByteArray();
var DataAndSalt:ByteArray = new ByteArray();
DataAndSalt.length = (Data.length + Salt.length);//Defines the **length of Resultant Array**
//Array Copy Method(VB)/ Concate the ByteArray(ActionScript) one After another
DataAndSalt.writeBytes(Data);
DataAndSalt.writeBytes(Salt);
I will Show here Conversion of String into Byte Array and Merging Them (concate /combining) them into single byte array
// In Detail
var HashOut:ByteArray = new ByteArray();
var byterrData:ByteArray = new ByteArray();
var byterrSalt:ByteArray = new ByteArray();
//conversion of string Data and Salt in respective (UTF-8 and Default) Byte Array
var Data:String = "password";
var Salt:String ="‰ô©³¶í"; //Using Special Characters in a String variable
byterrData.writeMultiByte(Data, "iso-8859-1");
byterrSalt.writeMultiByte(Salt,Salt);
var DataAndSalt:ByteArray = new ByteArray();
DataAndSalt.length = (Data.length + Salt.length);
// Concate the ByteArray
DataAndSalt.writeBytes(Data);
DataAndSalt.writeBytes(Salt);
//Now You can Trace It by using
trace(DataAndSalt[0]);
trace(DataAndSalt[index Number]);
Not sure what your code looks like... the GET/POST issue is very weird.
However, use the below instead of trying to "add them up" (whatever that means).
array3 = array1.concat(array2);