//--------------------------------------------------------------------------- TJSONObject *parseJsonValue (TJSONValue *value) { TJSONObject *jsonObj = new TJSONObject; TBytes jsonBytes = TEncoding::UTF8->GetBytes(value->ToString()); jsonObj->Parse(jsonBytes, 0); return jsonObj; }
//--------------------------------------------------------------------------- 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; }