Home > Net >  How create a JSON-file from my data? (with С , boost json)
How create a JSON-file from my data? (with С , boost json)

Time:09-21

I wanted to create a Json-file that will be created from the data received earlier. I don't understand how to work with Json files at all. I want to use the Boost library, because I am using it in another part of this program. I need to create a Json-file with a specific structure which I have attached below.

I need to get JSON:

{
  "track": {
    "Wheels": {
      "Wheel": [
        {
          "start_pos": "10",
          "end_pos": "25"
        },
        {
          "start_pos": "22",
          "end_pos": "78"
        }
      ]
    },
    "Brakes": {
      "Brake": [
        {
        "start_pos": "10",
        "midl_pos": "25"
        }
      ]
    }
  }
}

C :

#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include <string>
using namespace std;
using boost::property_tree::ptree;

struct wheel
{
    string start_pos;
    string end_pos;
};

struct brake
{
    string start_pos;
    string midl_pos;
};

int main() 
{
    string tr = "track";
    string ws = "Wheels";
    string bs = "Brakes";

    struct wheel w1;
    w1.start_pos = "10";
    w1.end_pos = "25";

    struct wheel w2;
    w2.start_pos = "22";
    w2.end_pos = "78";

    struct brake b1;
    b1.start_pos = "10";
    b1.midl_pos = "25";

    return 0;
}

CodePudding user response:

Implementing it with the Boost JSON customization points.

Doing it test-driven:

Live On Coliru

#include "boost/json/src.hpp" // header-only approach
#include <iostream>
namespace json = boost::json;
using json::value_from;
using json::value_to;

static const json::value expected = json::parse(R"({
  "track": {
    "Wheels": {
      "Wheel": [
        {
          "start_pos": "10",
          "end_pos": "25"
        },
        {
          "start_pos": "22",
          "end_pos": "78"
        }
      ]
    },
    "Brakes": {
      "Brake": [
        {
        "start_pos": "10",
        "midl_pos": "25"
        }
      ]
    }
  }
})");

namespace MyLib {
    struct wheel { int start_pos, end_pos; };
    struct brake { int start_pos, midl_pos; };

    struct track {
        std::vector<wheel> wheels;
        std::vector<brake> brakes;
    };

    void tag_invoke(json::value_from_tag, json::value& jv, wheel const& w) {
        jv = {
            {"start_pos", std::to_string(w.start_pos)},
            {"end_pos", std::to_string(w.end_pos)},
        };
    }

    void tag_invoke(json::value_from_tag, json::value& jv, brake const& b) {
        jv = {
            {"start_pos", std::to_string(b.start_pos)},
            {"midl_pos", std::to_string(b.midl_pos)},
        };
    }

    void tag_invoke(json::value_from_tag, json::value& jv, track const& t) {
        jv = {{"track",
               {
                   {"Wheels", {{"Wheel", t.wheels}}},
                   {"Brakes", {{"Brake", t.brakes}}},
               }}};
    }
}

int main() 
{
    MyLib::track track{{
                           {10, 25},
                           {22, 78},
                       },
                       {
                           {10, 25},
                       }};

    json::value output = json::value_from(track);
    std::cout << output << "\n";

    std::cout << expected << "\n";
    std::cout << "matching: " << std::boolalpha << (output == expected) << "\n";
}

Prints

{"track":{"Wheels":{"Wheel":[{"start_pos":"10","end_pos":"25"},{"start_pos":"22","end_pos":"78"}]},"Brakes":{"Brake":[{"start_pos":"10","midl_pos":"25"}]}}}
{"track":{"Wheels":{"Wheel":[{"start_pos":"10","end_pos":"25"},{"start_pos":"22","end_pos":"78"}]},"Brakes":{"Brake":[{"start_pos":"10","midl_pos":"25"}]}}}
matching: true

CodePudding user response:

Despite comments suggesting to use a different library, which they could, I think @Nindzzya wanted to specifically use the boost library.

Using the boost library from How to use boost::property_tree to load and write JSON:

#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include <string>
#include <iostream>
using namespace std;
namespace pt = boost::property_tree;

struct wheel
{
    string start_pos;
    string end_pos;
};

struct brake
{
    string start_pos;
    string midl_pos;
};

int main() 
{
    string tr = "track";
    string ws = "Wheels";
    string bs = "Brakes";

    struct wheel w1;
    w1.start_pos = "10";
    w1.end_pos = "25";

    struct wheel w2;
    w2.start_pos = "22";
    w2.end_pos = "78";

    struct brake b1;
    b1.start_pos = "10";
    b1.midl_pos = "25";

    pt::ptree wheel1, wheel2, wheels, wheel;
    pt::ptree brake1, brakes, brake;
    pt::ptree track, root;

    wheel1.put("start_pos", w1.start_pos);
    wheel1.put("end_pos", w1.end_pos);

    wheel2.put("start_pos", w2.start_pos);
    wheel2.put("end_pos", w2.end_pos);

    wheels.push_back(make_pair("", wheel1));
    wheels.push_back(make_pair("", wheel2));

    wheel.add_child("Wheel", wheels);
    track.add_child(ws, wheel);


    brake1.put("start_pos", b1.start_pos);
    brake1.put("midl_pos", b1.midl_pos);

    brakes.push_back(make_pair("", brake1));

    brake.add_child("Brake", brakes);
    track.add_child(bs, brake);

    root.add_child(tr, track);

    pt::write_json(std::cout, root);

    return 0;
}

results in:

{
    "track": {
        "Wheels": {
            "Wheel": [
                {
                    "start_pos": "10",
                    "end_pos": "25"
                },
                {
                    "start_pos": "22",
                    "end_pos": "78"
                }
            ]
        },
        "Brakes": {
            "Brake": [
                {
                    "start_pos": "10",
                    "midl_pos": "25"
                }
            ]
        }
    }
}
  • Related