A recent project of mine necessitated that I find a way to share data between a client application written in C++ and a host application written in C#. After talking with the other programmer on the project, we decided that using Json to share data between the applications would be the best solution. This sent me on a search for a decent API for parsing Json data, which led to JsonCpp.
A cursory glance led me to conclude that this API would likely provide me with everything I would need for object serialization. The problem, however, was coming up with a decent design that would allow for full featured serialization. The goals were simple. I needed to be able to serialize and deserialze a class. I would also need access to all data members, including primitives, data structures, and nested classes.
JsonCpp provides us with the capability to do this with relative ease. As such, I hope to provide you all with a basic design to accomplish this. Luckily, we won’t need a deep understanding of JsonCpp to accomplish our goals. As such, I’m not going to overly explain much of the JsonCpp concepts, as the JsonCpp documentation is a good enough resource.
First, let’s consider a simple test class.
class TestClassA { public: TestClassA( void ); virtual ~TestClassA( void ); private: int m_nTestInt; double m_fTestFloat; std::string m_TestString; bool m_bTestBool; };
This class should provide us with a basic framework to test serialization. We’re going to start with primitive data first (granted, std::string isn’t strictly primitive, but it’s primitive enough).
Now that we have a basic test case, lets design a basic interface that all of our serializable classes can inherit from.
class IJsonSerializable { public: virtual ~IJsonSerializable( void ) {}; virtual void Serialize( Json::Value& root ) =0; virtual void Deserialize( Json::Value& root) =0; };
This should be self explanitory. We obviously need to include json.h in order to have access to the Json::Value class. The Json::Value class is actually fairly complex, and as such we aren’t going to spent much time examining it. Suffice to say, we are going to treat it as a sort of map, mapping our metadata tags to the actual values we will be serializing. This will make much more sense once we work on the implementation of the Serialize() and Deserialize() methods in our classes.
So let’s update our updated test class definition.
class TestClassA : public IJsonSerializable { public: TestClassA( void ); virtual ~TestClassA( void ); virtual void Serialize( Json::Value& root ); virtual void Deserialize( Json::Value& root); private: int m_nTestInt; double m_fTestFloat; std::string m_TestString; bool m_bTestBool; };
Now before we progress further on the C++ side of things, let’s cook up some test Json data to work with.
{ "testboolA" : true, "testfloatA" : 3.14159, "testintA" : 42, "teststringA" : "foo" }
Whether you know Json or not, this data structure should be completely self-explanatory. The goal here is going to be to deserialize this data into our class. However, we have one more thing to do.
The last class we need to design here is the actual serializer class.
class CJsonSerializer { public: static bool Serialize( IJsonSerializable* pObj, std::string& output ); static bool Deserialize( IJsonSerializable* pObj, std::string& input ); private: CJsonSerializer( void ) {}; };
This is a simple little “static class”. We’ll make the constructor private this way we can’t actually instantiate it. We will simply deal with the methods directly. This will let us add a second layer of abstraction to JsonCpp.
Alright, our basic design is done. Let’s get into the implementation. Let’s start with our test class.
void TestClassA::Serialize( Json::Value& root ) { // serialize primitives root["testintA"] = m_nTestInt; root["testfloatA"] = m_fTestFloat; root["teststringA"] = m_TestString; root["testboolA"] = m_bTestBool; } void TestClassA::Deserialize( Json::Value& root ) { // deserialize primitives m_nTestInt = root.get("testintA",0).asInt(); m_fTestFloat = root.get("testfloatA", 0.0).asDouble(); m_TestString = root.get("teststringA", "").asString(); m_bTestBool = root.get("testboolA", false).asBool(); }
Remember when I said we were going to look at the Json::Value class like a map? That should make more sense now. The Serialize() method should make sense. We are simply adding the values to our root object as if it was a map. The keys we are using map directly to the metadata in the Json data. The Deserialize() method is only slightly more complex. We need to use the get() method in order to retrieve our data. The get() method takes two parameters: get( <key>, <defaultvalue>). Knowing that, the method should make sense. We simply call get() with each key and place the values from the Json into our members.
Now let’s move on to the CJsonSerializer class implementation.
bool CJsonSerializer::Serialize( IJsonSerializable* pObj, std::string& output ) { if (pObj == NULL) return false; Json::Value serializeRoot; pObj->Serialize(serializeRoot); Json::StyledWriter writer; output = writer.write( serializeRoot ); return true; } bool CJsonSerializer::Deserialize( IJsonSerializable* pObj, std::string& input ) { if (pObj == NULL) return false; Json::Value deserializeRoot; Json::Reader reader; if ( !reader.parse(input, deserializeRoot) ) return false; pObj->Deserialize(deserializeRoot); return true; }
It should be obvious now why we wanted a second level of abstraction from JsonCpp.
Let’s look at the Serialize() method here. We need to pass two parameters, a pointer to the IJsonSerializable object to be serialized, and a reference to a std::string. That string is going to hold the serialized Json data from our class. This is very rudimentary. We simply create an instance of a Json::Value object to act as our root, and pass it to the Serialize() method of the object in question. That Serialize() method will fill the Json::Value object with all of the serialized data. We then create an instance of Json::StyledWriter, and use it to write the Json data to the empty std::string we originally passed.
As for the Deserialize() method, it’s not terribly different aside from the fact that the std::string we pass is going to contain Json instead of being empty. We will create an instance of Json::Reader and use it to parse our input string and fill a Json::Value object for us. We will then use that new Json::Value object and pass it to the IJsonSerializable objects Deserialze() method, which will set the data members based on the Json::Value object’s values.
As you can see, this is all pretty simple. We can now create a simple test case.
TestClassA testClass; std::string input = "{ \"testintA\" : 42, \"testfloatA\" : 3.14159, \"teststringA\" : \"foo\", \"testboolA\" : true }\n"; CJsonSerializer::Deserialize( &testClass, input ); std::cout << "Raw Json Input\n" << input << "\n\n"; std::string output; CJsonSerializer::Serialize( &testClass, output); std::cout << "testClass Serialized Output\n" << output << "\n\n\n";
If everything went according to plan, you should see the following:
Raw Json Input { "testintA" : 42, "testfloatA" : 3.14159, "teststringA" : "foo", "testboolA" : true } testClass Serialized Output { "testboolA" : true, "testfloatA" : 3.14159, "testintA" : 42, "teststringA" : "foo" }
Essentially our test case simply created an “empty” TestClass object. It then deserialized a string of input Json data into the class, filling it’s members. We then create a new empty output string, and deserialize our TestClass object into that string. If the outut is what we expect, then we can assume that basic serialization and deserialization is working!
Great, but how to serializat/deserializat array ?
I have solution for array, we need to add:
to TestClassA.:
private;
vector m_vStrings;
and to TestClass.cpp
void CTestClass::Serialize( Json::Value& root )
{
int size = m_vStrings.size();
for(int i=0; i<size; i++)
{
root["vector"].append(m_vStrings[i]);
}
}
void CTestClass::Deserialize( Json::Value& root )
{
if(root.get("vector","").isArray())
{
int size = root.get("vector","").size();
for(int i=0; i<size; i++)
{
m_vStrings.push_back(root.get("vector","")[i].asString());
}
}
}
If you’re trying to serialize an object and not just values, as in the example above, you must create a Json :: Value for the item you want to serialize and then add it there is another Json :: Value array which will contain all items.
Here’s a simple example
vector arrayPlayerName;
root [“game”] = “Soccer”
string name;
Json :: Value array;
for (string :: iterator it = arrayPlayerName.begin (); it! arrayPlayerName.end = (); it + +)
{
Json :: Value itemPlayer;
name = * it;
itemPlayer [“playerName”] = name;
array.append (item);
}
root [“player”] = array;
It is really a silly example, but i hope it can help.
Thanks! This is exactly what I need for one of my upcoming projects 🙂
How can we serialize and deserialize structures
Awesome work bro… Helped alot…
Thanks for tutorial) It’s very usful for beginners.
And, in text you made some mistakes:
You wrote:
{
“testboolA” : true,
“testfloatA” : 3.14159,
“testintA” : 42,
“teststringA” : foo
}
Correction:
{
“testboolA” : true,
“testfloatA” : 3.14159,
“testintA” : 42,
“teststringA” : “foo”
}
And, you wrote:
Raw Json Input
{ “testintA” : 42, “testfloatA” : 3.14159, “teststringA” : “test”, “testboolA” : true }
Correction:
Raw Json Input
{ “testintA” : 42, “testfloatA” : 3.14159, “teststringA” : “foo”, “testboolA” : true }
Thanks a lot, and sorry for my english)
@Vladlen: Thanks for the keen eye. I updated the post.
Gr8 tutorial!!
How to serialize for sub-class and super class attributes in a single string,,!!
Just a hint will b sufficient!!
Thanks
Very useful article.
For vector, try this:
static void SerializeVectorString(Json::Value& root, const string key , vector& stringVector)
{
for (vector::const_iterator memberIt = stringVector.begin(); memberIt != stringVector.end(); ++memberIt)
root[key].append(* memberIt);
}
static void DeserializeVectorString(Json::Value& root, const string key, vector& stringVector)
{
if(root[key].isArray() )
{
for (uint32_t i = 0; i < root[key].size(); i++)
stringVector.push_back(root[key][i].asString());
}
}
[…] This one is quite decent information and advice on how to design class for such activity. http://www.danielsoltyka.com/programming/2011/04/15/simple-class-serialization-with-jsoncpp/ […]
[…] This one is quite decent information and advice on how to design class for such activity. http://www.danielsoltyka.com/programming/2011/04/15/simple-class-serialization-with-jsoncpp/ […]
Very good code, clear and well thought. Congratulation!
This is a nice and helpful tutorial.Thank you!
Say one was to transfer this serialized class over tcp socket – say from a tcp client and receive it on the other end – tcp server. How would one go about it?
@virgoptrex: Technically you could actually use this approach and transfer the output json string over the wire, but I wouldn’t recommend it in most cases unless you are trying to do something specific. Instead, I would say you would want to look into binary serialization and instead transfer the binary representation of your object across the wire.
excellent job.
Congrats
just missing the include 😉
#include “jsoncpp/json.h”
cpp doesnt support virtual constructors so i had errors till i commented : virtual ~IJsonSerializable( void ) {};
this for program to run successfully
I’m not sure what you mean by virtual constructors. The code you mentioned is a virtual *destructor* and C++ most certainly does support those.
oh yes destructors! my bad!! thanks..My code didn’t compiled till i removed the virtual destructor. Could you guide me for creating json with dynamic nested values. updating values of objects as well as creating and populating nested objects in cpp
Hi Daniel,I have a very complex json structure to implement using c++. the basic implementation went smoothly but the dynamically nested structure i failed to create: e.g {
“T” : [
{
“I” : “Ong”,
“S” : [
{
“l” : “()”,
“Ve” : “”,
“fe” : “”,
“eo” : “”,
}
]
}
], this sounds fine to implement.
but when
“grr_1”:{
“rLe_1”:{“L1″:””,”L2″:””,”LN”:””},
“rLe_2”:{“L1″:””,”L2″:””,”LN”:””},
“rLe_3”:{“L1″:””,”L2″:””,”LN”:””},
“rLe_n”:{“L1″:””,”L2″:””,”LN”:””},
“output”:{“L1″:””,”L2″:””,”LN”:””}
},
“grr_n”:{
“rLe_1”:{“L1″:””,”L2″:””,”LN”:””},
“rLe_2”:{“L1″:””,”L2″:””,”LN”:””},
“rLe_3”:{“L1″:””,”L2″:””,”LN”:””},
“rLe_n”:{“L1″:””,”L2″:””,”LN”:””},
“output”:{“L1″:””,”L2″:””,”LN”:””}
},
this chunk of “grr” repeats itself dynamically its not fixed.
also the structure inside changes with change in grr value. THanks in advance for help. I am not sure how to keep track of all the objects as nested varies
How would you deserialize a nameless json array?
something like:
[
{
“testboolA” : true,
“testfloatA” : 3.14159,
“testintA” : 42,
“teststringA” : “foo”
},
{
“testboolA” : false,
“testfloatA” : 1.618,
“testintA” : 42,
“teststringA” : “bar”
}
]