Exemplo n.º 1
0
//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();
}