0

I have support for recursive multi-type lists and am having trouble converting them to json objects.

I have this class:

class Game{
string name;

//ElemtnSptr is defined below the class ListElement given below
//these are all lists
ElementSptr constants; 
ElementSptr variables;
ElementSptr rules;
}

These lists can contain other lists and I do not know the sublists names beforehand. So I have the ListElement class which can store other ListElements.

class ListElement{
Type type;
//contains some virtual methods 
};

// I need to store recursive multi-type lists
typedef std::shared_ptr<ListElement> ElementSptr; 

typedef std::vector<std::shared_ptr<ListElement>> ElementVector; 

typedef std::map<std::string, std::shared_ptr<ListElement>> ElementMap; 

template <typename T> 
class Element : public ListElement{
T data;

Element(int data)              : _data(data) { type = Type::INT; }
    Element(std::string data)      : _data(data) { type = Type::STRING; }
    Element(ElementVector data)    : _data(data) { type = Type::VECTOR; }
    Element(ElementMap data)       : _data(data) { type = Type::MAP; }
   
//implementations of the virtual methods 
};

_data can be a an ElementSptr or ElementVector or ElementMap or a basic type. Further, ElementVector or ElementMap contains ListElements which means they can contain other ElementVectors or ElementMaps and so on.

How do I write the to_json method for this?

callum arul
  • 159
  • 6

0 Answers0