Encoding & Decoding functions in Flex


The other day one of my colleagues asked me whether do we have any functions inside flex that can encode a URL . Even though it quite obvious that something can be done manually to replace the strings, but in order to find some answers I looked into the Top level packages where I found encodeURI() and decodeURI() . What does these functions actually do?
encodeURI():
This function bascially encodes the string into a valid URI. The following shown below are not encoded by encodeURI() unless it is a small group of basic characters as given in the documentation.

Characters that are not encoded
0 1 2 3 4 5 6 7 8 9
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
; / ? : @ & = + $ , #
- _ . ! ~ * ' ( )

There is another encoding function that is slightly different from encodeURI()encodeURIComponent() which also does the same encoding of strings to URI but.Before we talk about the major difference we should be aware of some terms :

URIComponent: is the text that appears in between any special characters..for eg: http and http://www.google.com are the  URI components in http://www.google.com.
Component Seperatorsare the special characters used in a URI (: ; / ?)

Now coming back to the major difference the encodeURIComponent() in encodes the same as encodeURI() and additionally also encodes special characters (; / ? : @ & + $ , # )

decodeURI():
This function does the exact reverse of encodeURI() , it decodes the encoded URI into string where all encoded characters are restored into unencoded representation.

Escape sequence not decoded Characters equivalents
%23 #
%24 $
%26 &
%2B +
%2C ,
%2F /
%3A :
%3B ;
%3D =
%3F ?
%40 @

Using the decodeURIComponent() the escape sequence in the above table can be decoded.
All the above functions are in addition to the already existing escape() and unescape() functions which were also used in flash.escape() encodes all nonalphanumeric charachters into format % and hexadecimal sequence and unescape() decodes the URL encoded format to string.
So , there are plenty of things to play with when you need some strings to be encoded, each function does look similar but it has some variations which needs to be closely observed.Before concluding here is a small code snippets which includes the usage of above functions:


var redirectUrl:String = "http://www.somedomain.com?loggedin=true& username=Sun";
    
var escaped:String = escape(redirectUrl);
var unescaped:String = unescape(escaped);
    
var encoded:String = encodeURI(redirectUrl);
var decoded:String = decodeURI(encoded);
    
var encodedURI:String = encodeURIComponent(redirectUrl);
var decodedURI:String = decodeURIComponent(encodedURI);
    
trace("escaped: "+escaped); //escaped: http%3A//www.somedomain.com%3Floggedin%3Dtrue%26username%3DSun
trace("unescaped: "+unescaped); //unescaped:
http://www.somedomain.com?loggedin=true&username=Sun
    
trace("encoded: "+encoded); //encoded:
http://www.somedomain.com?loggedin=true&%20username=Sun
trace("decoded: "+decoded); //decoded: http://www.somedomain.com?loggedin=true&username=Sun
    
trace("encodedURI: "+encodedURI); //encodedURI: http%3A%2F%2Fwww.somedomain.com%3Floggedin%3Dtrue%26username%3DSun
trace("decodedURI: "+decodedURI); //decodedURI:
http://www.somedomain.com?loggedin=true&username=Sun


Meanwhile when I was googling more on this, I found out a bug reported to Adobe where it was reported that encodeURI and decodeURI misbehaves with httpservice which is mentioned to be deferred and will be fixed in 4.0 hmm..I am curious to know about your experiences when using these functions…

Advertisements

Published by

Sunil Nair

I am a Software Developer currently working on developing Rich Internet Applications using Adobe Flex/AIR. I have had diverse experience of developing application in software industry for the last 4 years.

2 thoughts on “Encoding & Decoding functions in Flex”

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