Serialize C++ objects to json
This article shows how to serialize c++ objects to json in Visual Studio, while debugging the program, without writing any code.
Sample code
// Nested container with custom class
std::vector<std::map<std::string, Person>> people_vector = {
{{"person1", Person("Alice", 30)}, {"person2", Person("Bob", 25)}},
{{"person3", Person("Charlie", 35)}, {"person4", Person("David", 28)}}
};
Steps to serialize people_vector
to json:
- Create a simple c++ program with the above code snippet.
- Compile the program and place a breakpoint after the
people_vector
definition. - Start debugging the program and run it until it hits the breapoint.
- Go to Extensions->VSDebugPro menu and open Console.
- Print or export the object to json with the following command
print -j people_vector
export -j people.json people_vector
The print and export commands are using the Visual Studio C++ debugger interface to automatically evaluate and expand people_vector
into a full json representation.