Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

Effortless Flex 4 Development: Four Data Formats

  • Print
  • + Share This
In this excerpt from Effortless Flex 4 Development, Larry Ullman looks at four data formats—plain text, XML, JSON, and AMF—and evaluates the pros and cons of each format.
From the book

Four Data Formats

There are four data formats that I'll discuss and use in this book: plain text, XML, JSON, and AMF. To start things off simply, I want to look at each on its own, out of context, so that you can better distinguish the data representation from the code being used to manipulate the data.

When evaluating the pros and cons of each format, there are four criteria to consider:

  1. Ease of creation on the server
  2. Ease of use on the client
  3. Potential data complexity
  4. Transmitted data size

For the first and second criteria, I'm thinking of how much code is required to generate data in a given format and to retrieve the data from that format. These considerations are tied together and are also reflective of the third criteria. (Implied in these is whether extra libraries are required as well.) Some data, like plain text, is easy to create and use but cannot convey that much information; conversely, JSON is harder to create and use but can convey lots of details.

A final matter is the size, in bytes, of the data itself. This is important, as the amount of data being transferred from the server to the client will have an impact on the application's performance and the server's scalability.

For example, if you want to send the word Flex from one computer to another, you're only sending four characters of data, which is probably also four bytes: not a big deal. But if you wanted, like in an HTML page, to indicate that the word Flex should be in bold, you'd now have to send < strong > Flex</strong >. By using HTML to mark up the text, you've more than quadrupled the amount of data being transferred while only conveying one extra bit of information (bold). As the number of people using that data increases, even minor size differences can have huge implications.

You have to remember, however, that the data size is just one criteria; there is no benefit to data that is transmitted more quickly but isn't all that useful. As the developer, you'll need to select the appropriate data format for the situation, which is why it's necessary to be familiar with the options out there. Secondarily, if you're making use of third-party services, like Yahoo!, Amazon, and others, you likely won't have a choice as to what data format to use, and will just have to handle whatever that service returns.

Plain Text

Plain text is exactly as it sounds: characters without any formatting or markup. Examples are:

  • 112.43
  • true
  • red,blue,green
  • Franz Kafka was a 20th century writer...

It's just text: There's nothing to be evaluated or parsed or interpreted or anything. Plain text is universally readable and easily transmitted.

For the record, I'm making a distinction between plain text as a data format as opposed to a file format. XML, JSON, MXML, ActionScript, and lots of other languages are written in plain text files, whereas Microsoft Word documents, among many others, are binary files. The former are readable by many applications; the latter are stored in proprietary formats, and are only readable by a few programs.

As a data format, plain text contains no markup. For example, the HTML < strong > Flex < /strong > contains tags that provide added meaning. Plain text data has none of this. The most elaborate meaning that might be conveyed in plain text would be commas or tabs used to break up individual values, and newlines to indicate individual lines of text. In other words, the most complex bit of plain text data will just be a list of comma-, tab-, or newline-separated values.

Plain text is the simplest data format to create and use. And when plain text is the media, the server only transmits the minimum amount of data, without any extra information. However, plain text can only be effective for representing the most basic information.

XML

XML has been around for years and is widely, widely used. In fact, MXML is derived from XML, as are HTML, Really Simple Syndication (RSS), and many other languages. XML is technically plain text, in terms of how it's created and stored, but it contains markup to provide additional details.

Example XML

XML starts with a root document element, like Application in MXML. XML contains only one root document element, and all of the information must be stored within that. For an example, if you want to represent a catalog of artists as XML, you might start with an artists root element:

<artists></artists>

Next, within that root tag, you may have an element for each individual artist:

<artists>
  <artist></artist>
  <artist></artist>
  <artist></artist>
</artists>

Within the artist tag you'll want to represent all the information about a given artist: name, date of birth, works of art, etc. To make the data most usable, you wouldn't want to place this information just within the artist tag, but rather as atomically as possible, with each nugget in its own element:

<artist>
  <name>Georges Seurat</name>
  <birthDate>December 2, 1859</birthDate>
</artist>

To represent individual works of art, where multiple works would be associated with each artist, you would create a new work tag, one for each piece, but all within the artist record:

<artist>
  <name>Georges Seurat</name>
  <birthDate>December 2, 1859</birthDate>
  <work>
    <title>The Laborers</title>
    <year>1883</year>
  </work>
  <work>
    <title>The Models</title>
    <year>1888</year>
  </work>
</artist>

You can also add information using attributes (just like MXML components have attributes or properties). Again, what attributes you create where is really up to you:

<artist id="2490">

or

<work type="painting">

Of course, elements can have multiple attributes:

<work id="583782" type="painting">

In the end, a body of XML data is said to create a tree-like structure. You have the root element that contains one or more child elements (a child is also called a node). Each child can have its own children, and so on, until the data is fully represented.

XML Syntax Rules

You just saw an example of how XML is structured, but what are the exact syntactical rules? Chapter 1, "Building Flex Applications," discusses the basic XML rules with respect to MXML, but here they are again:

  • XML is case-sensitive.
  • Every attribute value must be quoted (and double-quotes are preferred).
  • Every tag needs to be formally closed.
  • Every tag needs to be properly nested.

With respect to closing tags, there are two options. The first is to create opening and closing tags, as in the example XML. This is normally done for any element that might contain values, including other elements. You can also close tags by concluding the opening tag with a slash before the closing angle bracket:

<tag />

You'll see this latter syntax in cases where the values are solely reflected in attributes:

<image source="somefile.png" />

As for nesting of tags, this only applies if you're using opening and closing tags. What is meant is that if you open element A, then open element B (so that element B is contained within—and is a child of—element A), you must then close B before closing A.

Because XML uses the angle brackets and quotation marks to create elements and attributes, you cannot use them within the values of elements or attributes, as in

<question answer="false">4 > 8</question>

In such cases, you'll need to use an entity version of any special character. Table 7.1 lists the five entities to watch for in XML. Each begins with an ampersand and ends with a semicolon.

Table 7.1. XML Entities

Symbol

Entity Version

&

&amp;

<

&lt;

>

&gt;

'

&apos;

"

&quot;

If you'd rather not use entities, you can use a CDATA block:

<question answer="false"><![CDATA[
4 > 8
]]></question>

Whether you use CDATA or entities will probably be determined by the amount of data being represented and the number of special characters found within that data.

Finally, in terms of XML syntax, know that XML stored or transmitted often includes the XML declaration, prior to the XML data itself:

<?xml version="1.0" encoding="utf-8"?>

This is the same code as found on the first line of MXML files. It indicates to the program reading the file that the file contains UTF8-encoded XML.

Pros and Cons

XML is relatively easy to create, is extremely extensible ("extensible" is in the name, after all), and can convey lots of well-organized data. Thanks to something called E4X, XML is a snap to use in ActionScript, as you'll see later in the chapter.

On the other hand, XML syntax is demanding and, if it's not 100% right, like if you don't properly nest or close a tag, then the entire XML data will be unusable. This can be a common cause of problems when dynamically generating XML. Also, because of all the added tags, both opening and closing, XML involves transmitting extra data.

Still, all in all, XML is an excellent choice for most situations.

JSON

JavaScript Object Notation (JSON) is one of those acronyms that may sound obtuse but is really exactly what it means: In JSON, data is represented as a JavaScript object. Since JavaScript and ActionScript have the same lineage, JSON data is quite similar in syntax to an ActionScript object, too. A JavaScript (or ActionScript) object, at its root, is represented by curly braces: {}. That is a valid, albeit empty, object.

JSON data objects have property-value pairs (properties just being variables found within an object). To add these to an object, use the property : value syntax, with each pair separated by a comma:

{
name: "Georges Seurat",
birthDate: "December 2, 1859"
}

The properties need not be quoted, but the values do unless they are numbers. Still, you'll commonly see all of the properties and values quoted:

{
"title":"House I",
"year": "1998"
}

If you have multiple objects to represent, you create an array of them, using the square brackets, separating each object by a comma:

[{
name: "Georges Seurat",
birthDate: "December 2, 1859"
},
{
name: "Roy Lichtenstein",
birthDate: "October 27, 1923"
}]

JSON is equally able to represent structured data easily, and with fewer characters than XML (because you're omitting the closing tags). But while you may think JSON's syntax is simpler than XML, the syntax can get hairy quickly. For example, this bit of XML,

<artist id="3955">
  <name>Roy Lichtenstein</name>
  <birthDate>October 27, 1923</birthDate>
  <work type="sculpture">
    <title>House I</title>
    <year>1998</year>
    <image source="3955_housei.png" />
  </work>
</artist>

would look like this in JSON:

{
  artist:{
    id:3955,
    name: "Roy Lichtenstein",
    birthdate: "October 27, 1923",
    work:{
     type: "sculpture",
     title: "House I",
     year:1998,
     image:{
       source: "3955_housei.png"
     }
    }
  }
}

Now imagine how the JSON would look when you start representing multiple artists and multiple works of art! And then, to be formal, wrap everything in double-quotes...

As with XML, make a slight mistake, like omitting a comma or curly bracket, and the data is useless. For this reason, you'll really want to use a special library in PHP to create the JSON data and a few extra steps are required to use the data in ActionScript. You don't have to transfer quite as much JSON data as you would with XML, but the efforts to encode and decode it minimize any comparable benefit.

While I'm inclined to choose XML over JSON (between the two), you might sometimes be in a situation where JSON is your only option.

AMF

Action Message Format (AMF) was created by Adobe specifically as a way to improve communications between a server and a Flash client. Unlike plain text, XML, and JSON, AMF uses a binary format, which means that it cannot be represented in a book like the others. More than a format for transmitting data, AMF provides a foundation for clients to directly communicate with servers. Instead of just calling a PHP script and seeing the results, AMF lets the client interact with PHP scripts, as appropriate. In this regard, the PHP script on the server is acting as an intelligent service, not as a single, standalone document. Moreover, by using something called a value object, complex data types can be transmitted and used by both the client and server as if they were native to both ActionScript and PHP (this will mean more at the end of Chapter 9, "Using Complex Services").

Using AMF in PHP requires a special library and the use of object-oriented programming (OOP). But the OOP can be trivial and easy to implement, and the AMF response allows for complex data sets to be transmitted quite efficiently (much as a JPEG can represent an image using a fraction of the original bytes, AMF can create a more byte-efficient representation of data). And, when the AMF response returns to the Flash client, the result can be handled in ActionScript easily.

  • + Share This
  • 🔖 Save To Your Account

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020