Serialising Objects

Published by

on

With modern programming languages like C# and the advent of Reflections Serialising/Deserialising of Data is gotten easier.

Before we dive into the process of saving an object as string , lets understand some important terminologies.

JSON – has become a de facto standard for applications to send/receive data. JSON stands for Javascript Object Notation. Its a light weight data interchange format initially designed to send/receive data between client/server , but lately is being used to store and process local data too. Mongo DB for example makes use of page view format similar to JSON. ( A whole DB is designed to work using JSON , thats how efficient and user friendly it is )

Serialisation – The process of converting an Object into JSON string is referred to as Serialisation.

Deserialisation – The process of population an object variables from a JSON string is referred to as DeSerialisation.

Let us consider a Class that represents a Player in a game.

public enum HouseType
{
    Gryffindor = 1,
    Slytherin = 2,
    Hufflepuff = 3,
    Ravenclaw = 4
}
 
public class Player
{
    public string Name { get; set; }
    public int Health { get; set; }
    public PlayerType PlayerType { get; set; }
}

If we want to now store the object of type Player it would be in binary format and not readable in human format. Hence a better way to store this would be to Serialise the Player Object.

Player harry = new Player()
   {
      Name = "Harry Potter",
      Health = 100,
      PlayerType = PlayerType.Gryffindor
    };
// Now to convert the object harry into JSON , there are several plugins which will allow to Serialise an object.  

There are several plugins, JSON Unity Plugin has been one of the best, owing to its ability to convert a wide variety of DataTypes.

// To Serialise the Object , all we need to do is
String harrySerialised = JsonConvert.SerializeObject(harry);

The Serialised string would look something like this

{
   "Name":"Harry Potter",
   "Health":100,
   "PlayerType":"Gryffindor"
}

To Deserialise the above JSON back to into a Player Object, all that needs to be done is

// Populate the Player object using the json string 
Player harry = JsonConvert.DeserializeObject<Player>(harrySerialised);

// Note the use of class Player , telling the converter the json string holds the data pertaining to Class "Player"

In the gaming world this is a powerful workflow where in

  • Huge set of objects can be serialised and then sent across to the server for storing in Database etc.
  • Multiplayer games can now send across the player location and actions to a remote server or among clients
  • In Unity the player/game data is store in PlayerPrefs. All of this can be serialised into a single string for easy access and retrieval.
  • Google cloud Save to save a game progress makes use of this approach.

Hope this article has thrown some light on the JSON and how it can be efficiently used Serialise and Deserialise data! Happy Coding !

Previous Post
Next Post