Most .Net programmers who need to use JSON can either pull in a third-party library or are using .Net 3.5 and therefore can rely on the built-in JavascriptSerializer . However, I recently found myself needing to turn a .Net DataTable into JavaScript Object Notation without either of those options available.
My first thought was that something must already be out there that I could use. Indeed, there are some really great libraries for this. Unfortunately, my situation is such that I can't pull in a whole library. I needed some purpose-specific code. I did find some specific implementations as well, but I found them all some how lacking and ended up rolling my own. The
code itself would take up about 2 printed pages, so I'll content myself with posting the link for now.
Instead, this leaves me space to talk about naming. Notice that I titled the post "DataTable To JSON". If you read the code itself you'll find I actually turned that name around: "JSONHelper.FromDataTable". In the past it's always been more natural for me to think in terms of transforming type 1 to type 2, and names used in my code prior to this reflect that.
However, I recently found this
post about naming conventions on StackOverflow, written by no less a personage than site co-founder
Joel Spolsky. He makes a very good case for the "Type2FromType1" syntax: namely it puts the return type next to the variable receiving it and the source type next to the parameter accepting it, rather than the complete opposite. I decided to try this for a while, and while it felt awkward at first it's growing on me very quickly.