On Sep 20, 2012 2:37 AM, "srinivasa Rao" <sriniv...@gmail.com> wrote:
>
> Is it possible to dump c-structure object(pointer) into json string. Similar to Java/Perl.
Not exactly. The main problem is that C programs have no metadata about the structures they contain, so it's a bit difficult to work with the in-memory in an automated way.
You would have better luck with C++ if you can get RTTI to work properly.
That's not to say it's not possible in C, it just takes more work. For instance, I wrote a "message-structure-mapping" library for my day job that provides a way to set up descriptors that map from json messages to/from C structures, including doing things like allocating memory and/or calling initialization functions, etc...
It works, but it definitely isn't simple, and it took a fair amount of work to work out all the bugs in that library. I can describe the api in a bit more detail if you'd like, but of course that code isn't public and it's unlikely that'll I'll be able convince my company to release it any time soon.
If you don't need a general solution for arbitrary json messages and arbitrary C structures I recommend defining a couple of functions to "serialize" and "deserialize" your structure and then just open code the specific mapping you need.
Eric
> Having come across the same issue, i wrote one myself. https://github.com/jamie-pate/jstruct . It's written to allow annotating existing c structures, then generate meta-data information based on the annotations. The library reads the metadata to import/export c structures to json strings and back. A python script takes care of parsing the annotated header and generating new headers and metadata initializers. The jstruct library uses https://github.com/json-c/json-c internally.
I have also noticed https://github.com/marel-keytech... but that was after writing the entire thing. (and info on that project's page is sparse)
Feel free to add features requests or even pull requests if you have ideas that would make the library more useful to you.