0

I'm using the libjson for parsing a JSON file in C++. I was wondering if you could do something like a PHP style notation for a map:

Just some pseudo code:

mapObj["id"] = 4;
mapObj["tags"] = vector {"Foo", "Bar"};

structMapObj = {
    {"name", "FooBar"},
    {"size", 1234567},
    {"date", "2014-12-24"}
};
mapObj["file"] = anotherMapObject;


// for the vector
mapObj["tags"][0];
mapObj["tags"][1];
mapObj["tags"].size();
mapObj["tags"].pushBack("Foo");

// for the map
mapObj["file"]["name"]
...

Is it possible to receive a result like this?

Maybe an enum for the current type in the BaseClass?

myObj["key"].getType; // returns a 1 for example an INT

I tried to make it with a BaseClass and a template class, but I wasn't able to iterate through the object. Or should I even overload the operators for my BaseClass? Or is it necessary to inherit the BaseClass for each case (a class for the map-type object, a class for the int-type, for string and so on)?

I'm a a little bit desperate right now. Just need someone who leads me into the right direction :-P

PS: I don't want to use boost :-/

Thank you very much,

Daniel

daniel
  • 110
  • 1
  • 7
  • _'PS: I don't want to use boost :-/'_ What a pity ... – πάντα ῥεῖ May 10 '14 at 13:22
  • If I understood well you want a map with string keys and arbitrary value types (`int`, `std::vector`, etc)? – 101010 May 10 '14 at 13:22
  • @πάνταῥεῖ first I want to understand how things work – daniel May 10 '14 at 13:26
  • @DanielQuast Achieving the same behavior as e.g. `boost::variant` by writing your own code for it, will be a long and windy road. Good luck! – πάντα ῥεῖ May 10 '14 at 13:29
  • @40two yes, I would like to parse through my file and than easily walk through my map: mapObj["key"]["InceptionMap"]["key"] .. – daniel May 10 '14 at 13:29
  • @πάνταῥεῖ you made me erase my comment, we were writing the same thing. OP see http://stackoverflow.com/questions/9831218/what-is-the-equivalent-of-boostvariant-in-std-c – 101010 May 10 '14 at 13:30
  • @DanielQuast unfortunately there's nothing something like `boost::variant` in the `std`, at least for time being. So you'll either have to resort to your own implementation, which in my humble opinion I think is not smooth sailing, or you'll invest some time learning `boost.variant` – 101010 May 10 '14 at 13:34
  • Argh! You win, i will take look at boost. I tried to avoid to use boost, just to be as independent as possible. ^^ – daniel May 10 '14 at 13:58
  • @daniel boost licensing allows you to include the source in your own code so you don't need to link against the boost libraries if you don't want to. In any case, boost::variant is 100% templates so there is no need to link against anything. Reading boost (or std) source code is an excellent way to become educated about the dark corners of c++ – Richard Hodges May 10 '14 at 14:26

2 Answers2

1

I appreciate that you don't want to use boost. However, this problem has been solved in boost.

You are essentially wanting a map of strings to variants.

Have a look at the source code for boost::variant and boost::any. Take particular note of how boost gets round the problem of recursive definitions, for example when you want to store a map inside an element of another map.

This will teach you more than you ever wanted to know on this subject :-)

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
0

There is an open source project for C++Builder programmer called JSonCBB library. This library provides a semantic like to your need: http://www.cbuilderblog.com/jsoncbuilderblog-class-library/

AngeloDM
  • 397
  • 1
  • 8
  • Thank you for your answer! I tried to avoid external libs, but everyone recommend boost, so give it a shot – daniel May 10 '14 at 14:01