Website may be up and down over next few months. I'm currently doing a complete overhaul of everything. Going back to simple individual .htm pages, new overall site theme, sanitizing and cleaning up html of all pages and blog posts, attempting to implement a new tooling and publishing system etc etc.

Source –> https://gist.github.com/chkn/4748252

using System;
using System.Linq;

public static class JSON 
{
    public static string Stringify (object obj)
    {
        string str;
        if (obj == null) return "null";
        if (obj is ValueType) return obj.ToString ().ToLowerInvariant ();
        if ((str = obj as string) != null) return System.Text.RegularExpressions.Regex.Escape (str);

        // assume it's a POCO
        return "{" + string.Join (",",
            from p in obj.GetType ().GetProperties ()
            let json = (JSONAttribute) p.GetCustomAttributes (typeof (JSONAttribute), true).FirstOrDefault ()
            where json != null
            select "\"" + (json.Key ?? p.Name) + "\":" + Stringify (p.GetValue (obj, null))
        ) + "}";
    }
}

public class JSONAttribute : Attribute
{
    public string Key { get; set; }
    public JSONAttribute () {}    
    public JSONAttribute (string key) { Key = key; }
}

Add comment



biuquote
  • Comment
  • Preview
Loading






Created by: X

Just another personal website in this crazy online world

Name of author Dean Lunz (aka Created by: X)
Computer programming nerd, and tech geek.
About Me -- Resume