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 {
    [JsonProperty("myString")]
    public string MyString { get;set;}
    [JsonProperty("myInt")]
    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();

Fun with MVC Action Filters

Today while working in an MVC web application I ran into a scenario where I needed to authorize access based on an internally developed authorization system.

At first I thought about overriding the OnAuthorization() method in the controller which I’ve done in the past, but then I realized that the permissions could be different based on the action which is invoked on the controller. Because of this, something on the controller level may have been too restrictive, 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

Mobile apps, something new

This week I began coding on my first mobile app.  The first obstacle I found of course is to pick the technologies to use.  At this point I know I want to target the IOS and Android platforms for the app. This presents some difficulties because of the different languages used to write applications for these systems.

After some research, I was able to get information on using html 5 to create an app that will work across platforms. This is great actually because I did not want to have to maintain multiple versions of the same application.  So after some thought and considering the user requirements I decided to move forward with html 5.

Next up is design in which I found the jQuery mobile framework. This is great actually because I have been using jQuery and jQuery ui for some time. So this will help me move things along faster. Along with this, I also started looking into the phonegap framework which would allow for the application to be installed like a native application.  And best of all, phonegap allows for a single code base that is then ran on both iOS and Android.  Problem solved!

At this point I am in the testing phase and working through the ruff models.  Look for another post in the coming weeks with my findings once I have been able to dig into the different frameworks in more detail.