// --------------------------------------------------------------------------- TJSONObject* __fastcall TNoteJSON::NoteToJSON(const TNote *ANote) { TJSONObject * jsonObj = new TJSONObject(); jsonObj->AddPair(TNames::Title, ANote->Title); jsonObj->AddPair(TNames::Content, ANote->Content); jsonObj->AddPair(TNames::Id, ANote->ID); return jsonObj; }
void __fastcall TUserNotifyThread::Execute() { TJSONObject *Msg; String EventType; FreeOnTerminate = True; if (FWasAdded) EventType = "user_login"; else EventType = "user_logout"; //create a JSON object with a message describing the user login/logout event Msg = new TJSONObject(); Msg->AddPair(new TJSONPair("notificationType", EventType)); Msg->AddPair(new TJSONPair("user", FUserName)); //give time for the client's callback to be added or removed Sleep(1000); //send a message to all clients that a user either logged in, or logged out FServer->BroadcastMessage(CHAT_ROOM_ID, Msg); };
//--------------------------------------------------------------------------- void __fastcall TChatRoomForm::AdminMessageButtonClick(TObject *Sender) { String Msg; TJSONObject *JSONMsg; Msg = Trim(AdminMessageField->Text); //no message to send, so just exit if (Msg == EmptyStr) return; //wrap the message in a JSON object JSONMsg = new TJSONObject(); JSONMsg->AddPair(new TJSONPair("notificationType", "message")); JSONMsg->AddPair(new TJSONPair("from", SUPER_USER_NAME)); JSONMsg->AddPair(new TJSONPair("message", Msg)); //Send the typed message to all logged in users //NOTE: the call to broadcast should really be done in its own thread to avoid locking the UI if (ServerContainerForm->ChatRoomServer->BroadcastMessage(CHAT_ROOM_ID, JSONMsg)) AdminMessageField->Text = EmptyStr; else ShowMessage("Failed to broadcast message. Sorry!"); }
void TNotesResource1::Post(TEndpointContext* AContext, TEndpointRequest* ARequest, TEndpointResponse* AResponse) { try { TJSONObject * lJson = NULL; if(ARequest->Body->TryGetObject(lJson)) { CheckNotesManager(AContext); TNote * lNote = TNoteJSON::JSONToNote(lJson); String lId = ""; FNotesStorage->AddNote(*lNote, lId); lJson = new TJSONObject(); lJson->AddPair(TNoteJSON::TNames::Id, lId); AResponse->Body->SetValue(lJson, true); } else { AResponse->RaiseBadRequest("JSON expected"); } } catch (...) { HandleException(); } }
//--------------------------------------------------------------------------- //сохраняет текущий проект на диск void ProjectManager::saveProject(UnicodeString fileName) { //открываем файловым поток TFileStream *fs = new TFileStream(fileName, fmCreate); try { //заносим текущий проетк в JSON объект TJSONObject *js = new TJSONObject(); TJSONArray *criteriaNamesJSON = new TJSONArray(); TJSONArray *alternativeNamesJSON = new TJSONArray(); //добавляем имена критериев const vector<UnicodeString> &criteriaNames = getCurrentProject().getCriteriaNames(); vector<UnicodeString>::const_iterator iter; for (iter = criteriaNames.begin(); iter != criteriaNames.end(); ++iter) { criteriaNamesJSON->Add(*iter); } js->AddPair(L"criteriaNames", criteriaNamesJSON); //добавляем имена альтернатив const vector<UnicodeString> &alternativeNames = getCurrentProject().getAlternativeNames(); for (iter = alternativeNames.begin(); iter != alternativeNames.end(); ++iter) { alternativeNamesJSON->Add(*iter); } js->AddPair(L"alternativeNames", alternativeNamesJSON); //добавляем оценки критериев (баллы и рассчитаныый рейтинг(приоритеты)) Estimates &criteriaEstimates = currentProject->getCriteriaEstimates(); vector< vector<int> > &rates = criteriaEstimates.getRates(); vector<double> &priorities = criteriaEstimates.getPriorities(); TJSONObject *crteriaEstimatesJSON = new TJSONObject(); TJSONArray *crteriaEstimatesArray = new TJSONArray(); for (int i = 0; i < priorities.size(); ++i) { crteriaEstimatesArray->Add(FloatToStr(priorities[i])); } crteriaEstimatesJSON->AddPair(L"priorities", crteriaEstimatesArray); TJSONArray *crteriaRatesTable = new TJSONArray(); for (int i = 0; i < rates.size(); ++i) { vector<int> &v = rates[i]; TJSONArray *crteriaRatesRow = new TJSONArray(); for (int j = 0; j < v.size(); ++j) { crteriaRatesRow->Add(IntToStr(v[j])); } crteriaRatesTable->Add(crteriaRatesRow); } crteriaEstimatesJSON->AddPair(L"rates", crteriaRatesTable); js->AddPair(L"criteriaEstimates", crteriaEstimatesJSON); //добавляем оценки альтернатив vector<Estimates> alternativeEstimates = getCurrentProject().getAlternativeEstimates(); TJSONArray *tableData = new TJSONArray(); for (int i = 0; i < alternativeEstimates.size(); i++) { TJSONObject *alternativeEstimatesJSON = new TJSONObject(); TJSONArray *prioritiesJSON = new TJSONArray(); const vector<double> &priorities = alternativeEstimates[i].getPriorities(); for (int j = 0; j < priorities.size(); j++) { prioritiesJSON->Add(FloatToStr(priorities[j])); } alternativeEstimatesJSON->AddPair("priorities", prioritiesJSON); vector< vector<int> > &rates = alternativeEstimates[i].getRates(); TJSONArray *alternativeRatesTable = new TJSONArray(); for (int j = 0; j < rates.size(); ++j) { vector<int> &v = rates[j]; TJSONArray *alternativeRatesRow = new TJSONArray(); for (int k = 0; k < v.size(); ++k) { alternativeRatesRow->Add(IntToStr(v[k])); } alternativeRatesTable->Add(alternativeRatesRow); } alternativeEstimatesJSON->AddPair(L"rates", alternativeRatesTable); tableData->AddElement(alternativeEstimatesJSON); } js->AddPair(L"alternativeEstimates", tableData); //сохраняем имя и метод js->AddPair(L"projectName", getCurrentProject().getName()); js->AddPair(L"method", IntToStr(getCurrentProject().getMethod())); //сохраняем JSON объект в файл как строку UnicodeString projectJSON = js->ToString(); fs->Write(projectJSON.BytesOf(), projectJSON.Length()); js->Free(); //устанавливаем, что проект сохранён setIsCurrentProjectSaved(true); } __finally { fs->Free(); } }