You are currently browsing the Eric Feminella blog archives for August, 2006


AS3 JSONDecoding Class

I added a simple JSON decoding class to the ActionScript 3 source code page of my blog today. This is a useful base class which you can extend to use a JSON object as a data source.

The JSONDecoder class defines a simple set of methods for loading a JSON encoded object from a specific URI, decoding the result into an ArrayCollection of ActionScript Objects, and displaying any errors that may occur.

JSON (JavaScript Object Notation) objects are a lightweight data-interchange format which are very useful due to the fact that they are language independent. JSON objects are simple objects comprised of name / value pairs which are easy to use with any language such as C, C++, C#, Java, JavaScript, ActionScript etc.

AS3 DeepCopy Utility Class

In ActionScript 3 when you create an object and set it’s value to that of an existing object, both the new object and the original object point to the same reference. This is known as creating a “shallow copy” of the original object. In a shallow copy, only a reference to the original object is created rather than creating a copy of the object directly.

For instance if you were to do the folowing:

var objA:Object = {id: “Object A: Original ID”};
var objB:Object = {id: “Object B: Original ID”};
var arrA:Array = new Array(objA, objB);
var arrB:Array = arrA;
objA.id = “Some new value”;
trace(arrA[0].id);
trace(arrB[0].id);
outputs:
“Some new value”
“Some new value”

As you can see in the example this can potentially cause problems if you need the indices in the array to not reflect changes in the objects in which they reference. In order to resolve this we would need to make what is known as a ‘deep copy’ of the objects in the Array. In a deep copy the objects that are being referenced are copied to a new memory location rather then pointing to the location of the reference objects themselves.

I wrote a simple class that uses a ByteArray to create a deep copy of the original objects. So if you wanted to create copies of the original Objects you could do so as in the following:

var objA:Object = {id: “Object A: Original ID”};
var objB:Object = {id: “Object B: Original ID”};
var arrA:Array = new Array(objA, objB);
var arrB:Array = DeepCopy.clone(arrA);
objA.id = “Some new value”;
trace(arrA[0].id);
trace(arrB[0].id);
outputs:
“Some new value”
“Object A: Original ID”

Below I have provided a link to view the source code.

DeepCopy

Real-World Flex Seminar in NYC

I will be attending the “Real World Flex Seminar” aptly titled “Next Generation Application Development with Flex” in NYC on August 14th located at the Roosevelt Hotel.

Speakers will include some of the industries leading Flex developers including Chris Coenraets and Jesse Warden as well as many others who are helping to push Flex into the next generation of RIA development.

For more information check out :http://www.flexseminar.com/