#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;
	//state* nextstate;
	std::vector<Event*> eventitem;
public:
	/*state(string statename,Event *e1)
	{
		
		eventitem.push_back(e1);
		e1->getdata("a","b","c");pk
	}*/

	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;
	State1 = new state;
	Event *Event1;
	Event1 = new Event;
	State1->getdata("StartToolState", "EnterStartTool");
	State1->Add(Event1);

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

	state* State2;
	State2= new state;
	Event* Event2;
	Event2 = new Event;
	Event2->getdata("MotionEvent","InvokeMethod","MoveToolPoints");
	State1->Add(Event1); 
	State1->Add(Event2);
	prtool1->Add(State2);
	
	delete Event2;
	delete State2;
	delete State1;
	delete Event1;

	// Open the file 
	ifstream file("FSMImplementationJSON.json");

	// 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;
	//errno_t err;
	fopen_s(&fp,"FSMImplementationJSON.json", "rb");

	assert(fp != nullptr);

	//// Use a FileReadStream to 
	//  // read the data from the file 
	char readBuffer[65536];
	rapidjson::FileReadStream is(fp, readBuffer,sizeof(readBuffer));
	if (fp == nullptr) {
		std::cerr << "Error opening the file" << std::endl;
		return 1;
	}
	//  // using a Document object 
	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;
}
i am getting below error
default constructor called
default constructor called
  StartToolState  EnterStartTool
FSMBase called
default constructor called
FSMBase called
default constructor called
default constructor called
 MotionEvent InvokeMethod MoveToolPoints
FSMBase called
FSMBase called
FSMBase called
 destructor called
 destructor called
 destructor called
 destructor called
Assertion failed: false, file C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\rapidjson-master\include\rapidjson\document.h, line 1231