JSONedit: C++ class generator
JSONedit 0.9.8 introduces higly experimental C++ class code generator. This tool generates header (.h) + body (.cpp) files for very simple class serializable to/from JSON file, very similar to "Settings" class I have used e.g. for tSIP softphone.
Some basic conversion rules and characteristics:
- maps numbers to C/C++ int/unsigned int, floating point numbers to double, strings to std::string, arrays to std::vector and objects to inner structs and obviously boolean to bool
- inner struct type name is created by changing first letter of object node name to upper case, struct itself - to lower case (be aware of possible name clash if objects in JSON source differ only by first letter case)
- converter generates default constructor for each inner struct and main object; default values are taken from source JSON
- all arrays are empty by default; use Default() function or constructor(s) to resize them if needed
- depends on json-cpp (version ~0.5 tested), i.e. generated code is using json-cpp library for parsing and generating JSON
Generator has quite a few limitations caused by its simplicity and/or differences between JSON and C++ objects:
- JSON root is required to be of object type
- JSON arrays structure have to be homogeneous, only type of first array element is checked
- generator refuses to process JSON with null type elements
- generator refuses to process JSON containing empty (0 elements) array(s)
- generator refuses to process JSON containing empty (no members) object(s)
- generator refuses to process heavily nested JSON arrays (i.e. array inside array inside array and object inside array inside array)
- no names validation for C++ purposes; generated code would not be valid if source node names would contain spaces or other restricted characters (e.g. $ as in youtube data example)
- C++ string escaping is missing
- some of indentation (related to array reading/writing) may be missing, you may want to run astyle over output files, also indentation may not match your preferred style
- additional data validation (e.g. checking/limiting integer data ranges) has to be done manually; on the other hand JSON type checking is quite strong
Output class files have been quickly tested with C++ Builder and Visual C++ 2008 Express Edition (classtests.zip). Depending on project settings (precompiled headers) #pragma hdrstop or including stdafx.h respectively may be needed.
Back to JSONedit