Esempio n. 1
0
//---------------------------------------------------------------------------
//загрузка проекта из файла, действия аналогичные, что и в сохранении, только
//в обратном порядке(парсим JSON объект и заполняем пустой проект)
Project & ProjectManager::loadProject(UnicodeString fileName)
{
    TFileStream *fs = new TFileStream(fileName, fmOpenRead);
	try {
		int n = fs->Size;
		char *chars = new char[n+1];

		fs->Read(chars, n);
		chars[n] = '\0';
		UnicodeString str(chars);

		TJSONObject *js = (TJSONObject*) TJSONObject::ParseJSONValue(str);

		closeProject();
		currentProject = new Project();

		TJSONArray *criteriaNamesJSON = (TJSONArray*) js->Get(L"criteriaNames")->JsonValue;
		vector<UnicodeString> &criteriaNames= getCurrentProject().getCriteriaNames();
		for (int i = 0; i < criteriaNamesJSON->Size(); ++i) {
			criteriaNames.push_back(criteriaNamesJSON->Get(i)->Value());
		}

		TJSONArray *alternativeNamesJSON = (TJSONArray*) js->Get(L"alternativeNames")->JsonValue;
		vector<UnicodeString> &alternativeNames = getCurrentProject().getAlternativeNames();
		for (int i = 0; i < alternativeNamesJSON->Size(); ++i) {
			alternativeNames.push_back(alternativeNamesJSON->Get(i)->Value());
		}

		TJSONObject *crteriaEstimatesJSON = (TJSONObject*) js->Get(L"criteriaEstimates")->JsonValue;
		TJSONArray *crteriaEstimatesArray = (TJSONArray*) crteriaEstimatesJSON->Get(L"priorities")->JsonValue;
		Estimates &criteriaEstimates = currentProject->getCriteriaEstimates();
		vector<double> &priorities = criteriaEstimates.getPriorities();
		for (int i = 0; i < crteriaEstimatesArray->Size(); ++i) {
			priorities.push_back(StrToFloat(crteriaEstimatesArray->Get(i)->Value()));
		}

		TJSONArray *crteriaRatesTable = (TJSONArray*) crteriaEstimatesJSON->Get(L"rates")->JsonValue;
		vector< vector<int> > &rates = criteriaEstimates.getRates();
		for (int i = 0; i < crteriaRatesTable->Size(); ++i) {
			vector<int> v;
			TJSONArray *crteriaRatesRow = (TJSONArray*) crteriaRatesTable->Get(i);

			for (int j = 0; j < crteriaRatesRow->Size(); ++j) {
				v.push_back(StrToInt(crteriaRatesRow->Get(j)->Value()));
			}

			rates.push_back(v);
		}

		TJSONArray *alternativeEstimatesJSON = (TJSONArray*) js->Get(L"alternativeEstimates")->JsonValue;
		vector<Estimates> &alternativeEstimates = getCurrentProject().getAlternativeEstimates();
		for (int i = 0; i < alternativeEstimatesJSON->Size(); ++i) {
			TJSONObject *alternativeJSON = (TJSONObject*) alternativeEstimatesJSON->Get(i);
			alternativeEstimates.push_back(Estimates());

			TJSONArray *prioritiesJSON = (TJSONArray*) alternativeJSON->Get(L"priorities")->JsonValue;
			vector<double> &priorities = alternativeEstimates[i].getPriorities();
			for (int j = 0; j < prioritiesJSON->Size(); ++j) {
				UnicodeString str = prioritiesJSON->Get(j)->Value();
				priorities.push_back(StrToFloat(str));
			}

			TJSONArray *alternativeRatesTable = (TJSONArray*) alternativeJSON->Get(L"rates")->JsonValue;
			vector< vector<int> > &rates = alternativeEstimates[i].getRates();
			for (int j = 0; j < alternativeRatesTable->Size(); ++j) {
				vector<int> v;
				TJSONArray *alternativeRatesRow = (TJSONArray*) alternativeRatesTable->Get(j);

				for (int k = 0; k < alternativeRatesRow->Size(); ++k) {
					v.push_back(StrToInt(alternativeRatesRow->Get(k)->Value()));
				}

				rates.push_back(v);
			}
		}

		getCurrentProject().setName(js->Get(L"projectName")->JsonValue->Value());
		getCurrentProject().setMethod(StrToInt(js->Get(L"method")->JsonValue->Value()));

		delete [] chars;
		js->Free();

		setIsProjectOpen(true);
		setIsCurrentProjectSaved(true);
		return getCurrentProject();
	} __finally {
		fs->Free();
	}

	return getCurrentProject();
}
Esempio n. 2
0
File: Main.cpp Progetto: efpies/KG
//---------------------------------------------------------------------------
void __fastcall TMainForm::ParseJSON (const UnicodeString& fileName)
{
	TStringList *jsonText = new TStringList;
	jsonText->LoadFromFile(fileName);

	TBytes jsonBytes = TEncoding::UTF8->GetBytes(jsonText->Text);
	delete jsonText;

	TJSONObject *json = new TJSONObject;
	json->Parse(jsonBytes, 0);

	TJSONObject *topObjectValueObj;

	// Points
	topObjectValueObj = parseJsonValue(json->Get("points")->JsonValue);

	for (int i = 0; i < topObjectValueObj->Size(); ++i) {
		TJSONPair *pair = topObjectValueObj->Get(i);
		TJSONArray *array = jsonArrayFromPair(pair);
//		Vertice *point = new Vertice(INTVALARR(array, 0),
//											 INTVALARR(array, 1),
//											 INTVALARR(array, 2));
//
//		points[pair->JsonString->Value()] = point;
	}

	delete topObjectValueObj;

	// Edges
	topObjectValueObj = parseJsonValue(json->Get("edges")->JsonValue);

	map<UnicodeString, TColor> *colors = new map<UnicodeString, TColor>;
	(*colors)["red"] = clRed;
	(*colors)["black"] = clBlack;
	(*colors)["green"] = clGreen;
	(*colors)["blue"] = clBlue;
	(*colors)["fuchsia"] = clFuchsia;
	(*colors)["lime"] = clLime;

	for (int i = 0; i < topObjectValueObj->Size(); ++i) {
		TJSONPair *pair = topObjectValueObj->Get(i);
		TJSONObject *object = parseJsonValue(pair->JsonValue);
		TJSONArray *array = jsonArrayFromPair(object->Get("vertices"));
//		Edge *edge = new Edge (points[STRVALARR(array, 0)], points[STRVALARR(array, 1)]);
//
//		if (object->Get("color")) {
//			edge->setPen((*colors)[STRVALOBJ(object, "color")], 1, psSolid);
//		}
//
//		edges[pair->JsonString->Value()] = edge;
	}

	delete colors;
	delete topObjectValueObj;

	// Objects
	topObjectValueObj = parseJsonValue(json->Get("objects")->JsonValue);

	for (int i = 0; i < topObjectValueObj->Size(); ++i) {
		TJSONPair *pair = topObjectValueObj->Get(i);
		TJSONObject *object = parseJsonValue(pair->JsonValue);
		TJSONArray *array = jsonArrayFromPair(object->Get("edges"));

		GraphicObject *graphicObject = new GraphicObject;

		for (int j = 0; j < array->Size(); ++j) {
			graphicObject->addEdge(edges[STRVALARR(array, j)]);
		}

		objects[pair->JsonString->Value()] = graphicObject;
	}

	delete topObjectValueObj;
}