Contact Us
Secure Video Conferencing LiveSwitch Cloud
Secure Video Conferencing LiveSwitch Cloud

Custom JSON using the FM Serializer

by Anton Venema, on June 15, 2012

Both WebSync and RestEasy come packaged with a custom JSON serialization engine called the Serializer (different from the Json class, which wraps calls to .NET's DataContractJsonSerializer). The Serializer allows you to take any object and serialize it with absolutely no requirements, restrictions, or changes to your models. The only catch is that you have to write a bit of code to make it happen.

For each object, you'll want to write 2 public methods - Serialize and Deserialize - and 3 private helper methods - Create, SerializeCallback and DeserializeCallback. Here's a simple example in C#:


public class User
{
public int? Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}

public class UserSerializer
{
public static string Serialize(User user)
{
return Serializer.SerializeObject(user, SerializeCallback);
}

public static User Deserialize(string userJson)
{
return Serializer.DeserializeObject(userJson, Create, DeserializeCallback);
}

private static void SerializeCallback(User user, Dictionary[string, string] jsonObject)
{
jsonObject["id"] = Serializer.SerializeInteger(user.Id);
jsonObject["name"] = Serializer.SerializeString(user.Name);
jsonObject["email"] = Serializer.SerializeString(user.Email);
}

private static void DeserializeCallback(User user, string name, string valueJson)
{
switch (name)
{
case "id":
user.Id = Serializer.DeserializeInteger(valueJson);
break;
case "name":
user.Name = Serializer.DeserializeString(valueJson);
break;
case "email":
user.Email = Serializer.DeserializeString(valueJson);
break;
}
}

private static User Create()
{
return new User();
}
}

As you can see, the process is very manual, but it's lightning fast! Whether the performance gain is worth the extra code is up to you. In general, we don't recommend using it unless you need maximum speed or something is prohibiting you from using one of the more popular serializers.

One of the problems we come across somewhat regularly is the manner in which the DataContractJsonSerializer serializes dictionaries. You would typically expect a Dictionary<string, string> to serialize to something like:


{
"key1": "value1",
"key2": "value2",
"key3": "value3",
...
}

Instead, however, it serializes to this:

[{
"Key": "key1",
"Value": "value1"
},{
"Key": "key2",
"Value": "value2"
},{
"Key": "key3",
"Value": "value3"
},
...]

This can be quite troublesome for JavaScript.

Thankfully, it's easy to write a custom serializer that will do what we need:

public class DictionarySerializer
{
public static string Serialize(Dictionary<string, string> dictionary)
{
return Serializer.SerializeObject(dictionary, SerializeCallback);
}

public static Dictionary<string, string> Deserialize(string dictionaryJson)
{
return Serializer.DeserializeObject(dictionaryJson, Create, DeserializeCallback);
}

private static void SerializeCallback(Dictionary<string, string> dictionary, Dictionary<string, string> jsonObject)
{
foreach (var key in dictionary.Keys)
{
jsonObject[key] = Serializer.SerializeString(dictionary[key]);
}
}

private static void DeserializeCallback(Dictionary<string, string> dictionary, string name, string valueJson)
{

dictionary[name] = Serializer.DeserializeString(valueJson);
}

private static Dictionary<string, string> Create()
{
return new Dictionary<string, string>();
}
}

A little bit of effort, but problem solved!

var dictionary = new Dictionary<string, string>();
dictionary["key1"] = "value1";
dictionary["key2"] = "value2";
dictionary["key3"] = "value3";

var dictionaryJson = DictionarySerializer.Serialize(dictionary);
// dictionaryJson is {"key1":"value1","key2":"value2","key3":"value3"}

 Websync Free Trial

Anton Venema

As Frozen Mountain’s CTO, Anton is one of the world’s foremost experts on RTC solutions, as well as the technical visionary and prime architect of our products, LiveSwitch Cloud and LiveSwitch Server. Anton is responsible for ensuring that Frozen Mountain’s products exceed the needs of today and predict the needs of tomorrow.