Tao Te KaChing
Workin' the cash register of the Great Tao

JSON.NET, Linq, Extension Methods, and Me...

Greetings!

Been long time no post!  A two-year-old + three-month-old = time bye bye.  Whatever…

I have been doing a lot of web work lately.  One library that has been retardedly fantastic is JSON.NET for all your JSON needs.  If you have love for JSON, you must use this library, because it’s plain tasty.

I found myself needing a quick and dirty JSON diff tool, so I threw down this extension method:

static public JObject JsonDiff(this JObject jobj1, JObject jobj2)
{
    return new JObject(
        (from p1 in jobj1.Properties()
         from p2 in jobj2.Properties()
         where (p1.Name.Equals(p2.Name) && !JToken.DeepEquals(p1.Value, p2.Value))
         select p2).Union(
        from p1 in jobj1.Properties()
        where !(from p2 in jobj2.Properties()
                select p2.Name).Contains(p1.Name)
        select p1).Union(
       from p2 in jobj2.Properties()
       where !(from p1 in jobj1.Properties()
               select p1.Name).Contains(p2.Name)
       select p2).ToArray());
}

It seems to work gravy, but can definitely use more filtering.  I ask any and all for their suggestions.

~zagnut

,,,,,

COMMENTS