user1029083
user1029083

Reputation: 191

json to c++ struct

I have following Json structure.

{
"name": "abc",
"city": "holland",

"links": [
    {
        "href": "/city/holland/1",
        "method": "GET",
        "rel": "edit",
        "type": "application/holland.citydata+json"
    },
    links": [
    {
        "href": "/city/holland/2",
        "method": "GET",
        "rel": "self",
        "type": "application/holland.citydata+json"
    },

], 

I have parsed this json response using some parser. Now i want to convert it to C++ struct object.

typedef struct json_object;
struct json_object {

char name;
char city; };

I have to read each href value in each link by looping through JasonParser response object.How can i acheive that in struct.

Should I use list for the links? How can i do that in struct?

Would someone please give example.

Upvotes: 6

Views: 13120

Answers (4)

idealvin
idealvin

Reputation: 318

Use flex and yacc to implement a parser and code generator to generate code for converting JSON to struct automatically for you.

Coost provides such a tool gen, define the struct in file xx.proto:

package xx
    
// supported base types:
//   bool, int, int32, uint32, int64, uint64, double, string
object X {
    string api
    data {  // anonymous object, field name can be put ahead
        bool b
        int i
        double d
        [int] ai   // array of int
        ao [{      // array of anonymous object
            int v
            string s
        }]
    }
}

then use gen xx.proto to generate the code you need, see the example j2s for details.

Upvotes: 1

Beached
Beached

Reputation: 1688

This is an old question, but comes up often. I would use https://github.com/beached/daw_json_link and then the following C++ data structures

  struct links_element_t {
    std::string href;
    std::string method;
    std::string rel;
    std::string type;
  };

  struct json_object_t {
    std::string name;
    std::string city;
    std::vector<links_element_t> links;
  };

In order to map this from the JSON document, the following code is needed

#include <daw/daw_json_link.h>
namespace daw::json {
template <>
struct json_data_contract<links_element_t> {
  static constexpr char const mem_href[] = "href";
  static constexpr char const mem_method[] = "method";
  static constexpr char const mem_rel[] = "rel";
  static constexpr char const mem_type[] = "type";
  using type = json_member_list<json_string<mem_href>, json_string<mem_method>,
                                json_string<mem_rel>, json_string<mem_type>>;

  static inline auto to_json_data(links_element_t const& value) {
    return std::forward_as_tuple(value.href, value.method, value.rel,
                                 value.type);
  }
};

template <>
struct json_data_contract<root_object_t> {
  static constexpr char const mem_name[] = "name";
  static constexpr char const mem_city[] = "city";
  static constexpr char const mem_links[] = "links";
  using type = json_member_list<
      json_string<mem_name>, json_string<mem_city>,
      json_array<mem_links, json_class<no_name, links_element_t>,
                 std::vector<links_element_t>>>;

  static inline auto to_json_data(root_object_t const& value) {
    return std::forward_as_tuple(value.name, value.city, value.links);
  }
};
}

Afterwards, to parse it to the json_object_t it would be like

json_object_t json_object = daw::json::from_json<json_object_t>( json_doc );

Upvotes: 0

Tom Kerr
Tom Kerr

Reputation: 10720

This is how I would do it.

struct Link {
  std::string href;
  std::string method;
  std::string rel;
  std::string type;
};

struct JSONObject {
  std::string name;
  std::string city;
  std::vector<Link> links;
};

Depending on how you are using it, you could refine it a bit.

enum Method {
  GET
  ,POST
};

This could be reasonable, but I think strings are expressive enough until they get in your way.

Upvotes: 2

Related Questions