#include<iostream>
#include<string>
#include<vector>
#include<rapidjson/document.h>
#include<rapidjson/filereadstream.h>
#include<fstream> 

using namespace std;
using namespace rapidjson;


class FSMBase
{
public:
	string StateName;
	string EventName;
	std::vector<FSMBase*> subitem;

public:

	FSMBase()
	{
		std::cout << "default constructor called" << std::endl;
	}

	void getdata()
	{
		std::cout << "FSMBase called" << std::endl;
	}

	virtual void Add(FSMBase* b1)
	{
		subitem.push_back(b1);
		b1->getdata();
	}

	~FSMBase()
	{
		std::cout << " destructor called" << std::endl;
	}
};


class premitivetool :public FSMBase
{
public:

	void Add(FSMBase* b1)
	{
		subitem.push_back(b1);
		b1->getdata();
	}
};

class Event : public FSMBase
{
	string ActionType;
	string ActionParam;
public:

	void getdata(string Name, string ActType, string ActPar)
	{
		EventName = Name;
		ActionType = ActType;
		ActionParam = ActPar;
		std::cout << " " << EventName << " " << ActionType << " " << ActionParam << std::endl;
	}
};

class state :public FSMBase
{
	string EntreyMethodName;
	std::vector<Event*> eventitem;
public:

	void getdata(string Name, string Methodname)
	{
		StateName = Name;
		EntreyMethodName = Methodname;
		std::cout << "  " << StateName << "  " << EntreyMethodName << std::endl;
	}

	void Add(FSMBase* fsmbase1)
	{
		subitem.push_back(fsmbase1);
		fsmbase1->getdata();
	}
};

int main()
{
	state* State1;
	Event* Event1;
	State1 = new state;	
	Event1 = new Event;
	State1->getdata("StartToolState", "EnterStartTool");
	State1->Add(Event1);

	premitivetool* prtool1;
	prtool1 = new premitivetool;
	prtool1->Add(State1);

	state* State2;
	Event* Event2;
	State2 = new state;
	Event2 = new Event;
	Event2->getdata("MotionEvent", "InvokeMethod", "MoveToolPoints");

	State1->Add(Event2);
	prtool1->Add(State2);

	delete Event2;
	delete State2;
	delete State1;
	delete Event1;
	delete prtool1;

	// Open the file 
	ifstream file("FSMImplementationJSON.json");
	if (!file.is_open()) {
		cerr << "Could not open file: FSMImplementationJSON.json" << endl;
		return 1;
	}

	// Read the entire file into a string 
	string json((istreambuf_iterator<char>(file)),
		istreambuf_iterator<char>());

	Document doc;

	// Parse the JSON data 
	doc.Parse(json.c_str());
	//std::cout << doc["Name"].GetString() << std::endl;

	// Check for parse errors 
	if (doc.HasParseError()) {
		cerr << "Error parsing JSON: "
			<< doc.GetParseError() << endl;
		return 1;
	}
	FILE* fp;
	fopen_s(&fp, "FSMImplementationJSON.json", "rb");

	if (fp == nullptr) {
		cerr << "Error opening the file" << std::endl;
		return 1;
	}

	char readBuffer[65536];
	rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
	rapidjson::Document d;
	d.ParseStream(is);


	if (d.HasMember("State") && d["State"].IsArray())
	{
		const rapidjson::Value& State = d["State"];
		std::cout << "State: ";
		for (rapidjson::SizeType i = 0; i < State.Size(); i++)
		{
			/*if (d.HasMember("Name") && d["Name"].IsString())
			{
				std::string name = d["Name"].GetString();
				std::cout << "name" << name << std::endl;
			}
			if (d.HasMember("EntryMethodName") && d["EntryMethodName"].IsString())
			{
				std::string entryname = d["EntryMethodName"].GetString();
				std::cout << "entryname" << entryname << std::endl;
			}*/
			if (State[i].IsString()) {
				std::string state = State[i].GetString();
				std::cout << state << " ";
			}
		}
		std::cout << std::endl;
	}
	//// Close the file 
	if (fp)
		fclose(fp);
	return 0;
}

FSMImplementation.json
{
	"PrimitiveTool": {
		"State": [
			{
				"Name": "StartToolState",
				"EntryMethodName": "EnterStartTool",
				
				"Event": {
					"Name": "CompleteEvent",
					"ActionType": "ChangeState",
					"ActionParam": "WaitForAccept"
				}
			},
			{
				"Name": "WaitForAccept",
				"EntryMethodName": "EnterWaitForAcceptState",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveToolPoints"
					},
					{
						"Name": "AcceptEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "StartManualEPrimeToolState",
				"EntryMethodName": "EnterStartManualEPrimeTool"
			},
			{
				"Name": "ManualSVelPointState",
				"EntryMethodName": "EnterManualSVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveSVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "EnterCompleteManualSVelPoint"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					}
				]

			},
			{
				"Name": "ManualEVelPointState",
				"EntryMethodName": "EnterManualEVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveEVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "EnterCompleteManualEVelPoint"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "ManualAVelPointState",
				"EntryMethodName": "EnterManualAVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveAVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "EditEVelPoint",
				"EntryMethodName": "EnterEditEVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveEVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "CompleteEVelPointState"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "EditSVelPoint",
				"EntryMethodName": "EnterEditSVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveSVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "CompleteSVelPointState"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "EditAVelPoint",
				"EntryMethodName": "EnterEditAVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveAVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "CompleteAVelPointState"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "EditManualEVelPoint",
				"EntryMethodName": "EnterEditManualEVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveEVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "CompleteEVelPointState"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "EditManualSVelPoint",
				"EntryMethodName": "EnterEditManualSVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveSVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "CompleteSVelPointState"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "EditManualAVelPoint",
				"EntryMethodName": "EnterEditManualAVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "MoveAVelPoint"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "CompleteAVelPointState"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "CompleteEVelPointState",
				"EntryMethodName": "EnterCompleteEVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "CheckNextMeas"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "EditEVelPoint"
					},
					{
						"Name": "RotaryEncoderInc",
						"ActionType": "InvokeMethod",
						"ActionParam": "EncoderInc"
					},
					{
						"Name": "RotaryEncoderDec",
						"ActionType": "InvokeMethod",
						"ActionParam": "EncoderDec"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "CompleteSVelPointState",
				"EntryMethodName": "EnterCompleteSVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "CheckNextMeas"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "EditSVelPoint"
					},
					{
						"Name": "RotaryEncoderInc",
						"ActionType": "InvokeMethod",
						"ActionParam": "EncoderInc"
					},
					{
						"Name": "RotaryEncoderDec",
						"ActionType": "InvokeMethod",
						"ActionParam": "EncoderDec"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "CompleteAVelPointState",
				"EntryMethodName": "EnterCompleteAVelPoint",
				
				"Event": [
					{
						"Name": "MotionEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "CheckNextMeas"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "ChangeState",
						"ActionParam": "EditAVelPoint"
					},
					{
						"Name": "RotaryEncoderInc",
						"ActionType": "InvokeMethod",
						"ActionParam": "EncoderInc"
					},
					{
						"Name": "RotaryEncoderDec",
						"ActionType": "InvokeMethod",
						"ActionParam": "EncoderDec"
					},
					{
						"Name": "CompleteEvent",
						"ActionType": "ChangeState",
						"ActionParam": "AcceptedState"
					}
				]
			},
			{
				"Name": "AcceptedState",
				"EntryMethodName": "EnterCompleteToolPoints",
				
				"Event": [
					{
						"Name": "ClickStartPoint",
						"ActionType": "InvokeMethod",
						"ActionParam": "EnterSVelPointEdit"
					},
					{
						"Name": "ClickEndPoint",
						"ActionType": "InvokeMethod",
						"ActionParam": "EnterEVelPointEdit"
					},
					{
						"Name": "ClickAVelPoint",
						"ActionType": "InvokeMethod",
						"ActionParam": "EnterAVelPointEdit"
					},
					{
						"Name": "ClickEvent",
						"ActionType": "InvokeMethod",
						"ActionParam": "SelectManualToolStart"
					}
				]
			}
		]
	}
}
modify the code such that it creates dynamic state and event