Introducing .JSON, 500 lines of dynamically typed JSON goodness

image

Ever wish you could work with JSON in .NET as you normally do with regular old objects? Well, now you can:

string url = "https://github.com/api/v2/repos/show/kamranayub";
dynamic[] repos = JsonService.GetFrom(url).repositories;

return View(repos.OrderByDescending(r => r.watchers);

Enter .JSON, a single file that holds 2 classes you can use to manipulate JSON in a dynamic way, either from web services, strings, or objects.

dynamic json = Json.Parse("{'foo':'bar','baz':['a','b','c']}");

string foo = json.foo;
string[] = json.baz;

Inspired by Massive, I decided this would be a useful tool for the community, seeing as all the existing JSON libraries for .NET are not exactly what I'd want when dealing with JSON data.

I wanted several things from this:

  • Ease of use
  • LINQ
  • Dynamically typed
  • GET, POST, and authentication support
  • One file
  • Minimal dependencies (there's one for using Microsoft's built-in serialization/deserialization of JSON but it's GAC'd at least).

I am happy to say that as it stands, I've accomplished all of them. If you spot bugs, open an issue or patch it. I plan to refactor it a little to support a Fluent interface, but other than that, it pretty much just works.

Happy JSON'ing!

You calling me fake, boy-o? [Spam]

I saw this comment on my Code Canyon item today (editorialized):

You stole this from so-and-so's book, it's almost exactly the template they provide, blah blah, more false accusations.

I offer you, accusation-spewing fiend, some proof that I own this code:

image

Is their template laid out exactly like this? Doubtful considering I made it.

image

Yep, looks like my code alright…

image

Hey look, API documentation for everything I made?

I don't know what template you were looking at, but I'll give you $1000 that it doesn't have any of those functions. All of these helpers have been functions I've made in the past during my years as a programmer. Hell, I probably can dig up a lot more.

Anyway, I don't need to prove myself to you. It's not fake, I worked on it for about a week, and I should really update it, thanks for reminding me. It's just a template that helps people get up and running a bit faster during development, the helpers are a useful add-on.

If you don't like it, use Sharp Architecture. It's probably more up-to-date now, anyway! I have been considering taking my template off Code Canyon anyway in favor of creating a new, free template on GitHub and keeping just packaged snippets to being sold. Mine is woefully out-of-date with best practices (LINQ to SQL??? No DI containers??? The horror!).

Good day to you, sir.

Making jQuery Play Nice with XML [Project]

tl;dr I've made a plugin for jQuery that makes it easy to work with XML natively in jQuery. Get the code from my GitHub.

I've been working on a project that has required me to parse and manipulate XML on the client-side. Since I was using jQuery on the project, it made sense to leverage its cross-platforminess and easy coding style to manipulate the XML.

Unfortunately, jQuery hasn't really buried its claws very deep in the XML realm because if you've tried doing anything with XML you've probably run into issues. If you don't care about IE, you'll probably have less issues, but in my case, I had to support IE and even with vanilla jQuery, some things still don't work in other browsers.

In plain old Javascript, you can manipulate XML in IE using ActiveX and the MSXML object. However, it's IE-specific and it lacks the ease of use of jQuery. I wanted a way to leverage jQuery and also support multiple browsers.

So, I've created a helper function (plugin, really?) that helps jQuery deal with the XML and any nuances it has (such as text vs. CDATA nodes, etc.).

You use it just like you normally would with jQuery with one exception:

var $xml = $.xml("<your_xml_string />");

You have to use the function I created to instantiate it.

Other than that, I've tried to make it as seamless as possible, though I have yet to test every use case. I got it far enough that it works for my project, even doing some advanced selectors and manipulation. Webkit has a bug with the appending but I'm hopeful when I sit down and look at it it'll be an easy fix. I seem to have fixed the Webkit bugs.

There are two XML-specific functions you can use when dealing with my plugin as well:

// 1. .cdata()

// Get CDATA, which will return non-encoded data
$xml.find("mynode").cdata();

// .html() has been redirected to use CDATA on XML documents
$xml.find("mynode").html();

// To set CDATA, use the cdata function
$xml.find("mynode").cdata("<strong>html</strong>");

// 2. .xml()

// Use .xml() to get the outer XML of the element
var wholeDocument = $xml.xml();

var nodeXml = $xml.find("mynode").xml();

I've posted it on GitHub for a reason. I don't exactly plan to add anything extra unless it has a bug I directly deal with in my project (after all, I made it to get the job done). However, you can totally make it better if you feel like it and I'll merge your changes to the trunk. Fork it, pull it, whatever, I welcome any additions and help.

An error has occured while trying to process your request. Hide