How To Exclude Properties From Serialization

Holger Weissböck on June 26, 2013

Hello Floxxers, the new SDK (0.7) is out and it brings a feature many of you have been requesting in the forums. As of now you can selectively exclude properties from being serialized to and deserialized from the Flox servers! To pick you up, I’ll quickly summarize that particular problem:

Saving An Entity

Let’s say our game implementation uses entities of type GameSession to represent and store the state of each played game session. It will, for example, hold the information which actions a player has made during his endeavours. For that it uses the property _actions. The GameSession class may look like this:

public class GameSession extends Entity {

    private var _actions:Array;

    public function GameSession() {
    }

    public function get actions():Array {
        return _actions;
    }

    public function set actions(actions:Array):void {
        _actions = actions;
    }
}

Now, since GameSession extends Entity it’s pretty easy to store the current game state to the Flox server. There are several different ways to save a Flox entity and you can read up on them in the documentation. For now, we’ll just save it using the simplistic saveQueued() method:

var gameSession:GameSession = new GameSession();
gameSession.actions = new Array();
gameSession.saveQueued();

That’s it! The GameSession is saved on the server: Your player can now load it from every device he wants and continue that particular game session at any time.

Adding Transient Information

Now, during development you’ll sometimes reach situations where it would be pretty convenient to add properties to the GameSession object, that do not necessarily have to be stored on the Flox servers. For example, if your GameSession points back to some interface element, like this:

public class GameSession extends Entity {

    private var _actions:Array;
    private var _currentScoreSprite:Sprite;

    public function GameSession() {
    }

    public function get actions():Array {
        return _actions;
    }

    public function set actions(actions:Array):void {
        _actions = actions;
    }

    public function get currentScoreSprite():Sprite {
        return _currentScoreSprite;
    }

    public function set currentScoreSprite(currentScoreDisplayObject:Sprite):void {
        _currentScoreSprite = currentScoreSprite;
    }
}

Disclaimer: I’m not saying referencing interface elements in your model is beautiful or a good idea: I just picked that property for the sake of this example. I guess, in real life everyone has to decide for himself if he wants to be so reckless as to intermix the data model with actual interface elements. Moving on!

If we save this entity, both properties _actions and _currentScoreSprite will be serialized and stored on the Flox servers. However, it’s quite unlikely that you actually want the information about _currentScoreSprite stored on the Flox server. For what purpose, really? Furthermore, the next time we load this entity from the Flox server the SDK will not be able to correctly deserialize and recreate the _currentScoreSprite anyway. Flox can correctly store and load properties of types boolean, integer, number, string, array and objects (used as dictionaries).

Fixing The Serializer

So, we conclude that we need to exclude the _currentScoreSprite property from the serialization process. We don’t want it on the server. Here’s how we do that with the new Flox SDK:

public class GameSession extends Entity {

    private var _actions:Array;
    private var _currentScoreSprite:Sprite;

    public function GameSession() {
    }

    public function get actions():Array {
        return _actions;
    }

    public function set actions(actions:Array):void {
        _actions = actions;
    }

    [NonSerialized]
    public function get currentScoreSprite():Sprite {
        return _currentScoreSprite;
    }

    public function set currentScoreSprite(currentScoreDisplayObject:Sprite):void {
        _currentScoreSprite = currentScoreSprite;
    }
}

As you can see, the only thing we did, is add the [NonSerialized] metadata to the getter of _currentScoreSprite. Now, when we save that entity, the _currentScoreSprite property will not be serialized to the server, which not only keeps the amount of garbage data down on your server-side Flox entities, but also reduces the size of serialized entities thus saving transmission time.

SDK Update

In order to get that functionality you need to update your SDK to the latest version (0.7).

  • If you’re already using Flox, you should download the latest SDK version. Just replace your current SDK with the new one.
  • If you are new to Flox and need some guidance with your setup check out our getting started guide.

By the way, if you’re interested in the other changes to the SDK, you can check out the changelog in your docs section or the corresponding API documentation.

That’s It

That’s it with the latest update to Flox! As usual, please let me know what you think! And perhaps … what you’d like to see next!