Пример #1
0
/*******************************************************************************
 **
 ** Function         create_device
 **
 ** Description     Create  Device, 
 **                 Hardcoded 
 **                  to add reading from configuration file. 
 **
 ** Returns          void
 **
 *******************************************************************************/
static int create_device(void)
{
    char *master_key;
    char fname[50];
    FILE *device_fp; 
    printf("Creating M2X device \n");
 
    sprintf(fname, "/etc/m2x/dev_%s", name);
    device_fp = fopen (fname, "r");
    if (device_fp) 
    {
        fscanf(device_fp, "%s", device_id);
        APPL_TRACE_INFO("Device '%s' Exists. Device ID:%s\n",name, device_id );
        fclose(device_fp);
        return 2;
    } 
    else 
    {
        /* 1.3 Create a Device */
        sprintf(buf, "{\"name\": \"%s\", \"description\": \"%s\", \"tags\": \"%s\", \"visibility\": \"%s\"}",
          name, description, tags, visibility);
        APPL_TRACE_INFO("Create device: %s\n", buf);
        response = m2x_device_create(ctx, buf);
        APPL_TRACE_INFO("Status code: %d\n", response.status);

        if (m2x_is_success(&response) && response.raw) 
        {
            APPL_TRACE_INFO("%s\n", response.raw);
            APPL_TRACE_INFO("Feed ID    : %s\n", json_object_get_string(json_value_get_object(
                response.json), "id"));

            memcpy(device_id, json_object_get_string(json_value_get_object(response.json), "id"),
                        sizeof(device_id));
            APPL_TRACE_INFO("%s\n", &device_id);
            
            /* Store the Device Id in the file */
            /*sprintf(fname, "/etc/m2x/dev_%s", name);*/
            device_fp = fopen (fname, "w+");
            if (device_fp) {  
                fprintf(device_fp, "%s", device_id);
                fclose(device_fp);
            } else
            { 
                printf("create_device: Failed to open file");
            }  
            device_count++;
        } 
       
        /* Release the response so that same struct is used for next response */
        m2x_release_response(&response);
        /* Sleep for about a min, since it takes ~40 sec to create the device */
        sleep(45);
        return 1;
    }
}
Пример #2
0
static void handle_lobi_error(ANativeActivity *nativeActivity, int code, const char *response) {
    // see 
    // https://github.com/nakamap/docs/wiki/Android-SDK-Docs#wiki-nakamap-callback
    switch (code) {
    case LOBI_NETWORK_ERROR:
        // do something
        break;
    case LOBI_RESPONSE_ERROR:
        // do something
        break;
    case LOBI_FATAL_ERROR:
        {
            JSON_Value *responseValue = json_parse_string(response);
            JSON_Object *jsonObject = json_value_get_object(responseValue);
            // do something.
            // only in this case, response is not null and has
            // an array of messages with the key 'error'
            JSON_Array *errors = json_object_get_array((const JSON_Object *)jsonObject, "error");
            if (errors) {
                for (size_t i = 0, len = json_array_get_count(errors);
                     i < len; ++i) {
                    const char *error = json_array_get_string(errors, i);
                    lobi_showToast(nativeActivity, error);
                    LOGV("error #%d: %s", i, error);
                }
            }

            json_value_free(responseValue);
            break;
        }
    }
}
Пример #3
0
static GString *gmime_message_to_json(GMimeMessage *message, guint content_option) {
  MessageData *mdata = convert_message(message, content_option);


  JSON_Value *root_value = json_value_init_object();
  JSON_Object *root_object = json_value_get_object(root_value);

  json_object_set_value(root_object,  "from",        addresses_list_to_json(mdata->from));
  json_object_set_value(root_object,  "to",          addresses_list_to_json(mdata->to));
  json_object_set_value(root_object,  "replyTo",     addresses_list_to_json(mdata->reply_to));
  json_object_set_value(root_object,  "cc",          addresses_list_to_json(mdata->cc));
  json_object_set_value(root_object,  "bcc",         addresses_list_to_json(mdata->bcc));
  json_object_set_string(root_object, "messageId",   mdata->message_id);
  json_object_set_string(root_object, "subject",     mdata->subject);
  json_object_set_string(root_object, "date",        mdata->date);

  json_object_set_value(root_object, "inReplyTo",   references_to_json(mdata->in_reply_to));
  json_object_set_value(root_object, "references",  references_to_json(mdata->references));

  json_object_set_value(root_object,  "text",        message_body_to_json(mdata->text));
  json_object_set_value(root_object,  "html",        message_body_to_json(mdata->html));
  json_object_set_value(root_object,  "attachments", message_attachments_list_to_json(mdata->attachments));

  free_message_data(mdata);
  gchar *serialized_string = json_serialize_to_string(root_value);
  json_value_free(root_value);

  GString *json_string = g_string_new(serialized_string);
  g_free(serialized_string);

  return json_string;
}
Пример #4
0
gint jobdesc_streams_count (gchar *job, gchar *pipeline)
{
        JSON_Value *val;
        JSON_Object *obj;
        JSON_Array *array;
        gsize size, i;
        gint count, index;
        gchar *bin, *ptype;

        val = json_parse_string_with_comments (job);
        obj = json_value_get_object (val);
        if (g_str_has_prefix (pipeline, "encoder")) {
                ptype = "appsrc";
                array = json_object_dotget_array (obj, "encoders");
                sscanf (pipeline, "encoder.%d", &index);
                obj = json_array_get_object (array, index);

        } else if (g_str_has_prefix (pipeline, "source")) {
                ptype = "appsink";
                obj = json_object_get_object (obj, "source");
        }
        array = json_object_dotget_array (obj, "bins");
        size = json_array_get_count (array);
        count = 0;
        for (i = 0; i < size; i++) {
                bin = (gchar *)json_array_get_string (array, i);
                if (g_strrstr (bin, ptype) != NULL)
                        count += 1;
        }
        json_value_free (val);

        return count;
}
static JSON_Value* querySpecification_toJson(const PROVISIONING_QUERY_SPECIFICATION* query_spec)
{
    JSON_Value* root_value = NULL;
    JSON_Object* root_object = NULL;

    //Setup
    if ((root_value = json_value_init_object()) == NULL)
    {
        LogError("json_value_init_object failed");
    }
    else if ((root_object = json_value_get_object(root_value)) == NULL)
    {
        LogError("json_value_get_object failed");
        json_value_free(root_value);
        root_value = NULL;
    }

    //Set data
    else if (json_object_set_string(root_object, QUERY_SPECIFICATION_JSON_KEY_QUERY, query_spec->query_string) != JSONSuccess)
    {
        LogError("Failed to set %s in JSON string", QUERY_SPECIFICATION_JSON_KEY_QUERY);
        json_value_free(root_value);
        root_value = NULL;
    }

    return root_value;
}
Пример #6
0
// returns the name of the zone that fits the given latitude and longitude best
static char *GetZoneName(double latitude, double longitude)
{
    char *zoneName = NULL;
    char *zones = GetUrl(URL_ZONES);

    if (zones != NULL)
    {
        JSON_Value *rootJson = json_parse_string(zones);
        free(zones);
        zones = NULL;

        if (rootJson != NULL)
        {
            if (rootJson->type == JSONObject)
            {
                double bestDistance = -1.0;
                ParseZones(&zoneName, &bestDistance, json_value_get_object(rootJson), latitude, longitude);
            }

            json_value_free(rootJson);
            rootJson = NULL;
        }
    }

    return zoneName;
}
int APP_ParseCommand(char * pReadBuffer)
{
    if(pReadBuffer == NULL)
        return COMMAND_BAD_POINTER;
    JSON_Value *rootValue = json_parse_string(pReadBuffer);
    if (json_value_get_type(rootValue) != JSONObject)
            return COMMAND_BAD_JSON;
    JSON_Object * tObject = json_value_get_object(rootValue);
        
    if(json_object_dotget_string(tObject, "message.command") != NULL)
    {
        if(strcmp(json_object_dotget_string(tObject, "message.command"), "hello") == 0)
        {
            return COMMAND_HELLO;
        }
        else if (strcmp(json_object_dotget_string(tObject, "message.command"), "configuration") == 0)
        {
            return COMMAND_CONFIGURE;
        }
        else if (strcmp(json_object_dotget_string(tObject, "message.command"), "debug_set") == 0)
        {
            return COMMAND_DEBUG_SET;
        }
        else
        {
            return COMMAND_INVALID;
        }
    }
    else
    {
        return COMMAND_INVALID;
    }
    return COMMAND_INVALID;
}
Пример #8
0
void AppList::add_app_from_file(std::string file_path)
{
    JSON_Value *root = json_parse_file(file_path.c_str());

    if (!root) {
        std::cout << "Couldn't parse returned JSON";
        return;
    }

    if (json_value_get_type(root) != JSONObject) {
        std::cout << "Returned JSON isn't a JSON object";
        json_value_free(root);
        return;
    }

    JSON_Object *node = json_value_get_object(root);

    if (!node) {
        std::cout << "Couldn't get node";
        json_value_free(root);
        return;
    }

    this->add_app(node);

    json_value_free(root);
}
Пример #9
0
gchar * jobdesc_element_caps (gchar *job, gchar *element)
{
        JSON_Value *val;
        JSON_Object *obj;
        JSON_Array *array;
        gchar *p, *caps;
        gint index;

        val = json_parse_string_with_comments (job);
        obj = json_value_get_object (val);
        if (g_str_has_prefix (element, "encoder")) {
                array = json_object_dotget_array (obj, "encoders");
                sscanf (element, "encoder.%d", &index);
                obj = json_array_get_object (array, index);

        } else {
                obj = json_object_get_object (obj, "source");
        }
        p = g_strrstr (element, "elements");
        caps = (gchar *)json_object_dotget_string (obj, p);
        if (caps == NULL) {
                p = NULL;

        } else {
	        p = g_strdup (caps);
        }
        json_value_free (val);

        return p;
}
Пример #10
0
int parse_gateway_configuration(const char * conf_file) {
	const char conf_obj[] = "gateway_conf";
	JSON_Value *root_val;
	JSON_Object *root = NULL;
	JSON_Object *conf = NULL;
	const char *str; /* pointer to sub-strings in the JSON data */
	unsigned long long ull = 0;

	/* try to parse JSON */
	root_val = json_parse_file_with_comments(conf_file);
	root = json_value_get_object(root_val);
	if (root == NULL) {
		MSG("ERROR: %s id not a valid JSON file\n", conf_file);
		exit(EXIT_FAILURE);
	}
	conf = json_object_get_object(root, conf_obj);
	if (conf == NULL) {
		MSG("INFO: %s does not contain a JSON object named %s\n", conf_file, conf_obj);
		return -1;
	} else {
		MSG("INFO: %s does contain a JSON object named %s, parsing gateway parameters\n", conf_file, conf_obj);
	}

	/* getting network parameters (only those necessary for the packet logger) */
	str = json_object_get_string(conf, "gateway_ID");
	if (str != NULL) {
		sscanf(str, "%llx", &ull);
		lgwm = ull;
		MSG("INFO: gateway MAC address is configured to %016llX\n", ull);
	}

	json_value_free(root_val);
	return 0;
}
Пример #11
0
corto_int16 json_deserReference(void* p, corto_type t, JSON_Value* v)
{
    switch(json_value_get_type(v)) {
    case JSONString: {
        const char* reference = json_value_get_string(v);
        corto_object o = corto_resolve(NULL, (corto_string)reference);
        if (!o) {
            corto_error("unresolved reference \"%s\"", reference);
            goto error;
        }

        if (!corto_instanceof(t, o)) {
            corto_error("%s is not an instance of %s", reference, corto_idof(t));
        }

        corto_setref(p, o);
        corto_release(o);
        break;
    }
    case JSONObject: {
        JSON_Object* obj = json_value_get_object(v);

        JSON_Value* type = json_object_get_value(obj, "type");
        if (json_value_get_type(type) != JSONString) {
            corto_seterr("type parameter of anonymous object must be a string");
            goto error;
        }
        corto_type cortoType = corto_resolve(NULL, (char*)json_value_get_string(type));
        if (!cortoType) {
            corto_seterr("type '%s' not found for anonymous object", json_value_get_string(type));
            goto error;
        }

        corto_object cortoObj = *(corto_object*)p;
        if (!cortoObj || (corto_typeof(cortoObj) != cortoType)) {
            cortoObj = corto_create(cortoType);
            corto_setref(p, cortoObj);
            corto_release(cortoObj);
        }
        corto_release(cortoType);

        JSON_Value* value = json_object_get_value(obj, "value");
        if (json_deserType(cortoObj, cortoType, value)) {
            goto error;
        }
        break;
    }
    case JSONNull:
        corto_setref(p, NULL);
        break;
    default:
        corto_seterr("expected string, null or object (reference), got %s", json_valueTypeToString(v));
        break;
    }

    return 0;
error:
    return -1;
}
Пример #12
0
/* Testing correctness of parsed values */
void test_suite_2(JSON_Value *root_value) {
    JSON_Object *root_object;
    JSON_Array *array;
    size_t i;
    TEST(root_value);
    TEST(json_value_get_type(root_value) == JSONObject);
    root_object = json_value_get_object(root_value);
    TEST(STREQ(json_object_get_string(root_object, "string"), "lorem ipsum"));
    TEST(STREQ(json_object_get_string(root_object, "utf string"), "lorem ipsum"));
    TEST(STREQ(json_object_get_string(root_object, "utf-8 string"), "あいうえお"));
    TEST(STREQ(json_object_get_string(root_object, "surrogate string"), "lorem𝄞ipsum𝍧lorem"));
    TEST(json_object_get_number(root_object, "positive one") == 1.0);
    TEST(json_object_get_number(root_object, "negative one") == -1.0);
    TEST(json_object_get_number(root_object, "hard to parse number") == -0.000314);
    TEST(json_object_get_boolean(root_object, "boolean true") == 1);
    TEST(json_object_get_boolean(root_object, "boolean false") == 0);
    TEST(json_value_get_type(json_object_get_value(root_object, "null")) == JSONNull);
    
    array = json_object_get_array(root_object, "string array");
    if (array != NULL && json_array_get_count(array) > 1) {
        TEST(STREQ(json_array_get_string(array, 0), "lorem"));
        TEST(STREQ(json_array_get_string(array, 1), "ipsum"));
    } else {
        tests_failed++;
    }
    
    array = json_object_get_array(root_object, "x^2 array");
    if (array != NULL) {
        for (i = 0; i < json_array_get_count(array); i++) {
            TEST(json_array_get_number(array, i) == (i * i));
        }
    } else {
        tests_failed++;
    }
    
    TEST(json_object_get_array(root_object, "non existent array") == NULL);
    TEST(STREQ(json_object_dotget_string(root_object, "object.nested string"), "str"));
    TEST(json_object_dotget_boolean(root_object, "object.nested true") == 1);
    TEST(json_object_dotget_boolean(root_object, "object.nested false") == 0);
    TEST(json_object_dotget_value(root_object, "object.nested null") != NULL);
    TEST(json_object_dotget_number(root_object, "object.nested number") == 123);
    
    TEST(json_object_dotget_value(root_object, "should.be.null") == NULL);
    TEST(json_object_dotget_value(root_object, "should.be.null.") == NULL);
    TEST(json_object_dotget_value(root_object, ".") == NULL);
    TEST(json_object_dotget_value(root_object, "") == NULL);
    
    array = json_object_dotget_array(root_object, "object.nested array");
    if (array != NULL && json_array_get_count(array) > 1) {
        TEST(STREQ(json_array_get_string(array, 0), "lorem"));
        TEST(STREQ(json_array_get_string(array, 1), "ipsum"));
    } else {
        tests_failed++;
    }
    TEST(json_object_dotget_boolean(root_object, "nested true"));
    
    TEST(STREQ(json_object_get_string(root_object, "/**/"), "comment"));
    TEST(STREQ(json_object_get_string(root_object, "//"), "comment"));
}
Пример #13
0
/* Testing correctness of parsed values */
void test_suite_2(void) {
    JSON_Value *root_value;
    JSON_Object *object;
    JSON_Array *array;
    int i;
    const char *filename = "tests/test_2.txt";
    printf("Testing %s:\n", filename);
    root_value = json_parse_file(filename);
    TEST(root_value);
    TEST(json_value_get_type(root_value) == JSONObject);
    object = json_value_get_object(root_value);
    TEST(STREQ(json_object_get_string(object, "string"), "lorem ipsum"));
    TEST(STREQ(json_object_get_string(object, "utf string"), "lorem ipsum"));
    TEST(json_object_get_number(object, "positive one") == 1.0);
    TEST(json_object_get_number(object, "negative one") == -1.0);
    TEST(json_object_get_number(object, "hard to parse number") == -0.000314);
    TEST(json_object_get_boolean(object, "boolean true") == 1);
    TEST(json_object_get_boolean(object, "boolean false") == 0);
    TEST(json_value_get_type(json_object_get_value(object, "null")) == JSONNull);
    
    array = json_object_get_array(object, "string array");
    if (array != NULL && json_array_get_count(array) > 1) {
        TEST(STREQ(json_array_get_string(array, 0), "lorem"));
        TEST(STREQ(json_array_get_string(array, 1), "ipsum"));
    } else {
        tests_failed++;
    }
    
    array = json_object_get_array(object, "x^2 array");
    if (array != NULL) {
        for (i = 0; i < json_array_get_count(array); i++) {
            TEST(json_array_get_number(array, i) == (i * i));
        }
    } else {
        tests_failed++;
    }
    
    TEST(json_object_get_array(object, "non existent array") == NULL);
    TEST(STREQ(json_object_dotget_string(object, "object.nested string"), "str"));
    TEST(json_object_dotget_boolean(object, "object.nested true") == 1);
    TEST(json_object_dotget_boolean(object, "object.nested false") == 0);
    TEST(json_object_dotget_value(object, "object.nested null") != NULL);
    TEST(json_object_dotget_number(object, "object.nested number") == 123);
    
    TEST(json_object_dotget_value(object, "should.be.null") == NULL);
    TEST(json_object_dotget_value(object, "should.be.null.") == NULL);
    TEST(json_object_dotget_value(object, ".") == NULL);
    TEST(json_object_dotget_value(object, "") == NULL);
    
    array = json_object_dotget_array(object, "object.nested array");
    if (array != NULL && json_array_get_count(array) > 1) {
        TEST(STREQ(json_array_get_string(array, 0), "lorem"));
        TEST(STREQ(json_array_get_string(array, 1), "ipsum"));
    } else {
        tests_failed++;
    }
    TEST(json_object_dotget_boolean(object, "nested true"));    
    json_value_free(root_value);
}
Пример #14
0
static EJSBool
json_value_to_ejsval(JSON_Value *v, ejsval *rv)
{
    switch (json_value_get_type (v)) {
    case JSONNull:
        *rv = _ejs_null;
        return EJS_TRUE;

    case JSONString:
        *rv = _ejs_string_new_utf8 (json_value_get_string(v));
        return EJS_TRUE;

    case JSONNumber:
        *rv = NUMBER_TO_EJSVAL(json_value_get_number(v));
        return EJS_TRUE;

    case JSONObject: {
        JSON_Object *obj = json_value_get_object (v);
        *rv = _ejs_object_create (_ejs_null);

        int count = json_object_get_count (obj);
        for (int i = 0; i < count; i ++) {
            const char *propkey = json_object_get_name (obj, i);
            ejsval propval;
            if (!json_value_to_ejsval (json_object_get_value (obj, propkey), &propval))
                return EJS_FALSE;
            _ejs_object_setprop_utf8 (*rv, propkey, propval);
        }

        return EJS_TRUE;
    }
        
    case JSONArray: {
        JSON_Array *arr = json_value_get_array (v);
        int count = json_array_get_count (arr);

        *rv = _ejs_array_new (count, EJS_FALSE);

        for (int i = 0; i < count; i ++) {
            ejsval propkey = _ejs_number_new (i);
            ejsval propval;
            if (!json_value_to_ejsval (json_array_get_value (arr, i), &propval))
                return EJS_FALSE;
            _ejs_object_setprop (*rv, propkey, propval);
        }

        return EJS_TRUE;
    }
    case JSONBoolean:
        *rv = BOOLEAN_TO_EJSVAL(json_value_get_boolean(v));
        return EJS_TRUE;


    case JSONError:
        EJS_NOT_IMPLEMENTED();
        return EJS_FALSE;
    }
}
Пример #15
0
static
uintptr_t
do_load_ppp_module(const char *fname)
{
    tried_files = g_list_prepend(tried_files, g_strdup(fname));

    module_dl_handler = dlopen(fname, RTLD_LAZY);
    if (!module_dl_handler) {
        trace_info_f("%s, can't open %s\n", __func__, fname);
        return 1;
    }

    int32_t (*ppp_initialize_module)(PP_Module module_id, PPB_GetInterface get_browser_interface);
    ppp_initialize_module = dlsym(module_dl_handler, "PPP_InitializeModule");
    ppp_get_interface = dlsym(module_dl_handler, "PPP_GetInterface");

    if (!ppp_initialize_module || !ppp_get_interface) {
        trace_error("%s, one of required PPP_* is missing\n", __func__);
        if (module_dl_handler)
            dlclose(module_dl_handler);
        module_dl_handler = NULL;
        return 1;
    }

    module_file_name = g_strdup(fname);

    if (!fpp_config_plugin_has_manifest()) {
        use_fallback_version_strings();
        return 0;
    }

    // try to read manifest.json file (only for those who can have it)
    char *manifest_dir = strdup(fname);
    gchar *manifest_path = g_strdup_printf("%s/manifest.json", dirname(manifest_dir));
    free(manifest_dir);

    JSON_Value *root_val = json_parse_file(manifest_path);
    g_free(manifest_path);
    if (!root_val) {
        use_fallback_version_strings();
        return 0;
    }

    JSON_Object *root_obj = json_value_get_object(root_val);
    const char *version = json_object_get_string(root_obj, "version");
    if (version) {
        int v1 = 0, v2 = 0, v3 = 0, v4 = 0;
        module_version = g_strdup(version);
        (void)sscanf(module_version, "%9d.%9d.%9d.%9d", &v1, &v2, &v3, &v4);
        module_descr = g_strdup_printf("%s %d.%d r%d", fpp_config_get_plugin_name(), v1, v2, v3);
    } else {
        use_fallback_version_strings();
    }

    json_value_free(root_val);
    return 0;
}
Пример #16
0
MODULE_LOADER_RESULT ModuleLoader_ParseBaseConfigurationFromJson(
    MODULE_LOADER_BASE_CONFIGURATION* configuration,
    const JSON_Value* json
)
{
    // The JSON is expected to be an object that has a string
    // property called "binding.path"
    MODULE_LOADER_RESULT result;

    JSON_Value_Type value_type = json_value_get_type(json);
    if (
        configuration == NULL ||
        json == NULL ||
        value_type != JSONObject
       )
    {
        LogError(
            "Invalid input arguments. "
            "configuration = %p, json = %p, json value type = %d",
            configuration,
            json,
            (json != NULL) ? value_type : 0
        );

        /*
        Codes_SRS_MODULE_LOADER_13_049: [ ModuleLoader_ParseBaseConfigurationFromJson shall return MODULE_LOADER_ERROR if configuration is NULL. ]
        Codes_SRS_MODULE_LOADER_13_050 : [ModuleLoader_ParseBaseConfigurationFromJson shall return MODULE_LOADER_ERROR if json is NULL.]
        Codes_SRS_MODULE_LOADER_13_051 : [ModuleLoader_ParseBaseConfigurationFromJson shall return MODULE_LOADER_ERROR if json is not a JSON object.]
        */
        result = MODULE_LOADER_ERROR;
    }
    else
    {
        JSON_Object* config = json_value_get_object(json);
        if (config == NULL)
        {
            LogError("json_value_get_object failed");

            /*Codes_SRS_MODULE_LOADER_13_052: [ ModuleLoader_ParseBaseConfigurationFromJson shall return MODULE_LOADER_ERROR if an underlying platform call fails. ]*/
            result = MODULE_LOADER_ERROR;
        }
        else
        {
            // It is acceptable to have binding.path be NULL.

            /*Codes_SRS_MODULE_LOADER_13_053: [ ModuleLoader_ParseBaseConfigurationFromJson shall read the value of the string attribute binding.path from the JSON object and assign to configuration->binding_path. ]*/
            const char* binding_path = json_object_get_string(config, "binding.path");
            configuration->binding_path = STRING_construct(binding_path);

            /*Codes_SRS_MODULE_LOADER_13_054: [ ModuleLoader_ParseBaseConfigurationFromJson shall return MODULE_LOADER_SUCCESS if the parsing is successful. ]*/
            result = MODULE_LOADER_SUCCESS;
        }
    }

    return result;
}
Пример #17
0
// retrieves the balancer url with the lowest load, if there are several balancers with the same load one of these is randomly selected
static char *GetBalancerUrl(void)
{
    char *bestUrl = NULL;

    char *balance = GetUrl(URL_BALANCE);
    if (balance != NULL)
    {
        JSON_Value *rootJson = json_parse_string(balance);
        free(balance);
        balance = NULL;

        if (rootJson != NULL && rootJson->type == JSONObject)
        {
            JSON_Object *serversJson = json_value_get_object(rootJson);

            if (serversJson != NULL)
            {
                size_t serverCount = json_object_get_count(serversJson);
                int bestLoad = 0;

                for (int i = 0; i < serverCount; i++)
                {
                    const char *url = json_object_get_name(serversJson, i);

                    JSON_Value *value = json_object_get_value(serversJson, url);

                    if (value != NULL && value->type == JSONNumber)
                    {
                        int load = (int) json_object_get_number(serversJson, url);

                        if (load != 0 && (bestUrl == NULL || load <= bestLoad/* || (load == bestLoad && rand() % 2)*/))
                        {
                            if (bestUrl != NULL)
                            {
                                free(bestUrl);
                                bestUrl = NULL;
                            }

                            bestUrl = (char*) malloc(strlen(url) + 1);
                            if (bestUrl != NULL)
                                strcpy(bestUrl, url);

                            bestLoad = load;
                        }
                    }
                }
            }

            json_value_free(rootJson);
            rootJson = NULL;
        }
    }

    return bestUrl;
}
Пример #18
0
void do_network(JSON_Value* json, char* msg) {
    if(json_value_get_type(json) == JSONObject) {
        JSON_Object* obj = json_value_get_object(json);
        const char* msgtype = json_object_dotget_string(obj, "type");

        if(strcmp(msgtype, msg_type_sensor) == 0 || strcmp(msgtype, msg_type_actor) == 0) {
            const char* command = json_object_dotget_string(obj, "msg.command");
            if(command != 0) execute_command(command);            
        }
    }
}
Пример #19
0
void test_suite_precincts(JSON_Value *root_value) {
    JSON_Object *root_object;
    JSON_Array *array;

    TEST(root_value);
    TEST(json_value_get_type(root_value) == JSONObject);
    root_object = json_value_get_object(root_value);

    unsigned int number_of_precincts = (int) json_object_get_number(root_object, "number_of_precincts");
    printf("number_of_precincts: %d\n", number_of_precincts);

    TEST(number_of_precincts == 60);

    array = json_object_get_array(root_object, "precincts");
    if (array != NULL && json_array_get_count(array) == number_of_precincts) {
        JSON_Object *precinct_object;
        JSON_Array *layers_array;
        int id, coord_x, coord_y;
        unsigned int j;
        
        for(j = 0; j < number_of_precincts; j++) {
            
            precinct_object = json_array_get_object(array, j);
            id = (int) json_object_get_number(precinct_object, "id");
            coord_x = (int) json_object_get_number(precinct_object, "coord_x");
            coord_y = (int) json_object_get_number(precinct_object, "coord_y");

            printf("id: %d \t coord_x: %d \t coord_y: %d\n", id, coord_x, coord_y);

            layers_array = json_object_get_array(precinct_object, "layers");
            if (layers_array != NULL && json_array_get_count(layers_array) >= 1) {
                JSON_Object *layer_object;
                int ql, bytes, psnr;
                unsigned k;

                for(k = 0; k < json_array_get_count(layers_array); k++) {

                    layer_object = json_array_get_object(layers_array, k);
                    ql = (int) json_object_get_number(layer_object, "ql");
                    bytes = (int) json_object_get_number(layer_object, "bytes");
                    psnr = (int) json_object_get_number(layer_object, "psnr");

                    printf("\tql: %d \t bytes: %d \t psnr: %d\n", ql, bytes, psnr);
                }
                
            } else {

            }

        }
    } else {
        tests_failed++;
    }
}
Пример #20
0
char *json(UDF_INIT *initid, UDF_ARGS *args,
      char *result, unsigned long *length,
      char *is_null, char *error)
{
    JSON_Value *jv = json_parse_string(args->args[0]);
    JSON_Value_Type json_type = json_value_get_type(jv);
    if(json_type==JSONError){
        *length=0;
        *is_null=1;
        *error=1;
        json_value_free(jv);
        return NULL;
    }else{
        if(json_type == JSONObject){
            JSON_Object *o = json_value_get_object(jv);
            JSON_Value *value = json_object_dotget_value(o,args->args[1]);
            if(value==NULL){
                *length = 0;
                *is_null = 0;
                return NULL;
            }else{
                *is_null = 0;
                switch(json_value_get_type(value)){
                    case JSONArray:
                        *is_null = 1;
                        break;
                    case JSONError:
                    case JSONObject:
                        *is_null = 1;
                        *error = 1;
                        break;
                    case JSONString:
                        strcpy(initid->ptr,((char*)json_value_get_string(value)));
                        break;
                    case JSONNumber:
                        sprintf(initid->ptr,"%f",json_value_get_number(value));
                        break;
                    case JSONNull:
                        *is_null = 1;
                        break;
                    case JSONBoolean:
                        sprintf(initid->ptr,"%d",json_value_get_boolean(value));
                        break;
                }
                *length = initid->ptr == NULL ? 0 : strlen(initid->ptr); 
            }
        }else{
            *is_null = 1;
        }
        json_value_free(jv);
        return initid->ptr; 
    }
}
Пример #21
0
void test_suite_7(void) {
    JSON_Value *val_from_file = json_parse_file("tests/test_5.txt");
    JSON_Value *schema = json_value_init_object();
    JSON_Object *schema_obj = json_value_get_object(schema);
    json_object_set_string(schema_obj, "first", "");
    json_object_set_string(schema_obj, "last", "");
    json_object_set_number(schema_obj, "age", 0);
    json_object_set_null(schema_obj, "favorites");
    TEST(json_validate(schema, val_from_file) == JSONSuccess);
    json_object_set_string(schema_obj, "age", "");
    TEST(json_validate(schema, val_from_file) == JSONFailure);
}
Пример #22
0
static JSON_Value *address_to_json(Address *addr) {
  if (!addr)
    return NULL;

  JSON_Value *address_value = json_value_init_object();
  JSON_Object *address_object = json_value_get_object(address_value);

  json_object_set_string(address_object, "name",    addr->name);
  json_object_set_string(address_object, "address", addr->address);

  return address_value;
}
Пример #23
0
guint jobdesc_m3u8streaming_version (gchar *job)
{
        JSON_Value *val;
        JSON_Object *obj;
        guint version;

        val = json_parse_string_with_comments (job);
        obj = json_value_get_object (val);
        version = json_object_dotget_number (obj, "m3u8streaming.version");
        json_value_free (val);

        return version;
}
Пример #24
0
guint jobdesc_m3u8streaming_window_size (gchar *job)
{
        JSON_Value *val;
        JSON_Object *obj;
        guint window_size;

        val = json_parse_string_with_comments (job);
        obj = json_value_get_object (val);
        window_size = json_object_dotget_number (obj, "m3u8streaming.window-size");
        json_value_free (val);

        return window_size;
}
Пример #25
0
GstClockTime jobdesc_m3u8streaming_segment_duration (gchar *job)
{
        JSON_Value *val;
        JSON_Object *obj;
        GstClockTime segment_duration;

        val = json_parse_string_with_comments (job);
        obj = json_value_get_object (val);
        segment_duration = GST_SECOND * json_object_dotget_number (obj, "m3u8streaming.segment-duration");
        json_value_free (val);

        return segment_duration;
}
Пример #26
0
static JSON_Value *message_body_to_json(MessageBody *mbody) {
  if (!mbody)
    return NULL;

  JSON_Value *body_value = json_value_init_object();
  JSON_Object *body_object = json_value_get_object(body_value);

  json_object_set_string(body_object, "type",    mbody->content_type);
  json_object_set_string(body_object, "content", mbody->content->str);
  json_object_set_number(body_object, "size",    mbody->size);

  return body_value;
}
Пример #27
0
void serialization_example(void) {
    JSON_Value *root_value = json_value_init_object();
    JSON_Object *root_object = json_value_get_object(root_value);
    char *serialized_string = NULL;
    json_object_set_string(root_object, "name", "John Smith");
    json_object_set_number(root_object, "age", 25);
    json_object_dotset_string(root_object, "address.city", "Cupertino");
    json_object_dotset_value(root_object, "contact.emails",
                             json_parse_string("[\"[email protected]\", \"[email protected]\"]"));
    serialized_string = json_serialize_to_string(root_value);
    puts(serialized_string);
    json_free_serialized_string(serialized_string);
    json_value_free(root_value);
}
Пример #28
0
gchar * jobdesc_m3u8streaming_push_server_uri (gchar *job)
{
        JSON_Value *val;
        JSON_Object *obj;
        gchar *ret, *push_url;

        val = json_parse_string_with_comments (job);
        obj = json_value_get_object (val);
        push_url = (gchar *)json_object_dotget_string (obj, "m3u8streaming.push-server-uri");
        ret = g_strdup (push_url);
        json_value_free (val);

        return ret;
}
Пример #29
0
gchar * jobdesc_get_name (gchar *job)
{
        JSON_Value *val;
        JSON_Object *obj;
        gchar *name, *ret;

        val = json_parse_string_with_comments (job);
        obj = json_value_get_object (val);
        name = (gchar *)json_object_get_string (obj, "name");
        ret = g_strdup (name);
        json_value_free (val);

        return ret;
}
Пример #30
0
corto_int16 json_deserialize(corto_value *v, corto_string s)
{
    corto_assert(v != NULL, "NULL passed to json_deserialize");

    char *json = s;
    if ((json[0] != '{') && (json[1] != '[') && (json[0] != '[')) {
        corto_asprintf(&json, "{\"value\": %s}", json);
    }

    JSON_Value *jsonValue = json_parse_string(json);
    if (!jsonValue) {
        corto_seterr("invalid JSON '%s'", json);
        goto error;
    }

    corto_type type = corto_value_getType(v);

    if (type->kind == CORTO_PRIMITIVE) {
        JSON_Object* jsonObj = json_value_get_object(jsonValue);
        if (!jsonObj) {
            corto_seterr("json: invalid JSON for primitive value '%s'", json);
            goto error;
        }

        JSON_Value *jsonValue = json_object_get_value(jsonObj, "value");
        if (!jsonValue) {
            corto_seterr("json: missing 'value' field for primitive value '%s'", json);
            goto error;
        }
    }

    if (json_deserialize_from_JSON_Value(v, jsonValue)) {
        goto error;
    }

    if (json != s) {
        corto_dealloc(json);
    }

    return 0;
error:
    if (json != s) {
        corto_dealloc(json);
    }

    if (jsonValue) {
        json_value_free(jsonValue);
    }
    return -1;
}