JSON Deserialization Made Simple With System.Web.Helpers and List<dynamic>

February 3 2011

Been playing with WebMatrix and have come across some crazy and beautiful code that I really liked in the FourSquare helper on CodePlex. Check this method out, which returns a graph of friends from the fourquare api

/// Returns a list of friends
    /// </summary>
    /// <param name="accessToken">The access token of the authenticating user.</param>
    /// <param name="userId">The Id of the person for whom to pull a friend graph. if not specified, the authenticating user's list of friends will be returned.</param>
    public static IList<dynamic> GetFriends(string accessToken, int userId = 0) {
        var url = "https://api.foursquare.com/v2/users/{0}/friends?oauth_token={1}";

        var client = new WebClient();
        var jsonResult = client.DownloadString(string.Format(url, userId == 0 ? "self" : userId.ToString(), accessToken));
        var result = Json.Decode(jsonResult);
        
        return new List<dynamic>(result.response.friends.items);
    }

Notice how tight this code is. And note the crazy dynamic typing that C#4 provides in combination with the Json.Decode method. If you are wondering where that JSON.Decode method came from, it is from System.Web.Helpers -- you can use this dll anywhere, doesn't have to be with WebMatrix or Razor. Here's a link to the docs on System.Web.Helpers.Json: http://msdn.microsoft.com/en-us/library/system.web.helpers.json(VS.99).aspx 

 Serialization and JSON have long been painful in .NET because of .NET's strong typing.  This got better with the C# dynamic keyword (see this post and this post) but nothing is as elegant as the code above.

Check this example out, which returns a user's FourSquare badges:

/// <summary>
    /// Returns badges for a given user.
    /// </summary>
    /// <param name="accessToken">The access token of the authenticating user</param>
    /// <param name="userId">The Id of the user</param>
    public static IList<dynamic> GetBadges(string accessToken, int userId = 0) {
        var url = "https://api.foursquare.com/v2/users/{0}/badges?oauth_token={1}";
        
        var client = new WebClient();
        var jsonResult = client.DownloadString(string.Format(url, userId == 0 ? "self" : userId.ToString(), accessToken));
        var result = Json.Decode(jsonResult);
        
        var badges = new List<dynamic>();
var groups = new List<dynamic>(result.response.sets.groups); var allBadgesGroup = groups.FirstOrDefault(g => g.type == "all"); if (allBadgesGroup != null) { foreach(var badgeId in allBadgesGroup.items) { badges.Add(result.response.badges[badgeId]); } } return badges; }

Notice how the two dynamic lists get created which are then manipulated with a LINQ lambda query.  Nice! Having dealt so often with either manually mapping JSON to CLR objects and/or dealing with arrays nested in arrays nested in arrays, I'm absolutely loving this.

 

Comments (3) -

2/4/2011 7:45:19 AM #

robert levy

Interestingly, if I try to use this from a console app I get
"Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed."

Exact same code works fine in a winforms app.

robert levy

5/26/2011 12:31:28 AM #

Tiffany &amp; CO Outlet

My partner and i will not locate virtually any variation among Islam and also Islamic fundamentalists. I really believe religious beliefs could be the main, and also from your main fundamentalism increases being a toxic come. When we all eliminate fundamentalism and also retain religious beliefs, then one evening or perhaps one more fundamentalism can increase once more. I must point out in which due to the fact several liberals constantly guard Islam and also pin the consequence on fundamentalists regarding producing issues. Yet Islam alone oppresses females. Islam alone won't let democracy plus it violates individual legal rights.

Tiffany & CO Outlet

9/22/2011 5:52:27 AM #

Spreed

YESSSSSSSSSS!

Thank you for this post. Has helped solve a huge problem for me.

Spreed

3/7/2012 12:51:02 PM #

Mark Stouffer

Great post. I love your monospace. Congratulations on your VS Achievements too!

Mark Stouffer

Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.



biuquote
Loading