Ejemplo n.º 1
0
void YoutubeDL::fetchAvailableFormats(QString url)
{
    QVector<MediaFormat> formats;
    QJsonObject jsonObject = createJsonObject(url);
    QJsonArray jsonFormats = jsonObject["formats"].toArray();
    QJsonArray::iterator i;

    for (i = jsonFormats.begin(); i != jsonFormats.end(); ++i) {
        QJsonValue value = *i;
        QJsonObject formatObject = value.toObject();
        MediaFormat format;
        format.setFormatId(formatObject["format_id"].toString());
        format.setFormat(formatObject["format"].toString());
        format.setExtension(formatObject["ext"].toString());
        format.setNote(formatObject["format_note"].toString());
        if (formatObject.contains("height") && !formatObject["height"].isNull()) {
            QString resolution = QString::number(formatObject["width"].toDouble()) + "x"
                    + QString::number(formatObject["height"].toDouble());
            format.setResolution(resolution);
        }
        format.setVcodec(formatObject["vcodec"].toString().trimmed());
        format.setAcodec(formatObject["acodec"].toString().trimmed());
        formats.append(format);
    }
    this->formats = formats;
}
/************************************************************************************************************
 * The JsonList for the findElement Function to test are as shown below,                                    *
 *                                                                                                          *
 *  {                                                                                                       *
 *    "NAME1":"JS",                                                                                         *
 *    "AGE"  :{ "NAME2":"STEVEN",                                                                           *
 *              "NAME4":"YEN"}                                                                              *
 *  }                                                                                                       *
 *                                                                                                          *
 * -Test to find the 'Key'="AGE" in recur=0.                                                                *
 * -Test to find the 'Key'="NAME3" in recur=1.                                                              *
 *                                                                                                          *                                                                                                         *
 ************************************************************************************************************/
void test_Finding_Element_if_Key_not_Found_in_the_Recursion_JSON_List_Should_Throw_Error()
{
  printf("JSON list Finding Element test No.3\n");
  JsonObject *jsonObj;
  Token *jsonTok;
  ListElement *findKey;
  Token *findVal;
  ErrorObject *err;

  TOKEN_DECLARE;

  getToken_ExpectAndReturn(openBrace0);    //"{"
  getToken_ExpectAndReturn(NAME1);         //"NAME1"
  getToken_ExpectAndReturn(colon0);        //":"
  getToken_ExpectAndReturn(JS);            //"JS"
  getToken_ExpectAndReturn(coma0);          //","
  getToken_ExpectAndReturn(AGE);           //"AGE"
  getToken_ExpectAndReturn(colon1);        //":"
  getToken_ExpectAndReturn(openBrace1);    //"{"
  getToken_ExpectAndReturn(NAME2);         //"NAME2"
  getToken_ExpectAndReturn(colon2);        //":"
  getToken_ExpectAndReturn(STEVEN);        //"STEVEN"
  getToken_ExpectAndReturn(coma1);          //","
  getToken_ExpectAndReturn(NAME4);         //"NAME4"
  getToken_ExpectAndReturn(colon3);        //":"
  getToken_ExpectAndReturn(YEN);           //"YEN"
  getToken_ExpectAndReturn(closeBrace0);   //"}"
  getToken_ExpectAndReturn(closeBrace1);   //"}"
  getToken_ExpectAndReturn(dollarSign);

  jsonObj=createJsonObject();

  Try{
    jsonTok=jsonParse(jsonObj);
  }Catch(err){
    TEST_FAIL_JSON("unexpected error occurred =>'%s'",err->errorMsg);
    free(err);
  }

  TEST_ASSERT_EQUAL(END,jsonObj->state);

  Try{
    findKey=(ListElement *)(keyFind(((JsonToken *)jsonTok)->list, "AGE", strCompare));
    findKey=(ListElement *)(keyFind(((JsonToken *)(((OperatorToken *)(findKey->value))->token[1]))->list, "NAME3", strCompare));
    TEST_FAIL_MESSAGE("Expecting ERR_KEY_NOT_FOUND but none thrown.");
  }Catch(err){
    TEST_ASSERT_EQUAL_STRING("ERROR[14]:Key not Found.Finding 'Key'=>'NAME3'.",err->errorMsg);
    TEST_ASSERT_EQUAL(ERR_KEY_NOT_FOUND,err->errorCode);
    free(err);
  }

  free(jsonObj);
  free(jsonTok);
  free(err);
  free(findKey);
  free(findVal);

  TOKEN_FREE;
  printf("\n\n");
}
/************************************************************************************************************
 * The JsonList for the findElement Function to test are as shown below,                                    *
 *                                                                                                          *
 *  {                                                                                                       *
 *    "NAME1":"JS",                                                                                         *
 *    "AGE"  :{ "NAME2":"STEVEN",                                                                           *
 *              "NAME3":"YEN"}                                                                              *
 *  }                                                                                                       *
 *                                                                                                          *
 * -Test to find the 'Key'="AGE" in recur=0.                                                                *
 * -Test to find the 'Key'="NAME3" in recur=1.                                                              *
 * -Test to get the 'Value' for the 'Key' found in recur=1.                                                 *
 *                                                                                                          *                                                                                                         *
 ************************************************************************************************************/
void test_Finding_Element_in_Recursion_JSON_List()
{
  printf("JSON list Finding Element test No.4\n");
  JsonObject *jsonObj;
  Token *jsonTok;
  ListElement *findKey;
  Token *findVal;
  ErrorObject *err;

  TOKEN_DECLARE;

  getToken_ExpectAndReturn(openBrace0);    //"{"
  getToken_ExpectAndReturn(NAME1);         //"NAME1"
  getToken_ExpectAndReturn(colon0);        //":"
  getToken_ExpectAndReturn(JS);            //"JS"
  getToken_ExpectAndReturn(coma0);          //","
  getToken_ExpectAndReturn(AGE);           //"AGE"
  getToken_ExpectAndReturn(colon1);        //":"
  getToken_ExpectAndReturn(openBrace1);    //"{"
  getToken_ExpectAndReturn(NAME2);         //"NAME2"
  getToken_ExpectAndReturn(colon2);        //":"
  getToken_ExpectAndReturn(STEVEN);        //"STEVEN"
  getToken_ExpectAndReturn(coma1);          //","
  getToken_ExpectAndReturn(NAME3);         //"NAME3"
  getToken_ExpectAndReturn(colon3);        //":"
  getToken_ExpectAndReturn(YEN);           //"YEN"
  getToken_ExpectAndReturn(closeBrace0);   //"}"
  getToken_ExpectAndReturn(closeBrace1);   //"}"
  getToken_ExpectAndReturn(dollarSign);

  jsonObj=createJsonObject();

  Try{
    jsonTok=jsonParse(jsonObj);
    TEST_ASSERT_EQUAL(END,jsonObj->state);
    findKey=(ListElement *)(keyFind(((JsonToken *)jsonTok)->list, "AGE", strCompare));
    findKey=(ListElement *)(keyFind(((JsonToken *)(((OperatorToken *)(findKey->value))->token[1]))->list, "NAME3", strCompare));
    findVal=(Token *)(getElementValue(findKey));
    TEST_ASSERT_NOT_NULL(findVal);
  }Catch(err){
    TEST_FAIL_JSON("unexpected error occurred =>'%s'",err->errorMsg);
    free(err);
  }

  TEST_ASSERT_EQUAL_STRING(((StringToken *)(YEN))->name,((StringToken *)(findVal))->name);

  free(jsonObj);
  free(jsonTok);
  free(err);
  free(findKey);
  free(findVal);

  TOKEN_FREE;
  printf("\n\n");
}
Ejemplo n.º 4
0
json_error_t * json_from_string(char *jsonString, json_t **destObject) {

		int resultCode = 0;
		jsmn_parser parser;
		jsmntok_t tokens[TOKENS_COUNT];
		size_t tokenIndex = 0;

		jsmntok_t *failedToken = NULL;

		if(jsonString == NULL) {
			return parsingError(
				JSON_NULL_POINTER,
				"Error: Json string is NULL.",
				NULL,
				NULL
			);
		}

		memset(tokens, 0, TOKENS_COUNT*sizeof(jsmntok_t));

		json_t *root = NULL;

		jsmn_init(&parser);
		resultCode = jsmn_parse(&parser, jsonString, tokens, TOKENS_COUNT);
		if(resultCode != JSMN_SUCCESS) {
			failedToken = ((tokenIndex <= TOKENS_COUNT) ? &tokens[tokenIndex] : NULL);

			switch(resultCode) {
			case JSMN_ERROR_INVAL:
				return parsingError(
					JSON_INVALID_INPUT,
					"Error: Invalid character inside JSON string.",
					failedToken,
					jsonString
				);

			case JSMN_ERROR_NOMEM:
				return parsingError(
					JSON_MEMORY_ALLOC_FAILED,
					"Error: Not enough tokens were provided. Please add more tokens in application configuration.",
					failedToken,
					jsonString
				);

			case JSMN_ERROR_PART:
				return parsingError(
					JSON_INVALID_INPUT,
					"Error: The string is not a full JSON packet, more bytes expected.",
					failedToken,
					jsonString
				);
			case JSMN_SUCCESS:
				break;
			default:
				return parsingError(
					JSON_INLLEGAL_STATE_EXCEPTION,
					"Error: Invalid jsmp result code. Normally program should not reach here.",
					NULL,
					NULL
				);
			}

		}

		json_error_t *parseError = NULL;
		parseError = createJsonObject(&root, jsonString, tokens, TOKENS_COUNT, &tokenIndex );

		if(parseError != NULL) {
			json_object_free(root);
			return parseError;
		}

		*destObject = root;

		return NULL; /* none errors */
}
Ejemplo n.º 5
0
inline
json_error_t* allocate_object_by_type(
		json_t **objectPtr,
		jsmntok_t *token,
		jsmntok_t tokens[TOKENS_COUNT], size_t tokensCount, size_t *tokenIndex,
		char* js) {

	json_error_t* parseError = NULL;
	json_t* jsonObject = NULL;

	json_type type = decode_jsmn_type(token->type);



	switch (type) {
	case JSON_STRING:
		jsonObject = json_alloc(JSON_STRING, token->size);
		if(jsonObject == NULL) {
			parseError = parsingError(
				JSON_MEMORY_ALLOC_FAILED,
				"Error: Unable to allocate string object.",
				token,
				js
			);
		}
		break;



	case JSON_PRIMITIVE:
	{
		char *firstCharacter = js + token->start;
		switch(*firstCharacter) {
		case '-': {};
		case '0': {};
		case '1': {};
		case '2': {};
		case '3': {};
		case '4': {};
		case '5': {};
		case '6': {};
		case '7': {};
		case '8': {};
		case '9': {};
			jsonObject = json_alloc(JSON_NUMBER, token->size);
			break;
		case 't':
			jsonObject = json_alloc(JSON_TRUE, token->size);
			break;
		case 'f':
			jsonObject = json_alloc(JSON_FALSE, token->size);
			break;
		case 'n' :
			jsonObject = json_alloc(JSON_NULL, token->size);
			break;
		default:
			parseError = parsingError(
				JSON_INVALID_OBJECT_TYPE,
				"Error: Unable to allocate primitive object by first character. Wrong character at -->",
				token,
				js
			);
			break;
		}
	} break;


	case JSON_OBJECT:
	case JSON_ARRAY:
		parseError = createJsonObject(&jsonObject, js, tokens, tokensCount, tokenIndex);
		break;
	default:
		parseError = parsingError(
			JSON_INVALID_OBJECT_TYPE,
			"Error: Unable to allocate object by type. Wrong type.",
			token,
			js
		);
		break;
	}

	if(parseError == NULL) {

		add_value_from_token(jsonObject, js, token);
//		/* assign returned object */
//		if(objectPtr != NULL) {
//			json_free(objectPtr);
//		}
//		objectPtr = (json_t*) json_malloc(sizeof(json_t));
//		memcpy(objectPtr, jsonObject, sizeof(json_t));
//
//		json_free(jsonObject);

		*objectPtr = jsonObject;


	}


	return parseError;
}