JSON → C++ 変換ツール

JSONサンプルから、nlohmann/jsonのシリアライズ関数を備えたC++クラスを生成します。

JSONからnlohmann/json対応のC++クラスを生成

C++には組み込みのJSONサポートがなく、広く使われているnlohmann/jsonライブラリで自動変換を行うには、型ごとにfrom_json関数とto_json関数が必要です。このツールは、オブジェクトごとにprivateメンバー、getter、setterを持つクラスと、対応するfrom_json/to_json関数を宣言したヘッダーを書き出します。これにより、json.get<SensorReading>()やオブジェクトをjsonへ代入する操作がそのまま動作します。整数はint64_t、小数はdouble、文字列はstd::stringになります。

使い方

  1. デバイスのメッセージや設定ファイルなどのJSONサンプルを貼り付けます。
  2. ルートクラスの名前を入力します(例: SensorReading)。
  3. 出力をヘッダーとして保存し、nlohmann/jsonのjson.hppをインクルードパスに追加して、nlohmann::json::parse(text).get<quicktype::SensorReading>()でパースします。

よくある質問

生成されたコードに必要なライブラリは?
シングルヘッダーのnlohmann/jsonライブラリ(json.hpp)が必要です。ヘッダーはオプショナルなフィールドのためにboost/optional.hppもインクルードしていますが、JSONにオプショナルなフィールドがなければこのincludeは削除できます。
必要なC++の規格は?
nlohmann/jsonにはC++11以降が必要です。上記のライブラリ以外では、生成されたヘッダーは標準ライブラリの型のみを使用します。
quicktype名前空間は変更できますか?
はい。namespace quicktypeを自分の名前に変更してください。from_jsonとto_jsonはクラスと同じ名前空間にある限り、実引数依存の名前探索(ADL)によって見つけられます。

例: JSONからC++への変換

入力JSON

{
  "sensor_id": "temp-01",
  "value": 21.5,
  "unit": "C",
  "battery": 87,
  "ok": true
}

生成された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();
    }
}

JSONを他の言語に変換