JSON in C# (Part 1)

Comments [0]

There is something simple about JSON -- not that it can't be complex as well -- but simple data structures (where possible) should always be the goal. I was writing a simple test application for a web service and I wanted to save a history of the parameters I tested and also be able to use a combo box and retrieve past parameters. The web service had four parameters and I wanted to use a simple Javascript or Python style dictionary.

var parameter = {
    grName: "host1",
    grNode: "test",
    template: "$AnalogDevice",
    filter: ""

I looked at two C# possibilities from Jayrock and Newtonsoft, but my web service and test application were both ASP.NET 1.1 at the time and both of these options were for .NET 2.0. So I ended up using XML by modifying SerializableDictionary from Paul Welter for .NET 1.1 and I used an XmlFragmentWriter to write the parameters into the XmlDocument or to extract parameters from the document. The result is not very appealing:

                    <Attribute name="Area" include="1" />
                    <Attribute name="Tagname" include="1" />
                    <Attribute name="Description" include="1" />
                    <Attribute name="DeploymentStatus" include="1" />

I suppose it would have been simpler to use a DataSet, but the whole point of the web service tester was to design an interface that would support multiple web methods that take different parameters that would have a different schema. I could have just written methods to read and write the dictionary keys and values under the SelectNodes("/xml/Parameters") nodes, but there is something appealing about a serialization mechanism. As much as I detested MFC, there was something very nice about the code: ar << m_oObject;  Now mind you, when I was trying to figure out how to make the (typically) binary serialization mechanism in MFC use XML instead (sometime around 1999), I wasn't pontificating on the wonders of MFC! I eventually figured out how to do that but it was a lot more challenging at the time than it needed to be!

Well, unfortunately I haven't had the time to try either of the JSON components, but I will write about it when I get the time. I'm still not sure if I will mix XML and JSON or just use JSON. Since I'm trying to compare, I will probably just use JSON. After all, the point of using XML is to be able to use the myriad of XML options and if you put JSON elements in XML, you've lost the native parsing capability.

            var filter = { grNode: "node", grName: "somename", templateName: "$Well.WellHead.Choke" }

You might as well choose all XML or all JSON:

var Parameters = [
    { grNode: "node", grName: "somename", templateName: "$WellHead.Well.Choke" },
    { grNode: "node2", grName: "somename2", templateName: "$WellHead.Well.Bore" },

Comment Section

Comments are closed.