END_TEST

START_TEST (test_valid_3)
{
	char *s = "[{\"key1\":2  ,  \"key2\":\"val\"}, 45, null]";
    struct json_object *j_ar = json_parser_parse(s);

    fail_unless(json_type(j_ar) == json_type_array, "bad type");
    fail_unless(json_array_length(j_ar) == 3, "bad length");

    struct json_object *j_obj = json_array_get(j_ar, 0);
    struct json_object *j_int1 = json_array_get(j_ar, 1);
    struct json_object *j_null = json_array_get(j_ar, 2);

    fail_unless(json_type(j_obj) == json_type_object, "bad type");
    fail_unless(json_type(j_int1) == json_type_int, "bad type");
    fail_unless(json_type(j_null) == json_type_null, "bad type");

    struct json_object *j_int2 = json_object_get(j_obj, "key1");
    struct json_object *j_str = json_object_get(j_obj, "key2");

    fail_unless(json_int_get(j_int1) == 45, "bad value");
    fail_unless(json_int_get(j_int2) == 2, "bad value");
    fail_unless(strcmp(json_string_get(j_str), "val") == 0, "bad value");

    json_ref_put(j_ar);
}
END_TEST

START_TEST (test_valid_4_utf)
{
	char *s = "[\"Элементы\", 5, \"массива\"]";
    struct json_object *j_ar = json_parser_parse(s);

    fail_unless(json_type(j_ar) == json_type_array, "bad type");
    fail_unless(json_array_length(j_ar) == 3, "bad length");

    struct json_object *j_str_1 = json_array_get(j_ar, 0);
    struct json_object *j_int = json_array_get(j_ar, 1);
    struct json_object *j_str_2 = json_array_get(j_ar, 2);

    fail_unless(json_type(j_str_1) == json_type_string, "bad type");
    fail_unless(json_type(j_str_2) == json_type_string, "bad type");
    fail_unless(json_type(j_int) == json_type_int, "bad type");

    fail_unless(json_int_get(j_int) == 5, "bad value_int");
    fail_unless(strcmp(json_string_get(j_str_1), "Элементы") == 0, "bad value_str_1");
    fail_unless(strcmp(json_string_get(j_str_2), "массива") == 0, "bad value_str_2");

    json_ref_put(j_ar);
}
Example #3
0
static int get_status_and_reason(struct json_object *obj, int *status, char **reason)
{
	char *result_reason;
	int result_status;
	if (json_type(obj) == json_type_array || json_object_get(obj, JSON_RPC_ERROR_MEMBER) == NULL) {
		result_status = HTTP_SUCCESS_STATUS;
		result_reason = strdup(HTTP_SUCCESS_REASON);
	} else {
		int result_code = json_int_get(json_object_get(json_object_get(obj, JSON_RPC_ERROR_MEMBER), JSON_RPC_ERROR_CODE_MEMBER));
		result_reason = strdup(json_string_get(json_object_get(json_object_get(obj, JSON_RPC_ERROR_MEMBER), JSON_RPC_ERROR_MESSAGE_MEMBER)));
		result_status = get_status_by_code(result_code);
	}

	if (result_reason == NULL || result_status == -1)
		return -1;

	*reason = result_reason;
	*status = result_status;

	return 0;
}
Example #4
0
static inline void json_print(JSONNode * node, unsigned int tab)
{
    const char *name = json_node_get_name(node);
    if (name == NULL) {
        name = "NULL";
    }
    tab_print(tab);
    if (json_node_is_string(node)) {
        printf("%s=%s\n", name, json_string_get(node));
    } else if (json_node_is_false(node)) {
        printf("%s=FALSE\n", name);
    } else if (json_node_is_true(node)) {
        printf("%s=TRUE\n", name);
    } else if (json_node_is_int(node)) {
        printf("%s=%ld\n", name, json_int_get(node));
    } else if (json_node_is_float(node)) {
        printf("%s=%g\n", name, json_float_get(node));
    } else if (json_node_is_object(node) || json_node_is_array(node)) {
        JList *children = json_node_get_children(node);
        if (json_node_is_array(node)) {
            printf("%s=[\n", name);
        } else {
            printf("%s={\n", name);
        }
        while (children) {
            JSONNode *child = (JSONNode *) j_list_data(children);
            json_print(child, tab + 1);
            children = j_list_next(children);
        }
        tab_print(tab);
        if (json_node_is_array(node)) {
            printf("]\n");
        } else {
            printf("}\n");
        }
    } else {
        printf("%s=NULL\n", name);
    }
}
Example #5
0
void CServerBrowser::LoadDDNet()
{
	// reset servers / countries
	m_NumDDNetCountries = 0;
	m_NumDDNetTypes = 0;

	// load ddnet server list
	IStorageTW *pStorage = Kernel()->RequestInterface<IStorageTW>();
	IOHANDLE File = pStorage->OpenFile("tmp/cache/ddnet-servers.json", IOFLAG_READ, IStorageTW::TYPE_ALL);

	if(File)
	{
		char aBuf[4096*4];
		mem_zero(aBuf, sizeof(aBuf));

		io_read(File, aBuf, sizeof(aBuf));
		io_close(File);


		// parse JSON
		json_value *pCountries = json_parse(aBuf);

		if (pCountries && pCountries->type == json_array)
		{
			for (int i = 0; i < json_array_length(pCountries) && m_NumDDNetCountries < MAX_DDNET_COUNTRIES; i++)
			{
				// pSrv - { name, flagId, servers }
				const json_value *pSrv = json_array_get(pCountries, i);
				const json_value *pTypes = json_object_get(pSrv, "servers");
				const json_value *pName = json_object_get(pSrv, "name");
				const json_value *pFlagID = json_object_get(pSrv, "flagId");

				if (pSrv->type != json_object || pTypes->type != json_object || pName->type != json_string || pFlagID->type != json_integer)
				{
					dbg_msg("client_srvbrowse", "invalid attributes");
					continue;
				}

				// build structure
				CDDNetCountry *pCntr = &m_aDDNetCountries[m_NumDDNetCountries];

				pCntr->Reset();

				str_copy(pCntr->m_aName, json_string_get(pName), sizeof(pCntr->m_aName));
				pCntr->m_FlagID = json_int_get(pFlagID);

				// add country
				for (unsigned int t = 0; t < pTypes->u.object.length; t++)
				{
					const char *pType = pTypes->u.object.values[t].name;
					const json_value *pAddrs = pTypes->u.object.values[t].value;

					// add type
					if(json_array_length(pAddrs) > 0 && m_NumDDNetTypes < MAX_DDNET_TYPES)
					{
						int pos;
						for(pos = 0; pos < m_NumDDNetTypes; pos++)
						{
							if(!str_comp(m_aDDNetTypes[pos], pType))
								break;
						}
						if(pos == m_NumDDNetTypes)
						{
							str_copy(m_aDDNetTypes[m_NumDDNetTypes], pType, sizeof(m_aDDNetTypes[m_NumDDNetTypes]));
							m_NumDDNetTypes++;
						}
					}

					// add addresses
					for (int g = 0; g < json_array_length(pAddrs); g++, pCntr->m_NumServers++)
					{
						const json_value *pAddr = json_array_get(pAddrs, g);
						const char* pStr = json_string_get(pAddr);
						net_addr_from_str(&pCntr->m_aServers[pCntr->m_NumServers], pStr);
						str_copy(pCntr->m_aTypes[pCntr->m_NumServers], pType, sizeof(pCntr->m_aTypes[pCntr->m_NumServers]));
					}
				}

				m_NumDDNetCountries++;
			}
		}

		if (pCountries)
			json_value_free(pCountries);
	}
}
Example #6
0
static int dump(json_t *json, char **buf, int *size)
{
    int i;
    int len;

    if (json == NULL)
        return 1;

    switch (json_typeof(json)) {
        case JSON_STRING:
            {
                char *tmp;
                char *value = json_string_get(json);

                tmp = (char *)malloc(strlen(value) + 3);
                if ( ! tmp)
                    return 1;
                len = sprintf(tmp, "\"%s\"", value);
                append_string(tmp, len, buf, size);
                free(tmp);
            }
            break;
        case JSON_NUMBER:
            {
                char tmp[32] = {0, };
                long double value = json_number_get(json);
                len = sprintf(tmp, "%.17Lg", value);
                append_string(tmp, len, buf, size);
            }
            break;
        case JSON_OBJECT:
            {
                int obj_size;
                char *key;

                obj_size = json_object_sizeof(json);

                append_string("{", 1, buf, size);
                for (i = 0; i < obj_size; i++) {
                    key = json_object_get_key(json, i);
                    if ( ! key)
                        continue;
                    json_t *j = json_object_get(json, key);
                    if ( ! j)
                        continue;

                    append_string(key, strlen(key), buf, size);
                    append_string(":", 1, buf, size);
                    dump(j, buf, size);
                    if (i + 1 < obj_size)
                        append_string(", ", 2, buf, size);
                }
                append_string("}", 1, buf, size);
            }
            break;
        case JSON_ARRAY:
            {
                int array_size;

                array_size = json_array_sizeof(json);

                append_string("[", 1, buf, size);
                for (i = 0; i < array_size; i++) {
                    json_t *j = json_array_get(json, i);
                    if ( ! j)
                        continue;

                    dump(j, buf, size);
                    if (i + 1 < array_size)
                        append_string(", ", 2, buf, size);
                }
                append_string("]", 1, buf, size);
            }
            break;
        case JSON_TRUE:
            append_string("true", 4, buf, size);
            break;
        case JSON_FALSE:
            append_string("false", 5, buf, size);
            break;
        case JSON_NULL:
            append_string("null", 4, buf, size);
            break;
    }

    return 0;
}