Deserializing JSON to a .NET Object

Generally in .NET MVC you would use the default model binders to deserialize JSON to a .NET object. However, there are some cases, involving “complex” collections, where this becomes a bit tedious to do. Microsoft provides a few ways around this, but none are satisfactory. Unlike the default model binders, the JavaScriptSerializer provides support for deserializing JSON to a .NET object with complex collections.

Take the following simple class and method signature for example:

// simple class
public class SaveInformation
{
    public string Name { get; set; }
    public Dictionary<string, List<SaveItem>> Components { get; set; }
}

// method signature
public ActionResult Save(SaveInformation saveInformation)

Below I’ve included some simple JSON that we will pass to .NET via AJAX:

{"SaveToSession":false,"Name":"My Stupendous Thing","Components":{"Component1":[{"ProductId":"1234"}]}}

We are passing the JSON to .NET using AJAX with the “application/json” contentType. Because we are using the “application/json” contentType, .NET will automatically call the JsonValueProviderFactory and map the information over to our SaveInformation model. It seems pretty straight forward, the Save method will receive a SaveInformation object when it is called.

Not so straightforward unfortunately, the Save method does receive a SaveInformation object. If we inspect our SaveInformation object we see the Name is populated perfectly fine, but the Components dictionary ends up being null.

The reason for this? It seems that the JsonValueProviderFactory doesn’t fully support pure JSON syntax. In order for .NET to properly parse a complex collection, you actually have to give it a numerical index.

From http://msdn.microsoft.com/en-us/magazine/hh781022.aspx:

Though it’s somewhat counterintuitive, JSON requests have the same requirements—they, too, must adhere to the form post naming syntax. Take, for example, the JSON payload for the previous UnitPrice collection. The pure JSON array syntax for this data would be represented as:

[
    { "Code": "USD", "Amount": 100.00 },
    { "Code": "EUR", "Amount": 73.64 }
]

However, the default value providers and model binders require the data to be represented as a JSON form post:

{
    "UnitPrice[0].Code": "USD",
    "UnitPrice[0].Amount": 100.00,

    "UnitPrice[1].Code": "EUR",
    "UnitPrice[1].Amount": 73.64
}

I know right? It was hard for me to read this as well due to the anime-sized tears in my normal-sized eyes. The problem is, no browser I know of has a JSON.serializeForDotNet function. So this leaves us with two options, make our own JSON serializer, or make our own ValueProvider/ModelBinder. Neither of these options sounded very appealing to me, so I went with the hidden third option. Pass in a string 🙂

You can use the JavaScriptSerializer class, located in System.Web.Script.Serialization, to deserialize JSON information and, as an added bonus, it actually handles properly formatted pure JSON!

So, the new method looks like this. We pass in a string value and deserialize it using the JavaScriptSerializer. Now we receive a SaveInformation object with a fully populated Components dictionary.

public ActionResult Save(string saveInformation)
{
    // instantiate a JavaScriptSerializer
    JavaScriptSerializer serializer = new JavaScriptSerializer();

    // use the JavaScriptSerializer to deserialize our json into the expected object
    SaveInformation saveInformationObject = serializer.Deserialize<SaveInformation>(saveInformation);

URF! Ultra Rapid Fire mode needs to come back!

URF Login on April 1
URF Login on April 1

Riot introduced Ultra Rapid Fire mode into League of Legends on April 1 as an April fools joke. However, the community saw it as far less than a joke. The reasons that Riot gave for introducing URF included aboloshing “anti-fun”. I think they were on to something.

Here are the basics that were granted in Ultra Rapid Fire mode:

  1. Mana and Energy costs were removed (no need to manage that resource).
  2. Ranged champions attackspeed increased by 100%.
  3. Every champion begins the game with 80% cooldown reduction on abilities and spells.

This handy tool tip explains the gist of it:

The only way to play League anymore.
The only way to play League anymore.

There were some additional features I liked, movement speed was increased, so I didn’t have to buy boots every single game.

I played URF everyday since April 1st when it came out. I was very happy that (after the community begged and begged) Riot extended it for another week. But, alas, Riot ended it last night, April 13. I played one last game with ‘Vi’ which we won, and waved goodbye.

At the time I thought that everything would be fine. I’d go back to playing normal mode and it’d be just as fun as it was before. This morning I logged in and played a match with Vi on the Twisted Treeline map. It was very disappointing.

The movement speed was too slow, even with Boots of Speed. Waiting for cooldowns to proc was annoying. It took forever to build any items (even on Treeline). I was overwhelmed with a feeling of general frustration. The game had become less fun.

It used to be that I looked forward to playing League of Legends. However, when I think about playing League of Legends now I just feel extremely disappointed. I don’t really want to play anymore. It’s not that the game is bad, it’s a good game. But, after seeing how fun it could be it’s just not as interesting anymore.

Other people have noticed the same thing. The community is longing for URF to come back. Notice some of the response.

So with that said, I think I’m going to be taking a break from League of Legends. It’ll give me some time to finish some other games I’ve been needing to finish, like Tomb Raider 2013. Maybe I’ll come back later refreshed and find the game to be fun again. Maybe I’ll get fed up and program my own URF mode game. Or… maybe it is just time to say goodbye.