Skip to main content

Serializing & Deserializing JSON Object [ C++ ]

         This is sample code for serializing an object into json object and deserializing it back into earlier object.

Things to keep in mind :
  • While creating a json object, you have to write json_object* jObj=json_object_new_object(); initializing it as  json_object* jObj ; will not work.
  • You need to install JSON library using $ sudo apt-get install libjson0 libjson0-dev, Check path of installed json here $ ls /usr/include/json
  • You need to add json library to linker or compiler path. For ex. in Eclipse , Right click on project >> properties >> path and symbols. Add /usr/include/json in library paths & json in libraries.

//============================================================================
// Name        : JsonTest.cpp
// Author      : Manish Kumar Khedawat
//============================================================================

#include <iostream>
#include <json/json.h>

using namespace std;

struct objStruct{
 string str;
 int n1;
 int n2;
};

typedef objStruct obj;

void serializeToJson(json_object *jObj,obj* pObj)
{
 /*
     string str;
  int n1;
  int n2;
  */

 // Create json object for every member in struct Obj.

 json_object *jstr = json_object_new_string (pObj->str.c_str());
 json_object *jn1 =json_object_new_int(pObj->n1);
 json_object *jn2 =json_object_new_int(pObj->n2);

 // Add all above created object into jObj

 json_object_object_add(jObj,"str",jstr);
 json_object_object_add(jObj,"n1",jn1);
 json_object_object_add(jObj,"n2",jn2);

 // pObj is Serialzed into jObj
}

void deSerializeToJson(json_object *jObj,obj* pObj)
{
 /*
     string str;
  int n1;
  int n2;
  */

 // Get every member as different json obj from jObj
 json_object *jstr = json_object_object_get (jObj,"str");
 json_object *jn1 =json_object_object_get(jObj,"n1");
 json_object *jn2 =json_object_object_get(jObj,"n2");


 pObj->str=json_object_get_string(jstr);
 pObj->n1=json_object_get_int(jn1);
 pObj->n2=json_object_get_int(jn2);

 // jObj is DeSerialzed into pObj
}

int main() {
 // Lets Create an Object which we will serialze into Json
 obj obj1;
 obj1.n1=3;
 obj1.n2=6;
 obj1.str="This is String";

 // Create a json Object
 json_object* jObj=json_object_new_object();

 // To serialize into Json Object
 // Please Keep in mind , we are passing address of object (pointer) & not object
 serializeToJson(jObj,&obj1);

 obj obj2;
 // To serialize into Json Object
 // Please Keep in mind , we are passing address of object (pointer) & not object
 deSerializeToJson(jObj,&obj2);


 cout<<"String str == "<<obj2.str<<endl;
 cout<<"n1 & n2 : "<<obj2.n1<<" "<<obj2.n2<<endl;


 return 0;
}

Let me know through comments if it doesn't work out :)


Comments

Popular posts from this blog

Drawing and Animating ASCII Art in C# Console

This is sample code for drawing ASCII art character in C# console. Things to keep in mind : Replace the variable var with any ASCII art you want to get drawn in console.  Take care of [@],["] and [,] while replacing. using System ; namespace Dotned.UI.Framework { public class Arbit { public static void Main (String[] Args) { var arr = new [] { @" /$$$$$$ /$$ /$$ /$$ " , @" /$$__ $$ | $$ | $$ |__/ " , @" | $$ \__/ /$$$$$$ /$$$$$$/$$$$ /$$$$$$ /$$$$$$ | $$$$$$$ /$$ /$$$$$$$ /$$$$$$ " , @" | $$$$$$ /$$__ $$| $$_ $$_ $$ /$$__ $$|_ $$_/ | $$__ $$| $$| $$__ $$ /$$__ $$ " , @" ...

Git – setting up a remote repository and doing an initial push

So, Lets firstly set-up the remote repository: Step 1 : ssh will get me access to console on remote pc, will ask for remote pc's password Step 2 : create a directory with extension .git Step 3 : go to created directory Step 4 : initialize git Step 5 : Update Server configuration for access from non local sites Step 6 : Exit from remote access                     $ssh username@ipaddressofserverpc                    $mkdir my_project.git                    $cd my_project.git                    $git init --bare                    $git update-server-info # If planning to serve via HTTP                    $exit Now, On local machine: Step 1 : go to local folder Step 2 : Initia...