Conference App Secrets Part 3: JSON vs XML

BLACKBERRY JAM / 01.10.14 / BlackBerry

QR Invaders_QR Code

In part 2 of our series on Bubble Byte’s QR Invaders app, Jon Cebula talks about how he used Unity to read and process QR codes into a compelling and dynamic experience for gamers. Check out part 1 here if you missed it!

Obtaining the QR Code

The first unique challenge for QR Invaders was to actually scan a QR code. I wanted the scanning screen to be slick and “game-like” in its own sense. Unity provides WebCamTexture and GUITexture classes that make using the device’s camera very simple.

I wanted 3D graphics to appear over the camera preview, but the GUITexture is drawn after all 3D geometry, so I had to rethink how to present the preview.

In the end I used a simple 3D plane and overlaid the camera preview into its texture. This is fairly easy to do using the MeshFilter and MeshRenderer objects. The code provided below shows how these are added to a MonoBehaviour, which is Unity’s base object for anything that is used within its scene environment.

    void Awake()     {         gameObject.AddComponent("MeshFilter");         gameObject.AddComponent("MeshRenderer");          meshFilter = (MeshFilter)GetComponent(typeof(MeshFilter));         meshRenderer = (MeshRenderer)GetComponent(typeof(MeshRenderer));         meshRenderer.renderer.material = material;         mesh = meshFilter.mesh;     }

This code creates a MeshFilter, which contains all the 3D objects mesh data ─ basically the things that describe the shape of the object in 3D space ─ and a MeshRenderer, which contains the information about how the object looks such as surface color and textures. We then store and reference the Mesh object from the MeshFilter. So now we just need to create the actual plane and set up the Texture:

    this.CameraPoints = new Vector3[] {         camera.ScreenToWorldPoint(             new Vector3(                 meshX, meshY,                 camera.farClipPlane * 0.99f             )         ),         camera.ScreenToWorldPoint(             new Vector3(                 meshX + meshWidth ,meshY,                 camera.farClipPlane * 0.99f             )         ),         camera.ScreenToWorldPoint(             new Vector3(                 meshX + meshWidth, meshY + meshHeight,                 camera.farClipPlane * 0.99f             )         ),         camera.ScreenToWorldPoint(             new Vector3(                 meshX, meshY + meshHeight,                 camera.farClipPlane * 0.99f             )         )     };      mesh.vertices = this.CameraPoints;     mesh.uv = new Vector2[] {         new Vector2 (0, 0),         new Vector2 (1, 0),         new Vector2(1, 1),         new Vector2 (0, 1)     };     mesh.triangles = new int []{0, 2, 1, 0, 3, 2};     mesh.normals = new Vector3[] {         new Vector3(0f, 0f, 1f),         new Vector3(0f, 0f, 1f),         new Vector3(0f, 0f, 1f),         new Vector3(0f, 0f, 1f)     };      material.mainTexture = this.WebCamManager.CamTexture;

Here we create an array of Vector3 objects and assign them to this.CameraPoints. This is then set to the vertices array of the mesh, which is all of the points in 3D space that define the plane (think of it like a dot to dot picture). Next, we set the triangles array with indices to each of the points defining triangles ─ this is like joining the dots. Although you can use quads in Unity, it’s best to use triangles, allowing you to define exactly when the separating line will appear, as quads are converted to triangles anyway.

Using this technique I can now create GameObjects and place them in front of the plane to make them appear in front of the camera preview.

QR Invaders_QR Mesh

I then used some shading effects to alter the camera preview in real-time, creating a tint so the user gets a more compelling visual response when the QR code is scanned. The overlaid text here is actually made up from multiple meshes, helping to reduce the performance hit while we’re in camera preview.

Parsing the QR Code

Once the camera was working, it was a case of capturing the data and parsing it into the Space Invaders. I used the dot patterns on the QR codes to determine the enemy types that would be created.

  • The 3 big squares you see on each QR code became the bosses for Levels 2, 3 and 4.
  • The smaller block that appears on most QR codes became the Level 1 Boss.

On bigger QR codes the smaller blocks appear more frequently throughout so I parsed each to make extra mini-bosses for Level 2, 3 and 4.

Groups of four white or black tiles in a square became Quad Invaders who help the Bbosses at the end of each stage. Finally, each single dot became either a black or a white Invader.

In the final blog post, I’ll discuss some of the level optimizations I used to ensure a consistent 60 FPS experience for the player. Thanks for reading!

Jonathon Cebula
Bubble Byte

About Bubble Byte

Bubble Byte is small group of developers who work independently to one another to try and create games that foremost play great and then secondly have a style all of their own. Although we do collaborate for the testing, the quality control phase, share ideas for improvements, and we may collaborate on some graphical, audio, or coding elements, generally each game is created by one developer.   qr2

Guest post by Ekkehard Gentz, BlackBerry Elite Developer

This is Part 3 of my series, “Conference App Secrets.”

This time I want to discuss how to persist data to guarantee a fast-responding app.

I was asked why my conference apps are performing so incredibly fast while retreiving sessions, speakers, or searching (it got many five star reviews because of this).

You should download the BBJam Asia Conference App to see it there in action. The answer isn’t easy and will cover some more articles. Let me start to explain…

BlackBerry Jam Asia uses a JSON Data Model

I’m using JSON to persist the data of my conference apps and there are many reasons. Using REST interfaces, you still get much of the data from responses in XML format, and in most cases, you have to consume such data and cannot change the backend.

The first thing I’m always doing is to convert the XML data into JSON data before proceeding.

You’ll find all used datafiles of this sample app and sources at Github.

I cannot tell you details about the BBJam Asia Conference API, so I generated some mockup data:

  • 10,000 random addresses to have a large file to experiment with
  • 119 public available speaker data to have some conference-relevant data

The addresses I’m using are generated by a free service from I highly recommend this service to developers if you have to provide prototype apps with data or if you want to test using large data sets. They even generate different data for the country you selected as well as GeoCoordinates.

The speaker data is a WordPress export file from another conference app and contains real data from publicly available sites.

Addresses and speaker data are in XML format. I’ll show you how to convert these files into JSON and explain why you should do this.

Both datasets can be found here:
app secrets 1

While developing and testing, you often have to take a look at the data, and JSON is much easier to read than  XML. Here’s one address in XML:

    <Company>Record Bar</Company>
    <CountryFull>United States</CountryFull>
    <StreetAddress>3002 Poling Farm Road</StreetAddress>

And here, the same address in JSON:

   "City" : "Lincoln",
   "Company" : "Record Bar",
   "Country" : "US",
   "CountryFull" : "United States",
   "Domain" : "",
   "EmailAddress" : "",
   "GivenName" : "Sharon",
   "Latitude" : "40.722909",
   "Longitude" : "-96.678577",
   "Number" : "1",
   "State" : "NE",
   "StreetAddress" : "3002 Poling Farm Road",
   "Surname" : "McCall",
   "TelephoneNumber" : "402-310-0424",
   "Title" : "Mrs.",
   "ZipCode" : "68501"

So improved readability is one reason to use JSON.

If you’re using QML then you can easily copy and paste objects from JSON directly into your QML code like:

var myAddress = {  …. the JSON object … } … myDataModel.insert(myAddress) ….

Sometimes this is useful for mock data where you simulate inserting objects into list DataModels.

In most cases JSON datafiles are smaller then XML:

  • 10,000 addresses in XML: 6.7 MB
  • 10,000 addresses in JSON: 5.9 MB

If the XML is using many long property names the file size can be reduced more, but sometimes if the XML structure is very complicated it may be that a JSON file is larger then XML (take a look at the speaker data).

Perhaps you already know that Cascades supports JSON very well. For example, if using QVariants from C++ in QML, where QVariantMap was mapped automatically to JavaScript Objects (== JSON). Perhaps this deep integration of mapping is the reason for ….

Speed Speed Speed

conference app 4© David Castillo Dominici |

Most of the time, inside a conference app I’m reading data from the cache and this is where JSON clearly wins over XML. Here are some values I measured using the 10,000 addresses:

  • READ JSON:  2,392 ms
  • READ XML:  71,342 ms

Rule #1: if caching data, always use JSON – this can be up to 30 times faster then XML

It’s curious: writing data to JSON is somewhat slower then XML. Here’s what I measured for the 10,000 addresses:

  • Write JSON: 5,255 ms
  • Write XML:   3,567 ms

The write speed differences are marginal, and you can do the write async and continue working while you’re waiting to get the data from read.

In some cases, you’re getting more data than you need. Take a look at the speaker data exported from WordPress as RSS in XML format. Both file formats, XML and JSON, aren’t easy to read and contain too much data in a complex structure, but it’s easy to workaround. As soon as you get the data from HTTP Response, transform it and remove unneeded properties. This will give you even more speed. Here’s the measurement using the 119 speakers:

  • Read XML:     378 ms
  • Read JSON:     76 ms
  • Read JSON v2: 23 ms

JSON v2 contains only the attributes you need in a less complex structure.

Rule #2: if getting complex data remove unused properties to get even more speed

It’s boring only to compare numbers, so let’s use Cascades to demonstrate the speed of JSON-based persistence or caching and see how easy it is to convert data between XML and JSON.


The sample application is only using two QML files:

  • main.qml
  • MeasureSpeakerPage.qml

Main.qml contains a NavigationPane displaying a page to visualize the measurements of reading and writing the 10,000 addresses.

MeasureSpeakerPage.qml will be pushed on top to visualize the measurements of reading speaker data from XML, JSON and JSON-less properties.

Let’s take a look at the main.qml first. There are some attachedObjects:

  • SystemToast
  • ComponentDefinition
  • Qtimer
attachedObjects: [
    SystemToast {
        id: infoToast
    ComponentDefinition {
        id: measureSpeakerPageComponent
        source: "MeasureSpeakerPage.qml"
    QTimer {
        id: myTimer
        property int usecase
        interval: 100
        singleShot: true
        onTimeout: {
            switch (myTimer.usecase) {
                case 0:
                case 1:

We’re using the SystemToast as an easy way to let the user know if a process was started or finished.

While the first Page is defined directly inside the main.qml, the MeasureSpeakerPage will only be pushed on demand if the users starts an Action. To avoid unnecessary creation of UI Controls, we only define the component and create it dynamically before pushing on top.

The QTimer is used as a workaround to have a small delay between hitting the Action item and calling the process in C++. As we’ve seen above, it takes a long time to read the 10,000 addresses from XML; over one minute! While executing this task, we want to disable the Action items what was done async and needs more ms.

Without using the QTimer there will be not enough time to disable the Action items. Why? Because we’re doing something you never should do in a real application: executing a long-running task on the UI Thread. While Network requests are async operations out of the box, reading a file isn’t and will block the UI.

The goal of this demo application is to let you feel the long execution time

So I’m using the QTimer workaround. Now disabling the ActionItems works as expected. Here’s how I did it:

NavigationPane {
    id: navPane
    property bool running: false
    Page {
        id: measureAddressesPage
        actions: [
            ActionItem {
                title: "Measure Addresses"
                enabled: ! navPane.running
                onTriggered: {
                    navPane.running = true
                    myTimer.usecase = 0
            ... more ActionItems


All Action Items will be disabled if a Task is running by using a property from NavigationPane. As soon as an ActionItem was triggered, the property ‘running’ was set to ‘true‘. Then, the QTimer was started with a delay of 100 ms. Before the QTimer starts, the System has enough time to disable all Action Items. Because I only want to use one QTimer Object, I added a property ‘usecase’ to know which task from C++ should be started.

How do you get the information now that the task has finished? We’re using Qt Signals and Slots; C++ emitted a signal and in QML we connected a function (Slot) to this Signal. The connection itself was done in onCreationCompleted{}.

You’ll find different Slots (functions) connected in the sample app. Here’s a simple function showing a Toast, stopping the ActivityIndicator and setting the property ‘running’ to false:

function processFinished() {
    navPane.running = false
    switch (myTimer.usecase) {
        case 0:
            infoToast.body = "10'000 Addresses processed.
read JSON ..."
            infoToast.button.label = "OK"
            infoToast.icon = "asset:///images/ca_done.png"
        case 1:
onCreationCompleted: {

There are more complex Slots getting result values from C++ if measuring addresses or speakers.

Here are some screenshots to see how the results from measuring addresses are displayed.

We start with an empty Screen:
conference app 8

Starting the Action “Measure Addresses” means the ActionItems will be disabled, a Toast will be shown, and an ActivityIndicator is running:
conference app 9

This is a long running task and can take up to two minutes!

Getting the signal that the task is finished, the Toast changes the text and icon, the ActivityIndicator is stopped, a bar chart is drawn and the ActionItems are enabled:
conference app 10

The BarChart should also work in Landscape:
conference app 11

…and also on the Q10:
conference app 12

From this BarChart you easily understand how fast reading a JSON file is directly compared with XML.

You also see that the Z30 is much faster than the Q10 (Z10 is similar); 81 seconds vs 117 seconds to read a XML file with 10,000 addresses.

As you know, Cascades provides no BarChart diagram as UIControl out of the box, but it can be done using plain Containers.

To get all of this working smoothly, there are some Cascades tricks, so let’s take a look at the code step-by-step.

I’m using a StackLayout, but you can also use a DockLayout – it’s up to you. For me, in this case a StackLayout seems to fit better. Here’s how the Containers are layed out:
conference app 13

Everything is contained in an outer Container with Top->Bottom orientation:

  • the ActivityIndicator: only visible and consuming space if running
  • two Containers using StackLayout Left->Right for the Header Labels using spaceQuota to adjust them to be correct
  • one Container using StackLayout Left->Right for the Footer Labels
  • in the middle the most difficult Container: the valueContainer – also using a StackLayout Left->Right containing 4 Containers: one for each bar

The valueContainer should consume all available space, so the footer Labels will always be positioned at the bottom. The available space will change if changing the orientation or starting and stopping the ActivityIndicator.

The 4 Containers inside the valueContainer represent the bars and are filled with a background color. The height of the bars must correspond to the values we’re getting from C++, where the largest value should fill out the complete height. All other bars will get a height relative to their values. This means every time the height changes we have to recalculate the height of these Containers.

The trick to get it done is using LayoutHandlers. A LayoutHandler will be notified if the Layout changes and you can ask the handler about the height, which is the value we need. We attach a LayoutHandler to the valueContainer and if the height changes, we recalculate the values.

Page {
    id: measureAddressesPage
    property int barHeight: 1200
    onBarHeightChanged: {
    property int max: -1
    property int readJson: 0
    property int writeJson: 0
    property int readXml: 0
    property int writeXml: 0
    Container {
        id: valueContainer
        topPadding: 10
        preferredHeight: measureAddressesPage.barHeight
        layout: StackLayout {
            orientation: LayoutOrientation.LeftToRight
        attachedObjects: [
            LayoutUpdateHandler {
                id: valueContainerLayoutHandler
                onLayoutFrameChanged: {
                    measureAddressesPage.barHeight = layoutFrame.height

We store the values as properties at the Page (max, readJson, writeJson, readXml, writeXml) and there’s also a ‘barHeight’ property.

The ‘barHeight’ property is bound to the preferredHeight of the valueContainer and initially set to a high value: 1200. Cascades tries to set this value as preferredHeight, but it is smart to only use a height which fits into the available space. As we already discussed, the available space can change. For example, starting the ActivityIndicator will reduce the height, and stopping it will enlarge the height. In this case the LayoutHandler will get a notification and we’ll use the actual assigned height to recalculate the values.

There are some special situations where the LayoutHandler from valueContainer won’t be notified about Layout changes, but the outerContainer will be notified. So as a little trick, we also add a LayoutHandler to the outerContainer, and if the Layout changes we simply set the preferredHeight of the valueContainer to a high value (1200), causing Cascades to grab all the available space and so on.

Another situation is coming back from Landscape to Portrait, where we also set this height value to let Cascades do the calculating of the available space.

The TitleBar was set as sticky. This is a trick to get the correct height on the Q10. Otherwise, the Q10 will use a floating TitleBar, which means the height of the TitleBar will be ‘available’ and then your footer Labels will be only visible if scrolling the TitleBar away. Setting the TitleBar as sticky solves this.

Please take a deeper look at the sample code:

Business Logic (C++)

Now let’s see what happens on the C++ side.

We need the ‘app’ context property to get access to the app. Remember, all methods must be invokable from QML. For more information, take a look at the sources or the previous articles of this series.

We now want to concentrate an JSON and XML. You must include:

#include <bb/data/JsonDataAccess>
#include <bb/data/XmlDataAccess>

Now we can convert. Here’s an example of how to read the content of a File, convert from XML to JSON, and save the File:

void ApplicationUI::convertXMLtoJSONAddresses() {
    XmlDataAccess xda;
    QVariant data;
    QString filenameXML;
    filenameXML = "addresses_us.xml";
    QFile dataFile(assetsXMLPath(filenameXML));
    if (!dataFile.exists()) {
        // do something
    bool ok =;
    if (ok) {
        data = xda.loadFromBuffer(dataFile.readAll());
    } else {
        // do something
    QString filenameJSON;
    filenameJSON = "addresses_us.json";
    JsonDataAccess jda;, dataPath(filenameJSON));
    emit conversionDone();

Removing the File-Open-Read-Write code converting is as simple as:

XmlDataAccess xda;
QVariant data;
data = xda.loadFromBuffer(dataFile.readAll());
JsonDataAccess jda;, dataPath(filenameJSON));

You can load from Files or from a ByteArray you’re getting from a Network request. JsonDataAccess or XmlDataAccess will give you a QVariant. This QVariant can be a QVariantMap if the content is a single Object, or a QVariantList if it’s an Array.

In the sample app, you’ll find samples of how to convert from XML to JSON, or from JSON to XML. You’ll also see how Signals are emitted and how to convert the speakers to a much smaller JSON file with really fast read access. Here’s the MeasureSpeakerPage:
conference app 18

Don’t forget
Please don’t forget to add the libraries to your .pro file

LIBS +=  -lbbsystem -lbbdata


From this sample you have learned:

  • HowTo create a Custom BarChart diagram
  • Enable / Disable ActionItems
  • Use of SystemToast in QML
  • Use of ActivityIndicator
  • Use of LayoutHandler to provide flexible Layouts for all Devices
  • Connect QML functions to C++ Signals
  • How to read and write JSON and XML files
  • How to convert JSON to XML or XML to JSON

The main goal of this article was to demonstrate the speed of JSON and the easy way to deal with JSON.

The next part of this series will explain:

  • How I’m using JSON to persist the data in a conference app
  • How to attach JSON data to ListView DataModels
  • How to search and filter JSON data.

We’ll use the same data as used in this sample.

Download and Discuss

The Sample APP is available at GitHub Open Source (Apache 2 License).

I also created a Thread in the Forums, where you can ask or discuss this.

Have fun with the sample app and copy and paste what you need to implement Custom BarCharts into your own apps or to use JSON for your data files!

About BlackBerry

BlackBerry is an enterprise software and services company focused on securing and managing IoT endpoints.


About BlackBerry

BlackBerry is an enterprise software and services company focused on securing and managing IoT endpoints.