JSON to C++ Converter
Generate C++ classes with nlohmann/json serialization functions from a JSON sample.
Generate C++ classes from JSON for nlohmann/json
C++ has no built-in JSON support, and the popular nlohmann/json library needs a from_json and to_json function for every type it converts automatically. This converter writes a header that declares a class per object, with private members, getters and setters, and the matching from_json/to_json functions, so json.get<SensorReading>() and assigning an object back to json just work. Whole numbers become int64_t, decimals double and strings std::string.
How to use
- Paste a JSON sample, such as a device message or a config file.
- Enter the root class name, for example SensorReading.
- Save the output as a header, add json.hpp from nlohmann/json to your include path, and parse with nlohmann::json::parse(text).get<quicktype::SensorReading>().
Frequently asked questions
- Which libraries does the generated code need?
- It needs the single-header nlohmann/json library (json.hpp). The header also includes boost/optional.hpp, which is used for optional fields; if your JSON has no optional fields you can remove that include.
- Which C++ standard is required?
- nlohmann/json requires C++11 or later. Apart from the libraries above, the generated header uses only standard library types.
- Can I change the quicktype namespace?
- Yes. Rename namespace quicktype to your own; from_json and to_json are found by argument-dependent lookup as long as they stay in the same namespace as the classes.
Example: JSON to C++
Input JSON
{
"sensor_id": "temp-01",
"value": 21.5,
"unit": "C",
"battery": 87,
"ok": true
}Generated C++ (SensorReading)
// To parse this JSON data, first install
//
// Boost http://www.boost.org
// json.hpp https://github.com/nlohmann/json
//
// Then include this file, and then do
//
// SensorReading data = nlohmann::json::parse(jsonString);
#pragma once
#include "json.hpp"
#include <boost/optional.hpp>
#include <stdexcept>
#include <regex>
namespace quicktype {
using nlohmann::json;
#ifndef NLOHMANN_UNTYPED_quicktype_HELPER
#define NLOHMANN_UNTYPED_quicktype_HELPER
inline json get_untyped(const json & j, const char * property) {
if (j.find(property) != j.end()) {
return j.at(property).get<json>();
}
return json();
}
inline json get_untyped(const json & j, std::string property) {
return get_untyped(j, property.data());
}
#endif
class SensorReading {
public:
SensorReading() = default;
virtual ~SensorReading() = default;
private:
std::string sensor_id;
double value;
std::string unit;
int64_t battery;
bool ok;
public:
const std::string & get_sensor_id() const { return sensor_id; }
std::string & get_mutable_sensor_id() { return sensor_id; }
void set_sensor_id(const std::string & value) { this->sensor_id = value; }
const double & get_value() const { return value; }
double & get_mutable_value() { return value; }
void set_value(const double & value) { this->value = value; }
const std::string & get_unit() const { return unit; }
std::string & get_mutable_unit() { return unit; }
void set_unit(const std::string & value) { this->unit = value; }
const int64_t & get_battery() const { return battery; }
int64_t & get_mutable_battery() { return battery; }
void set_battery(const int64_t & value) { this->battery = value; }
const bool & get_ok() const { return ok; }
bool & get_mutable_ok() { return ok; }
void set_ok(const bool & value) { this->ok = value; }
};
}
namespace quicktype {
void from_json(const json & j, SensorReading & x);
void to_json(json & j, const SensorReading & x);
inline void from_json(const json & j, SensorReading& x) {
x.set_sensor_id(j.at("sensor_id").get<std::string>());
x.set_value(j.at("value").get<double>());
x.set_unit(j.at("unit").get<std::string>());
x.set_battery(j.at("battery").get<int64_t>());
x.set_ok(j.at("ok").get<bool>());
}
inline void to_json(json & j, const SensorReading & x) {
j = json::object();
j["sensor_id"] = x.get_sensor_id();
j["value"] = x.get_value();
j["unit"] = x.get_unit();
j["battery"] = x.get_battery();
j["ok"] = x.get_ok();
}
}