Freebase Query with C# : Level 2

In my previous post I wrote about how to query Freebase using MQL .In this blog post I will try and construct a slightly more complex query for Freebase.Freebase has name:value pairs which we can query and get our desired result.
We start with our simple query and make a little more complex.

class Question
{
public String type { get; set; }
public String name { get; set; }
public Dictionary album { get; set; }
public Question()  
{
 this.album = new Dictionary();}
}

Next,Just new up the Question instance with some data.


Question q = new Question();
q.type = "/music/artist";
q.name = null;q.album.Add("name",null);
q.album.Add("name~=","greatest hits");
q.album.Add("optional",true);

Now,begin the task to convert the Question object into a Json object for the query.The idea I used was to take in a object as a parameter and then iterate over all the properties using reflection (I Will talk more about it in the next post).


class ParseToJsonString 
{

 private StringBuilder sb;
 private Type type;
 private PropertyInfo[] propinfo;
 private PropertyInfo currentProperty;
 public String JsonString { get; set; }
 public ParseToJsonString(Object sender)
{
 this.sb = new StringBuilder();
 this.type = sender.GetType();
 this.propinfo = this.type.GetProperties();
 this.JsonString = "";
 toJsonString(sender);
//remain agnostic to the object being passed in as a parameter,get all the properties using reflection</pre>
private void toJsonString(Object sender){
     sb.Append("{");
     foreach (PropertyInfo p in propinfo){
      // Console.WriteLine(p.PropertyType);
         currentProperty = p;
         sb.AppendLine();
        if (p.PropertyType == typeof(Int32) || p.PropertyType == typeof(Double)
           || p.PropertyType == typeof(float) || p.PropertyType == typeof(bool)){
             handlePrimitive(sender, p);}
         else if (p.PropertyType == typeof(String))
           {
             handle String(sender, p);
           }

In the snippet above we loop through the properties and handle them for parsing according to their type.
For more complex types like list,dictionary etc. we have to adopt a similar approach.
I have written the method considering the for collections as String(since I am concentrating on Freebase),
this can be easily converted to handle all of the types.The output we get is as follows:-


{
 "type":"/music/artist",
 "name":null,
 "album":[{
             "name":null,
             "name~=":"greatest hits",
             "optional":<del datetime="2011-09-18T14:05:03+00:00">True</del>true
           }]
}

We can use this parser(with some improvements) to develop a wrapper and build an even more complicated query using the reserved key words of Freebase to have sorting,filtering,limiting etc..
Update:- the “optional” parameter must have a value of boolean in lower case ,True will raise an error.Freebase expects it to be true.
The entire class is available below for download.
Download ParseToJsonString

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s