Category Archives: .Net

Adventures in JSON parsing with C#

Despite building applications with C# for years, I was frustrated today with some basic JSON parsing. This is something that I have done hundreds of times in Ruby and Python, where parsing JSON consists of deserializing into a Hash (Ruby) or Dictionary (Python) on the fly without defining a class, or mapping, beforehand. It’s easy to build the objects on the fly in the dynamic languages. But, with C# being a statically typed language, it’s taken a long time to get to that point. This becomes important as you consume many JSON endpoints from a service, it saves us time if we don’t have to model all of these responses in C# before making requests. This proved to be an opportunity to catch up with the state of art in C# for dynamically parsing JSON.

Problem

Consume a JSON string (such as the following) in a C# program, hopefully creating some sort of dynamic object on the fly that we can work with like any other C# object, and then doing something with the object: saving it in database, sending it elsewhere or rendering to a screen.


{"results":[
{"employeename":"name1","employeesupervisor":"supervisor1"},
{"employeename":"name2","employeesupervisor":"supervisor1"},
{"employeename":"name3","employeesupervisor":["supervisor1","supervisor2"]}
]}

Step 1 – Parse JSON, dynamically converting it into a C# object

Again, trying to get up to speed and taking the most “modern” approach by dynamically parsing without pre-defining classes or mappings. Looking at some samples, I see mention of a JsonArray type, and a JsonObject in conjunction with the newer .NET Http Client library. But, I cannot seem to use these, and am not sure if they still exist. So, I’m going to quickly switch gears and use Json.NET, since it’s essentially been the de-facto JSON parsing library for .NET applications for years, and that doesn’t seem to be any different now.

I’ve said “dynamic” many times already, and as luck would have it, .NET 4 introduced dynamic types. dynamic types bypass compile time checking. Great, that seems promising. Rick Strahl has a post where he introduces us to Json.NET’s dynamic support, using JObject and JArray

Step 2 – Do something with the C# object

So, great, we have a nice dynamic c# object, but did you notice something in that sample? The last record’s supervisor has disparate value types. This is perfectly valid JSON, per the JSON spec:

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

Nowhere does it say that the value type has to be same for the property across all instances in an array, which if they were all the same type, we’d be done already. So, how do handle this case with our Json.NET JObject instance. As a demonstration, let’s say the requirement is to just take the first one if it’s an array.

Solution

Here’s a little program to demonstrate

Overall, the Json.NET looks like the solution we were looking for, with a concise few lines of code to parse and process the result. It took entirely too long to get to this point though, both in terms of the language and in my work to understand it today. Hopefully we’ll see more acceptance of dynamic parsing in C# and start moving more examples and codebases in that direction.

Coming full-circle with MVC

Interesting how software design repeats itself so much as different groups discover old patterns and make them new again to a whole segment of developers. I’ve been developing for about 8 years, only a fraction in the history of software, but already I’m looking at repeating myself. I’m reading today about the ASP.NET MVC (Model View Controller) announcement from this past weekend. I think it was a little over 3 years ago now that, in my day job, I left Java for .NET. For the bulk of my Java days I was doing MVC development with a custom IBM framework (which I wasn’t too fond of) and with Spring (which I had a lot more fun with). I consider myself pretty agnostic with software, I’m not religiously tied with any group, but I made the switch to .NET because I wanted to write web apps, and .NET was the direction my company’s technology roadmap was going, leaving Java mostly behind as a web front-end.

Thanks to Rails generating an increased buzz in the .NET community about MVC, Microsoft is now going to ship its own MVC framework as an extension to ASP.NET. I’ve been doing traditional ASP.NET and ASP.NET with the Model View Presenter (MVP) pattern now for 3 years and am not a big fan of it’s workings, with viewstate, postbacks, controls and event lifecycle. Too often I find myself having to work too hard to do fit what I’m trying to do into the framework. The web is a simpler medium, and I like the software stacks that web apps are written to be simpler too. I’m looking forward to exploring this new framework more along with Rails and MonoRail.

Log4Net problem finally solved (I think)

For the longest time I’ve been aggravating over a Log4Net problem using the RollingFileAppender with a RollingStyle of “Date”. This is probably the most popular style of logging, with the log file rolling over every day. But, I could never get it to work consistently. Log4Net would always start the log file over each day, but would not always archive the previous day’s log. End result being the entire log from the day before was gone! Not the best result. It would archive about once a week, so that meant this wasn’t a permissions issue. This had been going one for a year or so after an upgrade to a NHibernate 1.0 release, previous to that, everything was fine.

With the NetworthIQ move to a new, dedicated server last month, I was able to move the log directory outside of the web root (yeah, I know, you shouldn’t have logs in your web root, but they were protected). All of a sudden, the rolling is working again, with no lost logs. I guess there’s a conflict between IIS and Log4Net somewhere, maybe someone knows the exact reason, but for now I’m happy to have it working.

Thank you programming book gods

Yes! Somebody wrote a book about NHibernate. I can’t tell how woefully lacking NHibernate is on helpful documentation and articles. Couple that with the little time I have to work on building out my app and I am not nearly as efficient as I could be. I hope this will help. Maybe now I’ll be able to figure out how to delete a persistent object without breaking into plain old SQL and ADO.NET.

NHibernate in Action

(via Ayende)

Lesson learned for Visual Studio and Web Deployment Projects

In my jump into using Web Deployment Projects with Web Application Projects I ran into an issue which is rather obvious after the fact, but thought it would be good to share for Google searchers.

When creating a new solution/project configuration (i.e. dev, test, production, etc.) in Visual Studio 2005, which is useful to do when using Web Deployment Projects, make sure you set the output path for the Web project to “/bin”. By default a new configuration gets an output path of “/bin/[configuration]“. If the web assembly is not in /bin, the aspnet compiler portion of the Web Deployment project will fail with errors like this:

error ASPPARSE: Could not load type ‘code behind class’

To .Net or not to .Net

Over at Web 2.0 Central, Reg has an interesting post on the Top 13 reasons to CONSIDER the Microsoft platform for Web 2.0 development. I especially liked the disclaimer. Not that I question Reg’s objectivity, but it sounds like Microsoft showed him a pretty good time. Maybe I’m just jealous :-)

This seemed like a good topic to get me back into the blogging spirit. Since I’m working on a Web 2.0 app (NetworthIQ) using the Microsoft platform, I’ve got some thoughts on the subject.

1. Free Developer Tools.

Over the last month I’ve been migrating NetworthIQ from 1.1 to 2.0 using the Express editions. We signed up for the Empower program thinking it was the greatest thing ever. All the Microsoft software you want for a few hundred bucks a year. How can you beat that? Well, you know what they say, if it sounds too good to be true, it probably is. The catch is you have to build an installable application, then make an effort to get certfied by Microsoft, among other requirements. A public web site, even one you charge for doesn’t make the cut. We could build an installable client app or something to satisfy the requirements, but that’s too much of a distraction, and frankly, after spending the last 18 months doing it at my day job, I don’t like (building or using even) installed apps very much. Now, since we’re disciplined about separating work from side-company, we need to use our own tools. Free is about all we can justify right now, so the express versions fit the bill perfectly.

Going from full Visual Studio to express isn’t the most fun, but it’s a workable solution short of switching platforms. My issues with it are:

  • Being limited to a single web site solution sucks. we have to run two IDEs, using VC# express for our non web code (domain, data layers, etc.). App_Code was just too full and I didn’t see an easy way to embed NHibernate xml files into the generated assembly.
  • The debugging is a pain since you can’t just attach to a process.
  • The publishing options are lacking and the Web Deployment Projects don’t support express. However, we still have the power of MsBuild, so that one’s not too big of a deal.

However, I am really enjoying ASP.NET 2.0, and am starting to become really productive with it (it helps that I’m now doing it at the full-time job as well). VWD Express satisfies me enough for now. Plus, based on what I’ve used for PHP development it’s pretty darn good (I don’t know enough about developing/debugging tools for Rails to comment at this time).

2. Free Database

I haven’t spent a lot of time with SQL Express, but I am happy it’s free and it’s definitely aimed at MySQL. Competition is a good thing.

3. Microsoft Atlas makes AJAX easier.

I spent some time with Atlas at work over the last few weeks, developing a proof of concept app for the project I’m on. I wasn’t too impressed. To me, it’s buggy, bloated, and poorly documented. But, I do like the Web Service proxying that makes it easy to get data back and forth. I know it’s a long way from release, but for me, it’s not usable yet.

4. Microsoft doesn’t HAVE to get ALL of your business.

I don’t see many if any LAMP shops using Atlas.

5. Microsoft solutions can scale.

It’s definitely reassuring to know MySpace is using .NET. But, why does it look like they’re still using ColdFusion? Are all 1.5 billion page views on .NET?

6. Microsoft pricing is flexible.

This will be the breaking point for us I think. If and when we have to start scaling, are we going to want to pay the Microsoft tax for Windows and possibly SQL Server licenses? It remains to be seen, but there’s a reason we’re playing with Rails.

7. Ray Ozzie

Not a factor for me. Maybe I’m ignorant, but I haven’t even bothered learning about SSE and Live Clipboard. Should I?

8. Robert Scoble

I like Robert and have read him for a long time. He even posted about NetworthIQ. I agree that he’s doing a lot of good for Microsoft.

9. Being based on the Microsoft platform doesn’t limit your acquisition options.

Glad to know :-). But seriously, I wouldn’t pick a technology for this reason. Pick something that you can build a great product with, FAST.

10. Microsoft wants to be a part of the community

Ok.

11. Microsoft employees aren’t evil.

I’m definitely not a rah-rah Microsoft developer. I hate IE (ok hate is a strong word, perhaps saying I prefer firefox is better), and most of the Live.com stuff is crap. But, I like ASP.NET and I know there are people working hard there and reaching out to be better. Scott Guthrie comes to mind as someone doing great work for ASP.NET.

12. Microsoft has good development resources

For getting up and running this is pretty true. However, there are ongoing wars between the RAD (everything’s declarative) camp and the non RADs. Just hope you don’t get caught between the two when trying to figure out a good solution to a particular problem.

13. Microsoft speeds web application development

Ahh yes, this is the clincher for me (for now). Sometimes ASP.NET can be terribly frustrating and feels like it’s getting in the way when you want to do something simple. It produces crappy markup at times that is difficult to control, and you have to constantly be aware of ViewState (page size) issues. But, in the end, once you get up to speed, it’s a nice framework.

Hmmmm….. what?

I was reading an interview at SitePoint with Dino Esposito, an “ASP.NET Expert.” I know he has written several books (apparently well rated and sold), and I’ve read several articles by him before. But this comment scares me a bit:

“I don’t write much real-world code. But automated testing is key. Period.”

So, he’s an “Expert,” but doesn’t write much real-world code? Should we be looking elsewhere for ASP.NET 2.0 books? I generally like to learn from someone who has been there, done that. Since I don’t like Wrox books much (too big and disjointed usually), I’ve avoided their book. Hopefully, Fritz Onion’s book will be out sooner rather than later.

Cool .Net products/companies

In the spirit of trying to be more positive about Microsoft on this site, I came across this list of “cool” .Net products/companies by Dan’l Lewin (via Scoble). I’ve only heard of a couple of them, but will be checking the others out. There’s also an intersting list of reasons for choosing .Net at the end of the article. I’m sure the open source crowd will have something to say about that.

But, there was one glaring omission. NetworthIQ was missing from the list :-).