Monday, 11 July 2011

From lib_json to Jansson

I love homogeneity when it comes to developing software. Sure, heterogeneity is required when dealing with undefined cutting edge high performance systems. But for application development, where software elegance and maintainability are key. You really don't want to tie yourself down to platform specific libraries especially in the current "App" world where you want to hit as many platforms as possible. This is why I recently switched to Jansson for my JSON parsing needs.

Jansson is a C JSON library which doesn't depend on other libraries, so if like me you need something to run on Android's NDK which doesn't support STL, you want to be using this.
http://www.digip.org/jansson/

Since I find sample code to be the easiest way of picking up a new library..

Here is example code showing you how to parse your friends list from Facebook's graph api using lib_json.
 #include "json.h"  
   
 ...  
   
 std::string jsonResponse( reply->data.buffer );  
 Json::Value root;  
 Json::Reader reader;  
 const BOOL parsingSuccessful = reader.parse( jsonResponse, root );  
 ASSERT( parsingSuccessful );  
 if( parsingSuccessful )  
 {  
   const Json::Value jsonData = root["data"];  
   const uint length = jsonData.size();  
     
   for( uint i=0; i<length; ++i )  
   {  
     Json::Value text = jsonData[i];  
       
     std::string jsonStringName = text["name"].asString();  
     std::string jsonStringID = text["id"].asString();  
       
     // We can now do something with our Name and ID  
   }  
 }  

Here is example code showing you how to parse your friends list from Facebook's graph api Jansson.
 #include "jansson.h"  
   
 ...  
   
 json_error_t error;  
 json_t *root = json_loads( reply->data.buffer, 0, &error );  
 if( root )  
 {  
   json_t *jsonData = json_object_get( root, "data" );  
   if( json_is_array( jsonData ) )  
   {  
     const uint length = json_array_size( jsonData );  
     for( uint i=0; i<length; ++i ) // Iterates over the sequence elements.  
     {  
       json_t *jsonObject = json_array_get( jsonData, i );  
         
       json_t *jsonID = json_object_get( jsonObject, "id" );  
       const char *jsonStringID = json_string_value( jsonID );  
         
       json_t *jsonName = json_object_get( jsonObject, "name" );  
       const char *jsonStringName = json_string_value( jsonName );  
         
       // We can now do something with our Name and ID  
     }  
   }  
   json_decref( root ); 
 }  

Piece of cake :)

1 comment:

  1. i'm finding the JSON libraries that are available for Java on Android to be quite slow. is there anyway you can provide benchmarks that compare org.json, gson and jackson with your jannson impl? before i dive into the ndk i'd just like to make sure there's a tangible (and suitably substantial) performance difference. here's what i've found with gson/jackson:

    ////////Gson
    parse records: 62
    parse time in ms: 16837

    ///////Jackson
    parse records: 62
    parse time in ms: 12851

    if you could provide any comparison between either gson/jackson with jannson that would be an enormous help.

    ReplyDelete