/**
 * couchdb_struct_field_set_array_field:
 * @sf: A #CouchdbStructField object
 * @field: Name of the field
 * @calue: Value to set the field to
 *
 * Set the value of an array field in the given struct field.
 */
void
couchdb_struct_field_set_array_field (CouchdbStructField *sf, const char *field, CouchdbArrayField *value)
{
	g_return_if_fail (sf != NULL);
	g_return_if_fail (field != NULL);
	g_return_if_fail (value != NULL);

	json_object_set_array_member (sf->json_object, field, json_array_ref (couchdb_array_field_get_json_array (value)));
}
Esempio n. 2
0
/**
 * json_node_dup_array:
 * @node: a #JsonNode
 *
 * Retrieves the #JsonArray stored inside a #JsonNode and returns it
 * with its reference count increased by one.
 *
 * Return value: (transfer full): the #JsonArray with its reference
 *   count increased.
 */
JsonArray *
json_node_dup_array (JsonNode *node)
{
  g_return_val_if_fail (JSON_NODE_IS_VALID (node), NULL);
  g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);

  if (node->data.array)
    return json_array_ref (node->data.array);

  return NULL;
}
Esempio n. 3
0
JsonArray *
gs_snapd_find (const gchar *macaroon, gchar **discharges,
               gchar **values,
               GCancellable *cancellable, GError **error)
{
    g_autoptr(GString) path = NULL;
    g_autofree gchar *query = NULL;
    g_autofree gchar *escaped = NULL;
    guint status_code;
    g_autofree gchar *reason_phrase = NULL;
    g_autofree gchar *response_type = NULL;
    g_autofree gchar *response = NULL;
    g_autoptr(JsonParser) parser = NULL;
    JsonObject *root;
    JsonArray *result;

    path = g_string_new ("/v2/find?q=");
    query = g_strjoinv (" ", values);
    escaped = soup_uri_encode (query, NULL);
    g_string_append (path, escaped);
    if (!send_request ("GET", path->str, NULL,
                       macaroon, discharges,
                       &status_code, &reason_phrase,
                       &response_type, &response, NULL,
                       cancellable, error))
        return NULL;

    if (status_code != SOUP_STATUS_OK) {
        g_set_error (error,
                     GS_PLUGIN_ERROR,
                     GS_PLUGIN_ERROR_FAILED,
                     "snapd returned status code %u: %s",
                     status_code, reason_phrase);
        return NULL;
    }

    parser = parse_result (response, response_type, error);
    if (parser == NULL)
        return NULL;
    root = json_node_get_object (json_parser_get_root (parser));
    result = json_object_get_array_member (root, "result");
    if (result == NULL) {
        g_set_error (error,
                     GS_PLUGIN_ERROR,
                     GS_PLUGIN_ERROR_FAILED,
                     "snapd returned no result");
        return NULL;
    }

    return json_array_ref (result);
}
Esempio n. 4
0
/**
 * json_node_set_array:
 * @node: a #JsonNode initialized to %JSON_NODE_ARRAY
 * @array: a #JsonArray
 *
 * Sets @array inside @node and increases the #JsonArray reference count
 */
void
json_node_set_array (JsonNode  *node,
                     JsonArray *array)
{
  g_return_if_fail (node != NULL);
  g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);

  if (node->data.array)
    json_array_unref (node->data.array);

  if (array)
    node->data.array = json_array_ref (array);
  else
    node->data.array = NULL;
}
Esempio n. 5
0
JsonArray *
gs_snapd_list (const gchar *macaroon, gchar **discharges,
               GCancellable *cancellable, GError **error)
{
    guint status_code;
    g_autofree gchar *reason_phrase = NULL;
    g_autofree gchar *response_type = NULL;
    g_autofree gchar *response = NULL;
    g_autoptr(JsonParser) parser = NULL;
    JsonObject *root;
    JsonArray *result;

    if (!send_request ("GET", "/v2/snaps", NULL,
                       macaroon, discharges,
                       &status_code, &reason_phrase,
                       &response_type, &response, NULL,
                       cancellable, error))
        return NULL;

    if (status_code != SOUP_STATUS_OK) {
        g_set_error (error,
                     GS_PLUGIN_ERROR,
                     GS_PLUGIN_ERROR_FAILED,
                     "snapd returned status code %u: %s",
                     status_code, reason_phrase);
        return NULL;
    }

    parser = parse_result (response, response_type, error);
    if (parser == NULL)
        return NULL;
    root = json_node_get_object (json_parser_get_root (parser));
    result = json_object_get_array_member (root, "result");
    if (result == NULL) {
        g_set_error (error,
                     GS_PLUGIN_ERROR,
                     GS_PLUGIN_ERROR_FAILED,
                     "snapd returned no result");
        return NULL;
    }

    return json_array_ref (result);
}
Esempio n. 6
0
/**
 * json_builder_end_array:
 * @builder: a #JsonBuilder
 *
 * Closes the subarray inside the given @builder that was opened by the most
 * recent call to json_builder_begin_array().
 *
 * Cannot be called after json_builder_set_member_name().
 *
 * Return value: (transfer none): the #JsonBuilder, or %NULL if the call was inconsistent
 */
JsonBuilder *
json_builder_end_array (JsonBuilder *builder)
{
  JsonBuilderState *state;

  g_return_val_if_fail (JSON_IS_BUILDER (builder), NULL);
  g_return_val_if_fail (!g_queue_is_empty (builder->priv->stack), NULL);
  g_return_val_if_fail (json_builder_current_mode (builder) == JSON_BUILDER_MODE_ARRAY, NULL);

  state = g_queue_pop_head (builder->priv->stack);

  if (g_queue_is_empty (builder->priv->stack))
    {
      builder->priv->root = json_node_new (JSON_NODE_ARRAY);
      json_node_take_array (builder->priv->root, json_array_ref (state->data.array));
    }

  json_builder_state_free (state);

  return builder;
}
Esempio n. 7
0
/**
 * json_builder_begin_array:
 * @builder: a #JsonBuilder
 *
 * Opens a subarray inside the given @builder. When done adding members to
 * the subarray, json_builder_end_array() must be called.
 *
 * Can be called for first or only if the call is associated to an object member
 * or an array element.
 *
 * Return value: (transfer none): the #JsonBuilder, or %NULL if the call was inconsistent
 */
JsonBuilder *
json_builder_begin_array (JsonBuilder *builder)
{
  JsonArray *array;
  JsonBuilderState *state;
  JsonBuilderState *cur_state;

  g_return_val_if_fail (JSON_IS_BUILDER (builder), NULL);
  g_return_val_if_fail (builder->priv->root == NULL, NULL);
  g_return_val_if_fail (g_queue_is_empty (builder->priv->stack) || json_builder_is_valid_add_mode (builder), NULL);

  array = json_array_new ();
  cur_state = g_queue_peek_head (builder->priv->stack);
  if (cur_state)
    {
      switch (cur_state->mode)
        {
        case JSON_BUILDER_MODE_ARRAY:
          json_array_add_array_element (cur_state->data.array, json_array_ref (array));
          break;

        case JSON_BUILDER_MODE_MEMBER:
          json_object_set_array_member (cur_state->data.object, cur_state->member_name, json_array_ref (array));
          g_free (cur_state->member_name);
          cur_state->member_name = NULL;
          cur_state->mode = JSON_BUILDER_MODE_OBJECT;
          break;

        default:
          g_assert_not_reached ();
        }
    }

  state = g_slice_new (JsonBuilderState);
  state->data.array = array;
  state->mode = JSON_BUILDER_MODE_ARRAY;
  g_queue_push_head (builder->priv->stack, state);

  return builder;
}
Esempio n. 8
0
/* Parse JSON-RPC request */
static JsonNode *
melo_jsonrpc_parse_node (JsonNode *node)
{
  MeloJSONRPCInternalMethod *m;
  MeloJSONRPCCallback callback = NULL;
  gpointer user_data = NULL;
  JsonArray *s_params = NULL;
  JsonNode *result = NULL;
  JsonNode *error = NULL;
  JsonNode *params;
  JsonObject *obj;
  const char *version;
  const char *method;
  const char *id = NULL;
  gint64 nid = -1;

  /* Not an object */
  if (JSON_NODE_TYPE (node) != JSON_NODE_OBJECT)
    goto invalid;

  /* Get object from nide */
  obj = json_node_get_object (node);
  if (!obj)
    goto internal;

  /* Check if jsonrpc is present */
  if (!json_object_has_member (obj, "jsonrpc"))
    goto invalid;

  /* Check JSON-RPC version */
  version = json_object_get_string_member (obj, "jsonrpc");
  if (!version || strcmp (version, "2.0"))
    goto invalid;

  /* Check if method is present */
  if (!json_object_has_member (obj, "method"))
    goto invalid;

  /* Get method */
  method = json_object_get_string_member (obj, "method");
  if (!method)
    goto invalid;

  /* Get params */
  params = json_object_get_member (obj, "params");
  if (params) {
    JsonNodeType type;

    /* Check params type: only object or array allowed */
    type = json_node_get_node_type (params);
    if (type != JSON_NODE_ARRAY && type != JSON_NODE_OBJECT)
      goto invalid;
  }

  /* Get registered method */
  G_LOCK (melo_jsonrpc_mutex);
  if (melo_jsonrpc_methods) {
    m = g_hash_table_lookup (melo_jsonrpc_methods, method);
    if (m) {
      callback = m->callback;
      user_data = m->user_data;
      if (m->params)
        s_params = json_array_ref (m->params);
    }
  }
  G_UNLOCK (melo_jsonrpc_mutex);

  /* Check if id is present */
  if (!json_object_has_member (obj, "id")) {
    /* This is a notification: try to call callback */
    if (callback) {
      callback (method, s_params, params, &result, &error, user_data);
      if (s_params)
        json_array_unref (s_params);
      if (error)
        json_node_free (error);
      if (result)
        json_node_free (result);
    }
    return NULL;
  }

  /* Get id */
  nid = json_object_get_int_member (obj, "id");
  id = json_object_get_string_member (obj, "id");

  /* No callback provided */
  if (!callback)
    goto not_found;

  /* Call user callback */
  callback (method, s_params, params, &result, &error, user_data);
  if (s_params)
    json_array_unref (s_params);

  /* No error or result */
  if (!error && !result)
    goto not_found;

  /* Build response */
  return melo_jsonrpc_build_response_node (result, error, id, nid);

invalid:
  return melo_jsonrpc_build_error (NULL, -1,
                                        MELO_JSONRPC_ERROR_INVALID_REQUEST,
                                        "Invalid request");
not_found:
  return melo_jsonrpc_build_error (id, nid,
                                        MELO_JSONRPC_ERROR_METHOD_NOT_FOUND,
                                        "Method not found");
internal:
  return melo_jsonrpc_build_error (id, -1,
                                        MELO_JSONRPC_ERROR_INTERNAL_ERROR,
                                        "Internal error");
}
static gpointer _json_array_ref0 (gpointer self) {
	return self ? json_array_ref (self) : NULL;
}