SamiPasswordChangeBody* SamiPasswordChangeBody::fromJson(String* json) { this->cleanup(); String str(json->GetPointer()); int length = str.GetLength(); ByteBuffer buffer; buffer.Construct(length); for (int i = 0; i < length; ++i) { byte b = str[i]; buffer.SetByte(b); } IJsonValue* pJson = JsonParser::ParseN(buffer); fromJsonObject(pJson); if (pJson->GetType() == JSON_TYPE_OBJECT) { JsonObject* pObject = static_cast< JsonObject* >(pJson); pObject->RemoveAll(true); } else if (pJson->GetType() == JSON_TYPE_ARRAY) { JsonArray* pArray = static_cast< JsonArray* >(pJson); pArray->RemoveAll(true); } delete pJson; return this; }
void priceGetProcessor(HttpResponse* pHttpResponse, void (* handler)(void*, SamiError*)) { int code = pHttpResponse->GetHttpStatusCode(); if(code >= 200 && code < 300) { ByteBuffer* pBuffer = pHttpResponse->ReadBodyN(); IJsonValue* pJson = JsonParser::ParseN(*pBuffer); SamiPrice* out = new SamiPrice(); jsonToValue(out, pJson, L"SamiPrice*", L"SamiPrice"); if (pJson) { if (pJson->GetType() == JSON_TYPE_OBJECT) { JsonObject* pObject = static_cast< JsonObject* >(pJson); pObject->RemoveAll(true); } else if (pJson->GetType() == JSON_TYPE_ARRAY) { JsonArray* pArray = static_cast< JsonArray* >(pJson); pArray->RemoveAll(true); } handler(out, null); } else { SamiError* error = new SamiError(0, new String(L"No parsable response received")); handler(null, error); } } else { SamiError* error = new SamiError(code, new String(pHttpResponse->GetStatusText())); handler(null, error); } }
SamiMeasurementValue::SamiMeasurementValue(String* json) { init(); String str(json->GetPointer()); int length = str.GetLength(); ByteBuffer buffer; buffer.Construct(length); for (int i = 0; i < length; ++i) { byte b = str[i]; buffer.SetByte(b); } IJsonValue* pJson = JsonParser::ParseN(buffer); fromJsonObject(pJson); if (pJson->GetType() == JSON_TYPE_OBJECT) { JsonObject* pObject = static_cast< JsonObject* >(pJson); pObject->RemoveAll(true); } else if (pJson->GetType() == JSON_TYPE_ARRAY) { JsonArray* pArray = static_cast< JsonArray* >(pJson); pArray->RemoveAll(true); } delete pJson; }
void VKURequestData::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction) { result r; AppLog("OnTransactionCompleted"); data.Flip(); IJsonValue *pJson = JsonParser::ParseN(data); if(pJson != null) { if(pJson->GetType() == JSON_TYPE_OBJECT) { listener->ProcessResponseN(requestId, static_cast< JsonObject* >(pJson), targetRequestId); } else { delete pJson; } } else { AppLogException("Parse JSON is failed.", GetErrorMessage(r)); } VKUApi::GetInstance().EndRequest(requestId); }
HRESULT TestJson() { HRESULT hr; HSTRING className; IfFailedReturn(WindowsCreateString( RuntimeClass_Windows_Data_Json_JsonObject, wcslen(RuntimeClass_Windows_Data_Json_JsonObject), &className)); IInspectable* inspectable; IfFailedReturn(RoActivateInstance(className, &inspectable)); ULONG iidCount; IID* iids; IfFailedReturn(inspectable->GetIids(&iidCount, &iids)); wprintf(L"Iids: %d\r\n", iidCount); IJsonValue* jsonValue; IfFailedReturn(inspectable->QueryInterface(__uuidof(jsonValue), reinterpret_cast<void**>(&jsonValue))); HSTRING jsonString; IfFailedReturn(jsonValue->Stringify(&jsonString)); // TODO: Confirm that length parameter is required. I heard it is needed beacuase it is not guaranteed that the // PCWSTR will end on null character. UINT32 length; const wchar_t* rawJsonString; rawJsonString = WindowsGetStringRawBuffer(jsonString, &length); wprintf(L"Stringify: %s \r\nlength: %d\r\n", rawJsonString, length); IfFailedReturn(WindowsDeleteString(className)); IfFailedReturn(WindowsDeleteString(jsonString)); return S_OK; }
//Recursively traverse IJsonValue obtained by parser (using ParseN) void ProjectGiraffeTab1::TraverseFunction(IJsonValue* pValue) { TryReturnVoid(_tableView, "tableView is null"); TryReturnVoid(pValue, "input jsonvalue pointer is null"); User *dummyUser = new User(); dummyUser->setUsername(L"CS210 Student"); Graffiti *lastCreatedGraffiti = NULL; switch (pValue->GetType()) { case JSON_TYPE_OBJECT: { JsonObject* pObject = static_cast< JsonObject* >(pValue); IMapEnumeratorT< const String*, IJsonValue* >* pMapEnum = pObject->GetMapEnumeratorN(); while (pMapEnum->MoveNext() == E_SUCCESS) { AppLog("IN JSON OBJECT"); const String* key = null; IJsonValue* value = null; pMapEnum->GetKey(key); pMapEnum->GetValue(value); String* pListKey = new (std::nothrow) String(*key); _pJsonKeyList->Add(*pListKey); AppLog("Key: %ls", pListKey->GetPointer()); if(pListKey->Equals("message",true)){ AppLog("Message received"); lastCreatedGraffiti = new Graffiti(); lastCreatedGraffiti->setUser(dummyUser); JsonString* pVal = static_cast< JsonString* >(value); String* pListValue = new (std::nothrow) String(*pVal); lastCreatedGraffiti->setText(String(*pListValue)); _items->Add(lastCreatedGraffiti); AppLog("message : %ls", lastCreatedGraffiti->text().GetPointer()); } else if (value && pListKey->Equals("latitude", true)) { JsonNumber *jsonNum = static_cast<JsonNumber *>(value); Double *latValue = static_cast<Double *>(jsonNum); if (latValue && lastCreatedGraffiti) { lastCreatedGraffiti->setLatitude(latValue->ToFloat()); AppLog("latitude : %f", lastCreatedGraffiti->longitude()); } } else if (value != NULL && pListKey->Equals("longitude", true)) { JsonNumber *jsonNum = static_cast<JsonNumber *>(value); Double *lonValue = static_cast<Double *>(jsonNum); if (lonValue && lastCreatedGraffiti) { lastCreatedGraffiti->setLongitude(lonValue->ToFloat()); AppLog("longitude : %f", lastCreatedGraffiti->longitude()); } } } delete pMapEnum; } break; case JSON_TYPE_STRING: { JsonString* pVal = static_cast< JsonString* >(pValue); if (_isArray == 0) { String* pStr = new (std::nothrow) String(*pVal); _pValueList->Add(*pStr); } } break; case JSON_TYPE_ARRAY: { JsonArray* pJsonArray = static_cast< JsonArray* >(pValue); pJsonArray->GetCount(); IEnumeratorT< IJsonValue* >* pEnum = pJsonArray->GetEnumeratorN(); while (pEnum->MoveNext() == E_SUCCESS) { IJsonValue* pValue = null; pEnum->GetCurrent(pValue); if ((pValue->GetType() == JSON_TYPE_STRING) || (pValue->GetType() == JSON_TYPE_NUMBER) || (pValue->GetType() == JSON_TYPE_BOOL) || (pValue->GetType() == JSON_TYPE_NULL)) { _isArray = 1; } TraverseFunction(pValue); } delete pEnum; } break; case JSON_TYPE_NUMBER: { JsonNumber* pVal = static_cast< JsonNumber* >(pValue); String* pStr = new (std::nothrow) String((pVal->ToString())); if (_isArray == 0) _pValueList->Add(*pStr); } break; case JSON_TYPE_BOOL: { String* pStr = null; JsonBool* pVal = static_cast< JsonBool* >(pValue); if (*pVal == true) pStr = new (std::nothrow) String("true"); else pStr = new (std::nothrow) String("false"); if (_isArray == 0) _pValueList->Add(*pStr); } break; case JSON_TYPE_NULL: { String* pStr = null; pStr = new (std::nothrow) String("null"); if (_isArray == 0) _pValueList->Add(*pStr); } break; default: break; } _tableView->UpdateTableView(); }
void ProjectGiraffeTab1::OnTransactionReadyToRead (HttpSession &httpSession, HttpTransaction &httpTransaction, int availableBodyLen) { HttpResponse* pHttpResponse = httpTransaction.GetResponse(); HttpHeader* pHttpHeader = null; AppLog("Checking HTTP Status Code"); if (pHttpResponse->GetHttpStatusCode() == HTTP_STATUS_OK) { // MessageBox msgBox; // msgBox.Construct(L"HTTP STATUS", L"HTTP Request OK", MSGBOX_STYLE_NONE, 3000); // int modalresult = 0; // msgBox.ShowAndWait(modalresult); ByteBuffer* pBody = null; String statusText = pHttpResponse->GetStatusText(); String version = pHttpResponse->GetVersion(); pHttpHeader = pHttpResponse->GetHeader(); pBody = pHttpResponse->ReadBodyN(); //delete pBody; //Parses from ByteBuffer IJsonValue* pValue = JsonParser::ParseN(*pBody); AppLog("Received HTTP response."); AppLog("Before traverse %d", _tableView->GetItemCount()); _pJsonKeyList->RemoveAll(true); _pValueList->RemoveAll(true); _items->RemoveAll(true); // Populate the panel TraverseFunction(pValue); AppLog("After traverse"); //clean up allocated jsonValues if (pValue->GetType() == JSON_TYPE_OBJECT) { // Converts the pValue to JsonObject JsonObject* pObject = static_cast< JsonObject* >(pValue); pObject->RemoveAll(true); } else if (pValue->GetType() == JSON_TYPE_ARRAY) { // Converts the pValue to JsonArray JsonArray* pArray = static_cast< JsonArray* >(pValue); pArray->RemoveAll(true); } delete pBody; delete pValue; _tableView->ScrollToItem(0); }else{ AppLog("HTTP Status not OK"); } //Remove load icon here. //_loadingPopupThread->Quit(); //_loadingPopupThread->Join(); }