#include "JsonParseObject.h"
#include "cJSON.h"

JsonParseObject::JsonParseObject()
{

}

JsonParseObject::~JsonParseObject()
{
}

bool JsonParseObject::getKeys(vector<string>& keys)
{
	return true;
}

void JsonParseObject::parse(string& jsonStr)
{
	// 将json字符串转换位json结构体
	const char* json = jsonStr.c_str();
	mResultJson =  cJSON_Parse(json);
}

void JsonParseObject::read(map<string, string>& resMap)
{

}

void JsonParseObject::read(map<string, vector<double>>& resMap)
{
	if (!mResultJson)
		return;

	cJSON* parent = mResultJson->child;
	if (!parent)
		return;

	while (parent)
	{
		string key  = parent->string;
	
		cJSON* child = parent->child;
		if (child && child->next)
		{
			vector<double> minMaxValVec;

			double minVal = child->valuedouble;
			double maxVal = child->next->valuedouble;

			minMaxValVec.push_back(minVal);
			minMaxValVec.push_back(maxVal);
			
			resMap.insert(pair<string, vector<double>>(key, minMaxValVec));
			printf("key:%s, min: %f ,max: %f \n", key, minVal, maxVal);
		}
		parent = parent->next;
	}
}

void JsonParseObject::read(Condition& resMap)
{
	if (!mResultJson)
		return;

	cJSON* parent = mResultJson->child;
	if (!parent)
		return;

	while (parent)
	{
		string key = parent->string;
		double Val = parent->valuedouble;
		resMap.mCondMap.insert(std::pair<std::string, double>(key, Val));
		parent = parent->next;
	}
}

void JsonParseObject::clear()
{
	cJSON_Delete(mResultJson);
}