JSON Serialization on Windows Phone 7

I started toying around with creating a HackerNews app for Windows Phone 7 and quickly found out that System.Web.Script.Serialization.JavaScriptSerializer() isn't available on the platform.  Here's how I'm handling it -- make sure you add a reference to System.ServiceModel.Web.

using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Serialization
{
    public static class Json
    {
        public static string Serialize(T obj)
        {
            using (var ms = new MemoryStream())
            {
                var serializer = new DataContractJsonSerializer(typeof(T));
                serializer.WriteObject(ms, obj);
                var json = ms.ToArray();

                return Encoding.UTF8.GetString(json, 0, json.Length);
            }
        }

        public static T Deserialize(string json)
        {
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                var serializer = new DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(ms);
            }
        }
    }
}

Small, but nice iHackerNews update

Over the weekend I was able to add theme support to iHackerNews.com.  The "Dark" and "Light (default)" themes are the only two available, for now.  I'll probably build in a way for others to create their own, but for now I wanted to get the dark theme out since it was requested sometime back.  I also added in functionality to post top level comments on news items.  Before, comments were only allowed as replies.  For logged in users, I’m still contemplating on a way to derive voted items that were voted on outside of iHackerNews without hitting news.ycombinator.com with every transaction.  Maybe keep a short term cache of voted items?

(download)