Introduction

I would like to define a c++ structure

struct Person {
 std::string name;
 int age;
 bool student;
} person;

pass the person instance to the mapping method along with JSON data

map_json_to_struct(person, json_data)

then use the filled structure

std::cout << person.name << " : " << person.age;

Or vice versa

map_struct_to_json(person, json_data, "  ");
std::cout << json_data.str() << std::endl;

get Person as JSON

{
 "name": "Agent K",
 "age": 42,
 "student": false
}

StructMapping is trying to solve these problems

Compatibility

Compilation is required with -std=c++17 Mainly for:

  • if constexpr
  • static inline

Compiler | platform combinations on which StructMapping has been tested:

  • GNU C++ 10.1.0 | Linux
  • Visual C++ 2019 and Microsoft ® C/C++ Optimizing Compiler Version 19.26.28806 for x64 | Windows 64-bit (except tests)

As types of member-data can be used

  • bool
  • char, unsigned char, short, unsigned short, int unsigned int, long, long long
  • float, double
  • std::string
  • std::list
  • std::vector
  • std::set (expected to be represented in JSON as an array)
  • std::unordered_set (expected to be represented in JSON as an array)
  • std::multiset (expected to be represented in JSON as an array)
  • std::unordered_multiset (expected to be represented in JSON as an array)
  • std::map (the key can only be std::string)
  • std::unordered_map (the key can only be std::string)
  • std::multimap (the key can only be std::string)
  • std::unordered_multimap (the key can only be std::string)
  • c++ structure
  • enumerations

Installation

StructMapping is a header-only C++ library. All library files are in the include folder.

To build examples and run tests proceed with the steps below (cmake required):

  1. create a directory called build in StructMapping source directory
  2. change to build directory
  3. run cmake .. command to configure your build
  4. run cmake --build . command. On successful build you will find binary files for examples and tests (under windows tests are not build) in the bin directory
  5. (not for windows) to run the tests run ctest (you can get detailed output using ctest -V)
  6. it is possible to install library system-wide by running cmake --install . command from the build tree with administrative privileges. This will install all files according to system preferences.

#json #c++ #json serialization #programming-c #cplusplus

Mapping JSON to and from a C++ Structure
21.90 GEEK