Self-Identified Type of Data in C++ Using Json-Spirit

Due to the lack of reflection in C++, the type of a data container (e.g. a variable or object) usually needs to be determined by the programmer who writes the code at design/coding time. Template is a way of parameterize the data type, but only limited to certain circumstances. This somehow hampers programmer productivity.

Json-spirit C++ package on codeproject or its 4.03 git repo on gitorious can solve this problem in an interesting way. A short version of it: Using json-spirit library (e.g. header only version) the data structure can be composed by runtime Object object and Array object, and at a lower level Value and Pair objects. The bottom layer, is the Value which holds a string, integer, double, bool, null, or another layer of Object or Array. Then at the top layer, there are Object which holds a list of key-value pairs, and Array which holds a list of values. In the middle is only the Pair object which is useful with Object but not Array. For a Value object, you get its type by its access methods.

Json-spirit is based on boost and spirit. Since boost spirit is a header-only library, you can use the header-only version of json-spirit. The steps to get the header-only version, is just to download the package, unpack it, and include the template headers in your code. No need to run the build and install process. Look at the header-only demo source code in the package, it is quite simple to follow.

In spirit 1.8.5 it includes a trimmed-down boost called miniboost. See the sourceforge project to get the package. And the spirit 1.8.5 is easier to understand than the later versions. If you stick to spirit 1.8.5, then to use json-spirit it needs more boost packages than the miniboost in spirit package. The corresponding boost version is boost 1.34.1. It is best to use boost 1.34.1 with json-spirit, and spirit 1.8.5 with your spirit applications.

There is a trick to build your program with json-spirit 4.03: Add “-DBOOST_MPL_CFG_NO_PREPROCESSED_HEADERS” to your gcc command line. Not sure about this for json-spirit 4.05 or later versions.

 

Advertisements

About minghuasweblog

a long time coder
This entry was posted in All, C/C++, Methods, Tools and tagged , , , , , . Bookmark the permalink.

One Response to Self-Identified Type of Data in C++ Using Json-Spirit

  1. Pingback: Technical Topics | minghuasweblog

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