Exemple #1
0
static corto_int16 json_deserCollection(void* p, corto_type t, JSON_Value *v)
{
    corto_assert(t->kind == CORTO_COLLECTION, "not deserializing composite");
    corto_type elementType = corto_collection(t)->elementType;

    /* Deserialize elements */
    JSON_Array* a = json_value_get_array(v);
    if (!a) {
        corto_seterr("invalid array");
        goto error;
    }

    size_t count = json_array_get_count(a);
    size_t i;

    for (i = 0; i < count; i++) {
        void *elementPtr = json_deser_allocElem(p, corto_collection(t), i);
        JSON_Value *elem = json_array_get_value(a, i);
        if (json_deserType(elementPtr, elementType, elem)) {
            goto error;
        }
    }

    return 0;
error:
    return -1;
}
void parseJSON() {
    JSON_Value *root_value = json_parse_string(getData);
    
    if (root_value != NULL) {
        JSON_Value_Type type = root_value->type;
        
        if (type == JSONArray) {
            JSON_Array *stations = json_value_get_array(root_value);
            
            char lookupCallsign[32];
            sprintf(lookupCallsign, "%s%s", icaoCode, "_ATIS");
            
            for (int i = 0; i < json_array_get_count(stations); i++) {
                const JSON_Object *station = json_array_get_object(stations, i);
                const char *callsign = json_object_dotget_string(station, "callsign");
                
                if (strcmp(callsign, lookupCallsign) == 0) {
                    message = json_object_dotget_string(station, "atis");
                    XPLMDebugString(message);
                    break;
                }
            }
        }
        
        json_value_free(root_value);
    }
    
    newData = 0;
}
PROVISIONING_QUERY_RESPONSE* queryResponse_deserializeFromJson(const char* json_string, PROVISIONING_QUERY_TYPE type)
{
     PROVISIONING_QUERY_RESPONSE* new_result = NULL;
     JSON_Value* root_value = NULL;
     JSON_Array* root_array = NULL;

     if (json_string == NULL)
     {
         LogError("Cannot deserialize NULL");
     }
     else if (type == QUERY_TYPE_INVALID)
     {
         LogError("Invalid query type");
     }
     else if ((root_value = json_parse_string(json_string)) == NULL)
     {
         LogError("Parsing JSON string failed");
     }
     else if ((root_array = json_value_get_array(root_value)) == NULL)
     {
         LogError("Creating JSON array failed");
         json_value_free(root_value);
     }
     else
     {
         if ((new_result = queryResponse_fromJson(root_array, type)) == NULL)
         {
             LogError("Creating new Query Response failed");
         }
         json_value_free(root_value); //implicitly frees root_array
         root_value = NULL;
     }

     return new_result;
}
static void web_SockJsServer_message(web_SockJsServer _this, struct mg_connection *conn) {
    web_SockJsServer_Connection c = web_SockJsServer_Connection(conn->connection_param);
    if (_this->onMessage._parent.procedure) {
        if (conn->content_len) {
            char *msg = cx_malloc(conn->content_len + 1);
            memcpy(msg, conn->content, conn->content_len);
            msg[conn->content_len] = '\0';

            /* Parse & unpack message */
            JSON_Value *root = json_parse_string(msg);
            if (!root) {
                goto error;
            }

            if (json_value_get_type(root) == JSONArray) {
                JSON_Array *messages = json_value_get_array(root);
                cx_uint32 i;
                for (i = 0; i < json_array_get_count(messages); i++) {
                    const char *message = json_array_get_string(messages, i);
                    cx_call(_this->onMessage._parent.procedure, NULL, _this->onMessage._parent.instance, c, message);
                }
            }

            json_value_free(root);
        }
    }
error:;
}
Exemple #5
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;
    }
}
Exemple #6
0
char* pack_message(int fields, char* contents[]) {
    if (fields == 0) return NULL;

    JSON_Value *values = json_value_init_array();
    JSON_Array *array = json_value_get_array(values);
    int i;
    for (i = 0; i < fields; i++) {
        char* c = contents[i];
        json_array_append_string(array, contents[i]);
    }

    return json_serialize_to_string(values);
}
static void GenStat(Stat* s, JSON_Value* v) {
    switch (json_value_get_type(v)) {
    case JSONObject:
        {
            JSON_Object* o = json_value_get_object(v);
            size_t count = json_object_get_count(o);

            for (size_t i = 0; i < count; i++) {
                const char* name = json_object_get_name(o, i);
                GenStat(s, json_object_get_value(o, name));
                s->stringLength += strlen(name);
            }
            s->objectCount++;
            s->memberCount += count;
            s->stringCount += count;
        }
        break;

    case JSONArray:
        {
            JSON_Array* a = json_value_get_array(v);
            size_t count = json_array_get_count(a);
            for (int i = 0; i < count; i++)
                GenStat(s, json_array_get_value(a, i));
            s->arrayCount++;
            s->elementCount += count;
        }
        break;

    case JSONString:
        s->stringCount++;
        s->stringLength += strlen(json_value_get_string(v));
        break;

    case JSONNumber:
        s->numberCount++;
        break;

    case JSONBoolean:
        if (json_value_get_boolean(v))
            s->trueCount++;
        else
            s->falseCount++;
        break;

    case JSONNull:
        s->nullCount++;
        break;
    }
}
Exemple #8
0
static JSON_Value *addresses_list_to_json(AddressesList *addr_list) {
  if (!addr_list)
    return NULL;

  JSON_Value *addreses_value = json_value_init_array();
  JSON_Array *addresses_ary = json_value_get_array(addreses_value);

  guint i;
  for (i = 0; i < addr_list->len; i++) {
    Address *addr = addresses_list_get(addr_list, i);
    JSON_Value *jaddr = address_to_json(addr);
    json_array_append_value(addresses_ary, jaddr);
  }

  return addreses_value;
}
Exemple #9
0
static JSON_Value *references_to_json(GMimeReferences *references) {
  if (!references)
    return NULL;

  const char *msgid;
  const GMimeReferences *cur;
  JSON_Value *references_value = json_value_init_array();
  JSON_Array *references_array = json_value_get_array(references_value);

  for (cur = references; cur; cur = g_mime_references_get_next(cur)) {
    msgid = g_mime_references_get_message_id (cur);
    json_array_append_string(references_array, msgid);
  }

  return references_value;
}
Exemple #10
0
static mrb_value
json_value_to_mrb_value(mrb_state* mrb, JSON_Value* value) {
  ARENA_SAVE;
  switch (json_value_get_type(value)) {
  case JSONError:
  case JSONNull:
    return mrb_nil_value();
  case JSONString:
    return mrb_str_new_cstr(mrb, json_value_get_string(value));
  case JSONNumber:
    return mrb_float_value(json_value_get_number(value));
  case JSONObject:
    {
      mrb_value hash = mrb_hash_new(mrb);
      JSON_Object* object = json_value_get_object(value);
      size_t count = json_object_get_count(object);
      int n;
      for (n = 0; n < count; n++) {
        const char* name = json_object_get_name(object, n);
        mrb_hash_set(mrb, hash, mrb_str_new_cstr(mrb, name),
          json_value_to_mrb_value(mrb, json_object_get_value(object, name)));
      }
      return hash;
    }
  case JSONArray:
    {
      mrb_value ary;
      ary = mrb_ary_new(mrb);
      JSON_Array* array = json_value_get_array(value);
      size_t count = json_array_get_count(array);
      int n;
      for (n = 0; n < count; n++) {
        JSON_Value* elem = json_array_get_value(array, n);
        mrb_ary_push(mrb, ary, json_value_to_mrb_value(mrb, elem));
      }
      return ary;
    }
  case JSONBoolean:
    if (json_value_get_boolean(value)) {
      return mrb_true_value();
    }
    return mrb_false_value();
  default:
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }
  return mrb_nil_value();
}
Exemple #11
0
static JSON_Value *message_attachments_list_to_json(MessageAttachmentsList *matts) {
  if (!matts)
    return NULL;

  JSON_Value *attachments_value = json_value_init_array();
  JSON_Array *attachments_array = json_value_get_array(attachments_value);
  guint i;
  for (i = 0; i < matts->len; i++) {
    MessageAttachment *att = message_attachments_list_get(matts, i);
    JSON_Value  *attachment_value = json_value_init_object();
    JSON_Object *attachment_object = json_value_get_object(attachment_value);
    json_object_set_number(attachment_object, "partId",   att->part_id);
    json_object_set_string(attachment_object, "type",     att->content_type);
    json_object_set_string(attachment_object, "filename", att->filename);
    json_object_set_number(attachment_object, "size",     att->size);
    json_array_append_value(attachments_array, attachment_value);
  }
  return attachments_value;
}
Exemple #12
0
void print_commits_info(const char *username, const char *repo) {
    JSON_Value *root_value;
    JSON_Array *commits;
    JSON_Object *commit;
    size_t i;
    
    char curl_command[512];
    char cleanup_command[256];
    char output_filename[] = "commits.json";
    
    /* it ain't pretty, but it's not a libcurl tutorial */
    sprintf(curl_command, 
        "curl -s \"https://api.github.com/repos/%s/%s/commits\" > %s",
        username, repo, output_filename);
    sprintf(cleanup_command, "rm -f %s", output_filename);
    system(curl_command);
    
    /* parsing json and validating output */
    root_value = json_parse_file(output_filename);
    if (json_value_get_type(root_value) != JSONArray) {
        system(cleanup_command);
        return;
    }
    
    /* getting array from root value and printing commit info */
    commits = json_value_get_array(root_value);
    printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author");
    for (i = 0; i < json_array_get_count(commits); i++) {
        commit = json_array_get_object(commits, i);
        printf("%.10s %.10s %s\n",
               json_object_dotget_string(commit, "commit.author.date"),
               json_object_get_string(commit, "sha"),
               json_object_dotget_string(commit, "commit.author.name"));
    }
    
    /* cleanup code */
    json_value_free(root_value);
    system(cleanup_command);
}
Exemple #13
0
bud_error_t bud_sni_from_json(bud_config_t* config,
                              struct json_value_t* json,
                              bud_context_t* ctx) {
  JSON_Object* obj;
  JSON_Value* val;
  const char* cert_str;
  const char* key_str;
  const char* pass_str;
  JSON_Array* cert_strs;
  JSON_Array* key_strs;
  JSON_Array* pass_strs;
  bud_error_t err;

  cert_str = NULL;
  key_str = NULL;
  pass_str = NULL;
  cert_strs = NULL;
  key_strs = NULL;
  pass_strs = NULL;

  obj = json_value_get_object(json);
  val = json_object_get_value(obj, "cert");
  if (json_value_get_type(val) == JSONString)
    cert_str = json_value_get_string(val);
  else
    cert_strs = json_value_get_array(val);
  val = json_object_get_value(obj, "key");
  if (json_value_get_type(val) == JSONString)
    key_str = json_value_get_string(val);
  else
    key_strs = json_value_get_array(val);
  val = json_object_get_value(obj, "passphrase");
  if (json_value_get_type(val) == JSONString)
    pass_str = json_value_get_string(val);
  else
    pass_strs = json_value_get_array(val);

  if (obj == NULL ||
      !((cert_str != NULL && key_str != NULL) ||
        (cert_strs != NULL && key_strs != NULL))) {
    err = bud_error_str(kBudErrJSONParse, "<SNI Response>");
    goto fatal;
  }

  /* Load NPN from response */
  memset(ctx, 0, sizeof(*ctx));
  ctx->cert_file = cert_str;
  ctx->key_file = key_str;
  ctx->key_pass = pass_str;
  ctx->cert_files = cert_strs;
  ctx->key_files = key_strs;
  ctx->key_passes = pass_strs;
  ctx->ciphers = json_object_get_string(obj, "ciphers");
  ctx->ecdh = json_object_get_string(obj, "ecdh");
  ctx->ticket_key = json_object_get_string(obj, "ticket_key");
  ctx->npn = json_object_get_array(obj, "npn");
  ctx->ca_array = json_object_get_array(obj, "ca");
  val = json_object_get_value(obj, "request_cert");
  if (val != NULL)
    ctx->request_cert = json_value_get_boolean(val);
  err = bud_config_load_backend_list(config, obj, &ctx->backend);
  if (!bud_is_ok(err))
    goto fatal;

  err = bud_context_init(config, ctx);

  /* Make sure that deallocated values won't be used */
  ctx->cert_file = NULL;
  ctx->key_file = NULL;
  ctx->key_pass = NULL;
  ctx->cert_files = NULL;
  ctx->key_files = NULL;
  ctx->key_passes = NULL;
  if (!bud_is_ok(err))
    goto fatal;

  return bud_ok();

fatal:
  if (!bud_is_ok(err)) {
    SSL_CTX_free(ctx->ctx);
    ctx->ctx = NULL;
  }
  free(ctx->backend.list);
  ctx->backend.list = NULL;
  return err;
}
Exemple #14
0
// updates the planes map, planes not seen for a defined intervall are removed from the map and only planes within a defined distance from the given latitude and longited
static void UpdatePlanes(char *balancerUrl, char *zoneName, double latitude, double longitude)
{
    time_t currentTime = time(NULL);

    if (currentTime != ((time_t) -1))
    {
        pthread_mutex_lock(&planesMutex);
        for (std::map<std::string, Plane*>::iterator p = planes.begin(); p != planes.end(); ++p)
        {
            Plane *plane = p->second;
            if (currentTime - plane->lastSeen > PLANE_TIMEOUT)
            {
                //printf("Removing: %s - CurrentTime = %d - LastSeen = %d\n", p->first.c_str(), (int) currentTime, (int) plane->lastSeen);
                free(plane);
                plane = NULL;
                planes.erase(p->first);
            }
        }
        pthread_mutex_unlock(&planesMutex);

        char url[strlen(balancerUrl) + strlen(URL_ZONE_INFIX) + strlen(zoneName) + strlen(URL_ZONE_SUFFIX)];
        sprintf(url, "%s%s%s%s", balancerUrl, URL_ZONE_INFIX, zoneName, URL_ZONE_SUFFIX);

        char *zone = GetUrl(url);
        if (zone != NULL)
        {
            int cmp = 1;
            if (lastZone != NULL)
                cmp = strcmp(zone, lastZone);

            if (cmp != 0)
            {
                JSON_Value *rootJson = json_parse_string(zone);
                lastZone = zone;

                if (rootJson != NULL)
                {
                    if (rootJson->type == JSONObject)
                    {
                        JSON_Object *aircraftJson = json_value_get_object(rootJson);
                        if (aircraftJson != NULL)
                        {
                            size_t aircraftCount = json_object_get_count(aircraftJson);
                            for (int i = 0; i < aircraftCount; i++)
                            {
                                const char *id = json_object_get_name(aircraftJson, i);
                                if (id != NULL)
                                {
                                    JSON_Value *value = json_object_get_value(aircraftJson, id);

                                    if (value != NULL && value->type == JSONArray)
                                    {
                                        JSON_Array *propertiesJson = json_value_get_array(value);

                                        if (propertiesJson != NULL)
                                        {
                                            const char *registration = NULL, *icaoId = NULL, *icaoType = NULL, *squawk = NULL;
                                            double latitudePlane = 0.0, longitudePlane = 0.0, altitude = 0.0;
                                            float heading = 0.0f;
                                            int speed = 0, verticalSpeed = 0;

                                            int propertyCount = json_array_get_count(propertiesJson);
                                            for (int k = 0; k < propertyCount; k++)
                                            {
                                                JSON_Value *valueJson = json_array_get_value(propertiesJson, k);

                                                if (valueJson != NULL)
                                                {
                                                    switch (k)
                                                    {
                                                    case ARRAY_INDEX_LATITUDE:
                                                        latitudePlane = json_value_get_number(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_LONGITUDE:
                                                        longitudePlane = json_value_get_number(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_ALTITUDE:
                                                        altitude = json_value_get_number(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_HEADING:
                                                        heading = (float) json_value_get_number(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_SPEED:
                                                        speed = (int) json_value_get_number(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_SQUAWK:
                                                        squawk = json_value_get_string(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_ICAO_TYPE:
                                                        icaoType = json_value_get_string(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_REGISTRATION:
                                                        registration = json_value_get_string(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_VERTICAL_SPEED:
                                                        verticalSpeed = (int) json_value_get_number(valueJson);
                                                        break;
                                                    case ARRAY_INDEX_ICAO_ID:
                                                        icaoId = json_value_get_string(valueJson);
                                                        break;
                                                    }
                                                }
                                            }

                                            if (latitudePlane != 0.0 && longitudePlane != 0.0 && GetDistance(latitude, longitude, latitudePlane, longitudePlane) <= MAX_DISTANCE)
                                            {
                                                Plane *plane = NULL;

                                                pthread_mutex_lock(&planesMutex);
                                                std::map<std::string, Plane*>::iterator p = planes.find(id);
                                                if (p != planes.end())
                                                    plane = p->second;
                                                else
                                                {
                                                    plane = (Plane*) malloc(sizeof(*plane));
                                                    if (plane != NULL)
                                                        planes[std::string(id)] = plane;
                                                }

                                                if (plane != NULL)
                                                {
                                                    if (registration == NULL || strlen(registration) == 0)
                                                        registration = "Unknown";
                                                    if (icaoId == NULL || strlen(icaoId) == 0)
                                                        icaoId = "Unknown";
                                                    if (icaoType == NULL || strlen(icaoType) == 0)
                                                        icaoType = "UKN";
                                                    if (squawk == NULL || strlen(squawk) == 0)
                                                        squawk = "0000";
                                                    strncpy(plane->registration, registration, sizeof(plane->registration) / sizeof(char));
                                                    strncpy(plane->icaoId, icaoId, sizeof(plane->icaoId) / sizeof(char));
                                                    strncpy(plane->icaoType, icaoType, sizeof(plane->icaoType) / sizeof(char));
                                                    strncpy(plane->squawk, squawk, sizeof(plane->squawk) / sizeof(char));
                                                    if (plane->latitude != latitudePlane)
                                                    {
                                                        plane->latitude = latitudePlane;
                                                        plane->interpolatedLatitude = 0.0;
                                                    }
                                                    if (plane->longitude != longitudePlane)
                                                    {
                                                        plane->longitude = longitudePlane;
                                                        plane->interpolatedLongitude = 0.0;
                                                    }
                                                    if (plane->altitude != altitude)
                                                    {
                                                        plane->altitude = altitude;
                                                        plane->interpolatedAltitude = -1000.0;
                                                    }
                                                    plane->pitch = 0.0f;
                                                    plane->roll = 0.0f;
                                                    plane->heading = heading;
                                                    plane->speed = speed;
                                                    plane->verticalSpeed = verticalSpeed;
                                                    plane->lastSeen = currentTime;
                                                }
                                                pthread_mutex_unlock(&planesMutex);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        json_value_free(rootJson);
                        rootJson = NULL;
                    }
                }
            }
        }
    }
}
MODULE_LOADER_RESULT ModuleLoader_InitializeFromJson(const JSON_Value* loaders)
{
    MODULE_LOADER_RESULT result;

    if (loaders == NULL)
    {
        LogError("loaders is NULL");

        /*Codes_SRS_MODULE_LOADER_13_062: [ ModuleLoader_InitializeFromJson shall return MODULE_LOADER_ERROR if loaders is NULL. ]*/
        result = MODULE_LOADER_ERROR;
    }
    else
    {
        if (json_value_get_type(loaders) != JSONArray)
        {
            LogError("loaders is not a JSON array");

            /*Codes_SRS_MODULE_LOADER_13_063: [ ModuleLoader_InitializeFromJson shall return MODULE_LOADER_ERROR if loaders is not a JSON array. ]*/
            result = MODULE_LOADER_ERROR;
        }
        else
        {
            JSON_Array* loaders_array = json_value_get_array(loaders);
            if (loaders_array == NULL)
            {
                LogError("json_value_get_array failed");

                /*Codes_SRS_MODULE_LOADER_13_064: [ ModuleLoader_InitializeFromJson shall return MODULE_LOADER_ERROR if an underlying platform call fails. ]*/
                result = MODULE_LOADER_ERROR;
            }
            else
            {
                size_t length = json_array_get_count(loaders_array);
                size_t i;
                for (i = 0; i < length; i++)
                {
                    JSON_Value* loader = json_array_get_value(loaders_array, i);
                    if (loader == NULL)
                    {
                        /*Codes_SRS_MODULE_LOADER_13_064: [ ModuleLoader_InitializeFromJson shall return MODULE_LOADER_ERROR if an underlying platform call fails. ]*/
                        LogError("json_array_get_value failed for loader %zu", i);
                        break;
                    }

                    if (add_loader_from_json(loader, i) != MODULE_LOADER_SUCCESS)
                    {
                        LogError("add_loader_from_json failed for loader %zu", i);
                        break;
                    }
                }

                /*Codes_SRS_MODULE_LOADER_13_073: [ ModuleLoader_InitializeFromJson shall return MODULE_LOADER_SUCCESS if the the JSON has been processed successfully. ]*/

                // if the loop terminated early then something went wrong
                result = (i < length) ? MODULE_LOADER_ERROR : MODULE_LOADER_SUCCESS;
            }
        }
    }

    return result;
}
Exemple #16
0
static gchar * set_network_interfaces (RequestData *request_data)
{
        gchar *interfaces, *result, *name, *path, *value;
        augeas *aug;
        JSON_Value *val;
        JSON_Array *array;
        JSON_Object *obj;
        gint if_count, i, ret;

        aug = aug_init (NULL, NULL, AUG_NONE | AUG_NO_ERR_CLOSE | AUG_NO_MODL_AUTOLOAD);
        aug_set (aug, "/augeas/load/Interfaces/lens", "Interfaces.lns");
        aug_set (aug, "/augeas/load/Interfaces/incl", "/etc/network/interfaces");
        aug_load (aug);
        interfaces = request_data->raw_request + request_data->header_size;
        val = json_parse_string (interfaces);
        if (val == NULL) {
                GST_ERROR ("parse json type interfaces failure");
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"invalid data\"\n}");
                aug_close (aug);
                return result;
        }
        array = json_value_get_array (val);
        if (array == NULL) {
                GST_ERROR ("get interfaces array failure");
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"not array type interfaces\"\n}");
                aug_close (aug);
                json_value_free (val);
                return result;
        }
        if_count = json_array_get_count (array);
        for (i = 0; i < if_count; i++) {
                obj = json_array_get_object (array, i);
                name = (gchar *)json_object_get_string (obj, "name");
                value = (gchar *)json_object_get_string (obj, "method");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/method", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "address");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/address", name);
                        ret = aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "netmask");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/netmask", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "network");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/network", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "broadcast");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/broadcast", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
                value = (gchar *)json_object_get_string (obj, "gateway");
                if (value != NULL) {
                        path = g_strdup_printf ("//files/etc/network/interfaces/iface[.='%s']/gateway", name);
                        aug_set (aug, path, value);
                        g_free (path);
                }
        }
        if (aug_save (aug) == -1) {
                aug_get (aug, "/augeas//error", (const gchar **)&value);
                GST_ERROR ("set /etc/network/interface failure: %s", value);
                result = g_strdup_printf ("{\n    \"result\": \"failure\",\n    \"reason\": \"%s\"\n}", value);

        } else {
                result = g_strdup ("{\n    \"result\": \"success\"\n}");
        }
        json_value_free (val);
        aug_close (aug);

        return result;
}
Exemple #17
0
static mrb_value
json_value_to_mrb_value(mrb_state* mrb, JSON_Value* value) {
  mrb_value ret;
  switch (json_value_get_type(value)) {
  case JSONError:
  case JSONNull:
    ret = mrb_nil_value();
    break;
  case JSONString:
    ret = mrb_str_new_cstr(mrb, json_value_get_string(value));
    break;
  case JSONNumber:
    {
      double d = json_value_get_number(value);
      if (floor(d) == d) {
        ret = mrb_fixnum_value(d);
      }
      else {
        ret = mrb_float_value(mrb, d);
      }
    }
    break;
  case JSONObject:
    {
      mrb_value hash = mrb_hash_new(mrb);
      JSON_Object* object = json_value_get_object(value);
      size_t count = json_object_get_count(object);
      size_t n;
      for (n = 0; n < count; n++) {
        int ai = mrb_gc_arena_save(mrb);
        const char* name = json_object_get_name(object, n);
        mrb_hash_set(mrb, hash, mrb_str_new_cstr(mrb, name),
          json_value_to_mrb_value(mrb, json_object_get_value(object, name)));
        mrb_gc_arena_restore(mrb, ai);
      }
      ret = hash;
    }
    break;
  case JSONArray:
    {
      mrb_value ary;
      JSON_Array* array;
      size_t n, count;
      ary = mrb_ary_new(mrb);
      array = json_value_get_array(value);
      count = json_array_get_count(array);
      for (n = 0; n < count; n++) {
        int ai = mrb_gc_arena_save(mrb);
        JSON_Value* elem = json_array_get_value(array, n);
        mrb_ary_push(mrb, ary, json_value_to_mrb_value(mrb, elem));
        mrb_gc_arena_restore(mrb, ai);
      }
      ret = ary;
    }
    break;
  case JSONBoolean:
    if (json_value_get_boolean(value))
      ret = mrb_true_value();
    else
      ret = mrb_false_value();
    break;
  default:
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }
  return ret;
}