JSON standards vs C# naming standards

Anyone that has coded in C# for any amount of time is accustom to seeing property names in Pascal case such as the example below…

public class MyClass {
    public string MyString { get;set; }
    public int MyInt { get;set; }

Which will serialize the class as follows…

{ "MyString":"hello world","MyInt":1 }

Some of your JSON gurus would tell you that the properties for a JSON object should really be in camel case instead of pascal (examples). So we are in-between worlds here trying to find something that will work for everyone.

Our first option would be to use the JsonPropertyAttribute to specify the name for the property during serialization such as below…

public class MyClass {
    public string MyString { get;set;}
    public int MyInt { get;set; }

Option two would be to make a change in the serialization options such as below…

    MyClass obj = new MyClass()
        MyInt = 1,
        MyString = "hello world";
    var jsonSettings = new JsonSerializerSettings
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    string json = JsonConvert.SerializeObject(obj, Formatting.Indented, jsonSettings);

Both of which will give us our desired output of…

  "myString": "hello world",
  "myInt": 1

This may be most useful when working within the WebAPI technology of asp.net. If you would like to set the serializer to camelcase for the entire api, you would would need to add the below line to your Application_Start() method within your global.asax

            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

Memory is great, till you run out

So this morning I had an issue at work where a process kept throwing a System.OutOfMemoryException. We checked out the virtual machine, and seen that the memory being used was about 75-80% of what was available, but that was about it. This had us a bit confused until we looked at the log and seen the exception. Continue reading

Mobile Feed Reader App – Part 2: Create RSS reader

This is the second part to the mobile application posts I had been working on.  Look for another post to come soon regarding testing and debugging as that certainly was a challenge due to the amount of javascript that was used to make this work.  You can view the previous post on this topic here.  Now back to part two…

To review, we had left off in part one where we had our IDE (Eclipse – ADT) setup and configured with a new project loaded and ready.  After about a week of playing with it on and off as time allowed in the evenings, I now have a working rss reader for Android based on PhoneGap.  As I mentioned before, this used HTML5, CSS3, and jQuery Mobile all running on top of the Cordova (PhoneGap) framework.

I will start by breaking out into the directories that house the different files that had to be modified or created for this to work from top to bottom in the project…

  • assets/www (this houses all the HTML, CSS, and JavasSript for the project).
  • res/values (strings.xml was updated with the application name to be displayed).
  • res/xml (config.xml was updated to include the domain of our RSS feed as a whitelist domain).

Continue reading

Mobile Feed Reader App – Part 1: Environment & Project Setup

A few weeks ago I started to post regarding the building of my first mobile app. After working with jQuery Mobile some and getting a good feel on its capabilities I started to move forward.  For part one of this series I am going to step through the creation of the PhoneGap Android project and basically getting the project setup with the jQuery Mobile framework and getting it ready to work with.

First off, we need to make sure that we have the appropriate software installed onto the machine.  The list of software we will need includes the following

  • Android ADT – This is a bundle that includes a version of Eclipse, Android SDK,  Android platform and tools, as well as the system images for emulator.
  • Latest version of PhoneGap
  • Apache Ant

After all that software is installed, you will also want to add a package to the Eclipse IDE so that we can better edit HTML files.  To do this, open Eclipse and in the top menu navigate to Help->Install New Software.  Once this loads in the work with drop down box, select the Juno – http://download.eclipse.org/releases/juno option.  In the filter box, type in Web Developer and you will notice the Eclipse Web Developer Tools package.  Check this option and then follow the prompts to install it.

Continue reading