Thursday, September 20, 2012

Convert JSON to .Net Entities – Alternate Way


Often, there are multiple ways to do the same thing in code.

In my previous post, I demonstrated how to convert JSON to .Net entities using the classes in System.Runtime.Serialization and System.Runtime.Serialization.Json namespace.

The alternate is to use classes in Windows.Data.Json namespace.
http://msdn.microsoft.com/en-us/library/windows/apps/windows.data.json.aspx

I came across this style while doing the Contoso Cookbook lab examples, which has the original code.

Below is the code snippet of converting the JSON String to .Net Entitity classes. This example uses the entity classes from previous example. However, it populates each and every property by individually assigning it values. While its more lines of code than the previous example, it can be useful when your .Net Class structure differs from the JSON structure.
Namespaces you need to refer:
using Windows.Data.Json;

Code:
string jsonFriendsString = "{\"data\":[{\"uid\":560275290,\"first_name\":\"Vinay\",\"last_name\":\"Bhatia\",\"pic_square\":\"http://profile.ak.fbcdn.net/hprofile-ak-snc7/371731_560275290_925165134_q.jpg\",\"pic_big\":\"http://profile.ak.fbcdn.net/hprofile-ak-snc7/371731_560275290_925165134_n.jpg\",\"timezone\":null,\"current_location\":{\"city\":\"Singapore\",\"country\":\"Singapore\",\"zip\":\"\",\"id\":101883206519751,\"name\":\"Singapore, Singapore\"}},{\"uid\":702608824,\"first_name\":\"Santosh\",\"last_name\":\"Singh\",\"pic_square\":\"http://profile.ak.fbcdn.net/hprofile-ak-ash4/275768_702608824_1698652029_q.jpg\",\"pic_big\":\"http://profile.ak.fbcdn.net/hprofile-ak-ash4/275768_702608824_1698652029_n.jpg\",\"timezone\":null,\"current_location\":{\"city\":\"Edison\",\"state\":\"New Jersey\",\"country\":\"United States\",\"zip\":\"\",\"id\":109295752421654,\"name\":\"Edison, New Jersey\"}},{\"uid\":100001987004026,\"first_name\":\"Mayur\",\"last_name\":\"Udernani\",\"pic_square\":\"http://profile.ak.fbcdn.net/hprofile-ak-prn1/174061_100001987004026_5522672_q.jpg\",\"pic_big\":\"http://profile.ak.fbcdn.net/hprofile-ak-prn1/161176_100001987004026_3945963_n.jpg\",\"timezone\":null,\"current_location\":{\"city\":\"London\",\"state\":\"England\",\"country\":\"United Kingdom\",\"zip\":\"\",\"id\":106078429431815,\"name\":\"London, United Kingdom\"}}]}";

RootObject fbFriends = ConvertJSONToEntity(jsonFriendsString);
 
//Get Friends List
var friendsList = fbFriends.data;

//Loop Through Friends List
foreach (Datum friend in friendsList)
{
       //Do Something
}
 
Convert JSON to .Net Entities:
private RootObject ConvertJSONToEntity(string JSONString)
{
            //Create .Net Entity Classes to populate
            RootObject fbFriends = new RootObject();
            fbFriends.data = new List<Datum>();

            //Parse the JSON String to an JSON Object
            JsonObject objJSON = JsonObject.Parse(JSONString);

            var objElement = objJSON.ElementAt(0);

            JsonArray array = objElement.Value.GetArray();

            foreach (var item in array)
            {
                Datum friend = new Datum();

                var obj = item.GetObject();

                foreach (var key in obj.Keys)
                {

                    IJsonValue val;
                    if (!obj.TryGetValue(key, out val))
                        continue;

                    switch (key)
                    {
                        case "uid":
                            friend.uid = (long)val.GetNumber();
                            break;

                        case "first_name":
                            friend.first_name = val.GetString();
                            break;

                        case "last_name":
                            friend.last_name = val.GetString();
                            break; 

                        case "pic_square":
                            friend.pic_square = val.GetString();
                            break;

                        case "pic_big":
                            friend.pic_big = val.GetString();
                            break;

                        case "timezone":
                            var objTimeZone = val.ValueType;
                            if (objTimeZone != JsonValueType.Null)
                            {
                                friend.timezone = val.GetObject();
                            }
                            else
                            {
                                friend.timezone = "";
                            }

                            break;

                        case "current_location":
                            JsonObject loc = val.GetObject();
                            CurrentLocation curLoc = new CurrentLocation();

                            curLoc.city = loc.GetNamedString("city");
                            curLoc.country = loc.GetNamedString("country");
                            curLoc.zip = loc.GetNamedString("zip");
                            curLoc.name = loc.GetNamedString("name");
                            curLoc.id = loc.GetNamedNumber("id").ToString();

                            friend.current_location = curLoc;
                            break;
                    }//End Switch

                }//End Inner For Each keys

                //Add Object to List    
                fbFriends.data.Add(friend);

            }//end outer foreach

            //Return List of Objects
            return fbFriends;
        }

Saturday, September 15, 2012

Convert JSON to .Net Entities


JSON v/s XML:

As I mentioned in my previous blog post, JSON has emerged as the new serializable data transfer  format while writing Web Services in both the Enterprise and the Consumer world. Compared to XML, JSON is light weight and hence fast, when transmitted over the wire. Search for "JSON v/s XML" on the internet using your favorite Search Engine and you will find a number of blogs on this topic. This blog is not about JSON v/s XML.

This blog will discuss how to convert a JSON sting into .Net Entities.
 
Why do we need to convert JSON to .NET Entities?
JSON is normally used with JavaScript. However, it can also be used with .Net.
.Net code can also consume JSON Web Services.
In my case, I was writing a .Net App (Win 8 Metro Style) using XAML/C# and calling Facebook Web Services using the Facebook Query Language(FQL). FQL returns data in JSON format, and I had to convert to .Net.
 
How do we do it?
Step 1: Create equivalent .Net Classes
Step 2: Deserialize  JSON data to .Net Objects
 
How do we really do it?
Step 1: You can create classes  by hand, however, I will suggest using this handy online tool that convers JSON to .Net classes
Note: You need to add DataContract and DataMember attributes to generated class

Input JSON String

{"data":[{"uid":560275290,"first_name":"Vinay","last_name":"Bhatia","pic_square":"http://profile.ak.fbcdn.net/hprofile-ak-snc7/371731_560275290_925165134_q.jpg","pic_big":"http://profile.ak.fbcdn.net/hprofile-ak-snc7/371731_560275290_925165134_n.jpg","timezone":null,"current_location":{"city":"Singapore","country":"Singapore","zip":"","id":101883206519751,"name":"Singapore, Singapore"}},{"uid":702608824,"first_name":"Santosh","last_name":"Singh","pic_square":"http://profile.ak.fbcdn.net/hprofile-ak-ash4/275768_702608824_1698652029_q.jpg","pic_big":"http://profile.ak.fbcdn.net/hprofile-ak-ash4/275768_702608824_1698652029_n.jpg","timezone":null,"current_location":{"city":"Edison","state":"New Jersey","country":"United States","zip":"","id":109295752421654,"name":"Edison, New Jersey"}},{"uid":100001987004026,"first_name":"Mayur","last_name":"Udernani","pic_square":"http://profile.ak.fbcdn.net/hprofile-ak-prn1/174061_100001987004026_5522672_q.jpg","pic_big":"http://profile.ak.fbcdn.net/hprofile-ak-prn1/161176_100001987004026_3945963_n.jpg","timezone":null,"current_location":{"city":"London","state":"England","country":"United Kingdom","zip":"","id":106078429431815,"name":"London, United Kingdom"}}]}

 
Output of json2csharp.com:

public class CurrentLocation

{
    public string city { get; set; }
    public string country { get; set; }
    public string zip { get; set; }
    public object id { get; set; }
    public string name { get; set; }
    public string state { get; set; }
}

public class Datum
{
    public long uid { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string pic_square { get; set; }
    public string pic_big { get; set; }
    public object timezone { get; set; }
    public CurrentLocation current_location { get; set; }
}


Namespaces you need to refer:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;


Classes with required Attributes:

 [DataContract]
    public class RootObject
    {

        [DataMember]
        public List<Datum> data { get; set; }
    }


[DataContract]
public class CurrentLocation
{
 [DataMember]
    public string city { get; set; }

 [DataMember]
    public string country { get; set; }

 [DataMember]
    public string zip { get; set; }

 [DataMember]
    public object id { get; set; }

 [DataMember]
    public string name { get; set; }

 [DataMember]
    public string state { get; set; }

}

[DataContract]
public class Datum
{

 [DataMember]
    public long uid { get; set; }

 [DataMember]
    public string first_name { get; set; }

 [DataMember]
    public string last_name { get; set; }

 [DataMember]
    public string pic_square { get; set; }

 [DataMember]
    public string pic_big { get; set; }

 [DataMember]
    public object timezone { get; set; }

 [DataMember]
    public CurrentLocation current_location { get; set; }

}


Convert JSON to .Net Entitiies:

Now that you have your .Net classes ready, converting JSON to .Net using built in classes of Win RT is only a few lines of code.  I wrote this piece of code around 3 months back, when Window 8 RT was in consumer preview and there was no good documentation on how to use the classes.
 
 Here is an end to end code, in case want to debug it and test it out within a .Net application.

//In real world, populate jsonFriendsString by making a web service call.

 " has been escaped as \" in the code below. You wont need the escape character while consuming  from web services.

            string jsonFriendsString = "{\"data\":[{\"uid\":560275290,\"first_name\":\"Vinay\",\"last_name\":\"Bhatia\",\"pic_square\":\"http://profile.ak.fbcdn.net/hprofile-ak-snc7/371731_560275290_925165134_q.jpg\",\"pic_big\":\"http://profile.ak.fbcdn.net/hprofile-ak-snc7/371731_560275290_925165134_n.jpg\",\"timezone\":null,\"current_location\":{\"city\":\"Singapore\",\"country\":\"Singapore\",\"zip\":\"\",\"id\":101883206519751,\"name\":\"Singapore, Singapore\"}},{\"uid\":702608824,\"first_name\":\"Santosh\",\"last_name\":\"Singh\",\"pic_square\":\"http://profile.ak.fbcdn.net/hprofile-ak-ash4/275768_702608824_1698652029_q.jpg\",\"pic_big\":\"http://profile.ak.fbcdn.net/hprofile-ak-ash4/275768_702608824_1698652029_n.jpg\",\"timezone\":null,\"current_location\":{\"city\":\"Edison\",\"state\":\"New Jersey\",\"country\":\"United States\",\"zip\":\"\",\"id\":109295752421654,\"name\":\"Edison, New Jersey\"}},{\"uid\":100001987004026,\"first_name\":\"Mayur\",\"last_name\":\"Udernani\",\"pic_square\":\"http://profile.ak.fbcdn.net/hprofile-ak-prn1/174061_100001987004026_5522672_q.jpg\",\"pic_big\":\"http://profile.ak.fbcdn.net/hprofile-ak-prn1/161176_100001987004026_3945963_n.jpg\",\"timezone\":null,\"current_location\":{\"city\":\"London\",\"state\":\"England\",\"country\":\"United Kingdom\",\"zip\":\"\",\"id\":106078429431815,\"name\":\"London, United Kingdom\"}}]}";

 

            //Start JSON DeSerializaton

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RootObject));

 
            //Convert string to stream
            //GetyBytes method is used to to create a byte array
            byte[] byteArray = Encoding.Unicode.GetBytes(jsonFriendsString);

 
            MemoryStream stream = new MemoryStream(byteArray);

            stream.Position = 0;


            //DeSearialize Stream to Object
            RootObject fbFriends = (RootObject)serializer.ReadObject(stream);

            //End JSON DeSerializaton

 
            //Get Friends List
            var friendsList = fbFriends.data;

            //Loop Through Friends List
            foreach (Datum friend in friendsList)
            {
                //Do Something
            }

Any Other Options?
Part 2: In my next blog post, I show an alternate way of doing the same thing.
http://vinaybhatia.blogspot.in/2012/09/convert-json-to-net-entities-alternate.html 

Wednesday, August 29, 2012

Mirror Mirror on the Wall, what is going to be the strongest programming languages of all?

Prediction:

JavaScript in going to cross its current boundaries of being used only in Web Platforms. It will be used in many more scenarios than what it is being used today.

JavaScript is used in combination with other related technologies like HTML, jQuery, JSON, CSS and AJAX.  So it’s not just JavaScript, but all of the related technologies that will have more important role in the future.
For the rest of the blog, when I say JavaScript, I mean JavaScript and the related technologies.

On what basis I make the statement?

I have been working on the new Microsoft products released this year, and observing the trend. Below are some examples of how things are changing with the new releases.

Windows Applications World:
Traditional Win Form Apps were developed in VB.Net or C# as the programming language. The front end used was Win Forms or XAML/Silverlight. With Windows 8 Metro Style Apps, there is a new alternative. The Apps can be developed in either XAML & C++, XAML  & C# or HTML5  & JavaScript.  JavaScript was so far used only in Web Applications, so this comes as a radical shift. It’s being used in a non-web based platform.

SharePoint Customizations/Apps:
SharePoint Customizations were typically done in C# (very few people used VB.Net).  There was an alternative approach called “No Server Code” development, or “Middle tier approach to SharePoint Development”. This approach used SharePoint Designer, JavaScript, XML/XSLT without requiring any custom code or solution packages to be deployed on the server.  While the approach had its benefits (and I led one of the largest and the best team of SharePoint ‘No Server Code’ developers), its adoption in the industry was limited.

In MOSS 2007, to call the SharePoint Web Services, we initially wrote custom code and later used third party JavaScript libraries like Darren’s SPAPI.js or Marc Anderson’s SPServices.js. 

With SharePoint 2010, the approach got a big push with the inclusion of JavaScript Client Side Object Model(CSOM).  SharePoint 2010 CSOM had some limitations, which have been overcome in SharePoint 2013 CSOM.

SharePoint 2013 also introduces a new way of developing customizations called SharePoint Apps. The Apps use JavaScript CSOM to interact with data stored in SharePoint (though C# or even LAMP can be used for SharePoint apps).   Sandbox Solutions and Farm Solutions are still supported and will continue to be developed in C#.  
SharePoint is a Web Platform, and JavaScript was always used since the beginning.

With SharePoint 2013, JavaScript plays a very important role and the industry is likely to use more of JavaScript than C# for customizations.

 

Microsoft Office Customizations:

Office Macros were traditionally written in VBA. With Visual Studio Tools for Office(VSTO), we saw VB.Net being adopted. VB.Net was more widely used than C# in the Office Customization space as VB.Net is the logical upgrade path for VBA Code and also VBA developers.

The Office 2013 Apps (also referred as Agaves) are now developed in HTML5 and JavaScript. Yes, you read it right; JavaScript enters the Office 2013 Apps space.

Other factors:

Mobility:
If you choose to develop mobile apps in native code, you need to develop a different app for each of the platforms (Windows Phone, iOS and Android). However, if you develop a browser based application, it can be viewed on the Mobile Browsers for all the modern mobile devices and tablets. So mobility is a big driver for JavaScript emergence.

Web Services:

JSON is lightweight and has performance benefits (compared to XML) when transmitted over the wire.  The modern web services return data in JSON format, not XML (e.g Facebook API) . Some like built-in SharePoint Web services return data in both the formats, and let the consumer decide.

Mirror Mirror on the Wall, which is going to be the strongest programming languages of all?
The writing is on the wall. JavaScript and related technologies are going to play an increasingly important role (compared to what they do today) in the future of application development on Microsoft and other platforms.  As the Win 8 Apps or Office 2013 Apps examples show, JavaScript is no longer restricted to web development. It is becoming omnipresent. It is emerging the strongest programming language. 

What is my advice?
If you are in the Software industry, and don’t know JavaScript and the related technologies that I earlier mentioned, prioritize learning these skills over everything else. You will be doing yourself a lot of good. It’s my advice to students, fresher's and experienced software developers.

What if what Vinay Bhatia says does not turn out to be true?
Standard Disclaimer: It’s based on my observations and my personal opinions, and if it does not turn out to be true, don’t sue me. However, a few years down the line,  if my advice helps you move ahead of the competition, do let me know and buy me a drink.

What about other non-Microsoft platforms?
I work in the Microsoft Technologies space, and my views are based on the changes that I am seeing in the way we develop in Microsoft technologies.  If you work in non-Microsoft space, do let me know your views by leaving a comment.

Tuesday, August 07, 2012

Visual Studio 2012 for SharePoint 2010/2013

Lot of developers are still using Visual Studio 2010 for SharePoint 2010 Development , and not taking advantages of the new features in Visual Studio 2012.

Visual Studio 2012 has a number of new features which will improve the productivity of SharePoint 2010 and 2013 Development teams.

For example, there is a new list  and content type designer in Visual Studio. Earlier, the same required writing CAML/XML with hand. The new designer  UI makes it much easier.

Visual web parts can now be created as a Sandbox Solution. The same was also possible earlier using Power Tools, but Visual Studio 2012 has it built in.

For a complete list of new features in Visual Studio 2010 for SharePoint Developers, visit the MSDN link:
http://msdn.microsoft.com/en-us/library/ee290856(v=vs.110)

Visual Studio 2012 can be used for SharePoint 2010 also. There seems to be misconception that the new features are available only for SharePoint 2013. What is being missed is that these features are in Visual Studio, and available to both SharePoint 2010 and SharePoint 2013.

Below are some screenshots of how SharePoint 2010 teams can benefit from the new features in Visual Studio 2012 on a SharePoint 2010 machines. The UI is friendly and self-intuitive. The screenshots should be self-explanatory to those having SharePoint background. Sharing on my blogpost so that you can evaluate using Visual Studio 2012 for your SharePoint 2010 (and 2013) projects.


Screenshots:

SharePoint 2010 Visual Web Part Project Template:

Select Sandbox or Farm Option for Visual Web Part:



Adding Site Column, Content Type, List to SharePoint:


Create Content Types:


Content Type Wizard:



Create a New List:


Options while creating a new list:


Add Site Column to List:



Add Content Types to List:



If required, the CAML generated using the UI can then be modified manually.

Wednesday, July 18, 2012

SharePoint 2013 is here


There were couple of announcements from Microsoft yesterday related to new version of Office and Office offerings. But what I am most excited about is the one on Vesku’s blog related to SharePoint 2013 being made publicly available.



Finally, the long wait is over and SharePoint 2013 is in the public domain.

SharePoint v15 is now officially being called SharePoint 2013. There are tons of presentations and videos which have been publicly released yesterday. 
Those who are hands on types and believe in getting hands dirty, have few options:

While I plan to install SharePoint 2013 on a local machine in near future, for now, I have signed up with Office 365 Developer Site account and exploring the new features of SharePoint 2013.

Wednesday, June 20, 2012

Introducing SQLite

During the Windows 8 Metro App demos, there have been questions on SQLite. Until I started using it for my Windows 8 App, even I was not aware of the gem called SQLite. Thought of blogging additional information for everyone.

What was my requirement?

A lightweight database to store my contacts information for my Win 8 App. Need to ensure database is present on every machine or tablet which uses the app, so SQL Server / Oracle were immediately ruled out. Need an database which is lightweight, easy to install, and there are no licensing implications.

What did I use?

SQLite

What is SQLite?

SLite.Org describes it as follows: SQLite is a in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. SQLite is currently found in more applications than we can count, including several high-profile projects.

SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process. SQLite reads and writes directly to ordinary disk files. A complete SQL database with multiple tables, indices, triggers, and views, is contained in a single disk file. The database file format is cross-platform - you can freely copy a database between 32-bit and 64-bit systems or between big-endian and little-endian architectures. These features make SQLite a popular choice as an Application File Format.

Alternatives:

SQLite not as a replacement for SQL Server/Oracle, but a lightweight database. The other feasible alternative was to store the information in an XML file and insert/update the XML file using .Net framework classes.

SQLite for Windows 8:

SQLite team has announced that they are going to release an WinRT(Win 8) version of SQLite. It will be hopefully out before the RTM of Windows 8, and simplify the process of using it within an metro style app. However, it is not yet released as of today (19th June 2012).

So how did I use SQLite for my Windows 8 App?

I used the native SQLite DLL, and a .Net wrapper on top of it, which I got from my contact at Microsoft.

There are other .Net SQLite wrappers on CodePlex, but I haven’t been able to evaluate them. Ensure they are compatible with Win 8 consumer/release preview and pass the certification.

How big is my LibSQLite.dll?

756 KB.

I initially found it hard to believe that you can have an entire database engine in less than 1 MB.

How do I install?

This is the best part. It does not require any installation on end user machine. All I need to do is include it in my Visual Studio project, to ensure its present in my bin folder after code compilation.

Who else uses SQLite?

Countless apps. If you have a smart phone using Android, iPhone OS, Samsung Bada or Windows Phone OS, there are high chances that you have already have an app on your phone which uses SQLite.

Drawbacks/Limitations/Performance:

Inserting contact information for my 450 (and growing) Facebook friends, to my local SQLite database, is relatively slow and takes about 2 minutes.

There are ways and blogs to optimize SQLite performance which I need to look into.

Disclaimer: I had issues with running the app on tablets, and not sure what is the cause. SQLite can be one reason, but I have seen other Win 8 apps using SQLite running without any issues on tablets. So it’s likely that I have picked a wrong version of SQLite, or something else is causing the issue.

I am bad at SQL Data Definition Language syntax. How to I test my SQL JOIN queries?

If you have forgotten the SQL syntax that your probably learnt in college, no worries. You can use open source tools like  “SQLite Database Browser”. It’s a smaller, light weight version of SQL Server Management Studio(that we usually use for our enterprise projects). I create the tables and insert data using GUI, and then use the tool to create the equivalent SQL scripts for me. I also use the tool to test my SQL Queries and joins against the database, before using them in my code.

To know more:

SQLite:

http://www.sqlite.org/

Search on Google, and there are enough reference links and blogs on SQLite.

Though this email is written with SQLite for Win8 App as the background, the information is really generic and can be applied to other scenarios as well.

My experience with SQLite or App development is limited. If there is anything incorrect what I stated above, or better alternatives, do let me know.