Exemple #1
0
/**
 * melo_jsonrpc_get_array:
 * @schema_params: the schema to use for parameters conversion (see
 *    #MeloJSONRPCMethod for more details)
 * @params: the parameters to convert
 * @error: a pointer to a #JsonNode which is set with a valid JSON-RPC error if
 *    an error has occurred
 *
 * Convert the #JsonNode containing the parameters into a #JsonArray with all
 * items sorted as the provided schema.
 *
 * Returns: (transfer full): a new #JsonArray containing all parameters sorted
 * as provided schema, or %NULL if an error occurred. Use json_array_unref()
 * after usage.
 */
JsonArray *
melo_jsonrpc_get_array (JsonArray *schema_params, JsonNode *params,
                        JsonNode **error)
{
  JsonArray *array;
  guint count;

  /* Get array length */
  count = json_array_get_length (schema_params);

  /* Allocate new array */
  array = json_array_sized_new (count);

  /* Get array */
  if (!melo_jsonrpc_get_json_node (schema_params, params, NULL, array, error)) {
    json_array_unref (array);
    return NULL;
  }

  return array;
}
Exemple #2
0
JsonNode *
json_serialize_pspec (const GValue *real_value,
                      GParamSpec   *pspec)
{
  JsonNode *retval = NULL;
  JsonNodeType node_type;

  switch (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (real_value)))
    {
    /* JSON native types */
    case G_TYPE_INT64:
      retval = json_node_init_int (json_node_alloc (), g_value_get_int64 (real_value));
      break;

    case G_TYPE_BOOLEAN:
      retval = json_node_init_boolean (json_node_alloc (), g_value_get_boolean (real_value));
      break;

    case G_TYPE_DOUBLE:
      retval = json_node_init_double (json_node_alloc (), g_value_get_double (real_value));
      break;

    case G_TYPE_STRING:
      retval = json_node_init_string (json_node_alloc (), g_value_get_string (real_value));
      break;

    /* auto-promoted types */
    case G_TYPE_INT:
      retval = json_node_init_int (json_node_alloc (), g_value_get_int (real_value));
      break;

    case G_TYPE_UINT:
      retval = json_node_init_int (json_node_alloc (), g_value_get_uint (real_value));
      break;

    case G_TYPE_LONG:
      retval = json_node_init_int (json_node_alloc (), g_value_get_long (real_value));
      break;

    case G_TYPE_ULONG:
      retval = json_node_init_int (json_node_alloc (), g_value_get_ulong (real_value));
      break;

    case G_TYPE_UINT64:
      retval = json_node_init_int (json_node_alloc (), g_value_get_uint64 (real_value));
      break;

    case G_TYPE_FLOAT:
      retval = json_node_init_double (json_node_alloc (), g_value_get_float (real_value));
      break;

    case G_TYPE_CHAR:
      retval = json_node_alloc ();
      json_node_init_int (retval, g_value_get_schar (real_value));
      break;

    case G_TYPE_UCHAR:
      retval = json_node_init_int (json_node_alloc (), g_value_get_uchar (real_value));
      break;

    case G_TYPE_ENUM:
      retval = json_node_init_int (json_node_alloc (), g_value_get_enum (real_value));
      break;

    case G_TYPE_FLAGS:
      retval = json_node_init_int (json_node_alloc (), g_value_get_flags (real_value));
      break;

    /* complex types */
    case G_TYPE_BOXED:
      if (G_VALUE_HOLDS (real_value, G_TYPE_STRV))
        {
          gchar **strv = g_value_get_boxed (real_value);
          gint i, strv_len;
          JsonArray *array;

          strv_len = g_strv_length (strv);
          array = json_array_sized_new (strv_len);

          for (i = 0; i < strv_len; i++)
            {
              JsonNode *str = json_node_new (JSON_NODE_VALUE);

              json_node_set_string (str, strv[i]);
              json_array_add_element (array, str);
            }

          retval = json_node_init_array (json_node_alloc (), array);
          json_array_unref (array);
        }
      else if (json_boxed_can_serialize (G_VALUE_TYPE (real_value), &node_type))
        {
          gpointer boxed = g_value_get_boxed (real_value);

          retval = json_boxed_serialize (G_VALUE_TYPE (real_value), boxed);
        }
      else
        g_warning ("Boxed type '%s' is not handled by JSON-GLib",
                   g_type_name (G_VALUE_TYPE (real_value)));
      break;

    case G_TYPE_OBJECT:
      {
        GObject *object = g_value_get_object (real_value);

        retval = json_node_alloc ();

        if (object != NULL)
          {
            json_node_init (retval, JSON_NODE_OBJECT);
            json_node_take_object (retval, json_gobject_dump (object));
          }
        else
          json_node_init_null (retval);
      }
      break;

    case G_TYPE_NONE:
      retval = json_node_new (JSON_NODE_NULL);
      break;

    default:
      g_warning ("Unsupported type `%s'", g_type_name (G_VALUE_TYPE (real_value)));
      break;
    }

  return retval;
}
JsonNode *
json_serialize_pspec (const GValue *real_value,
                      GParamSpec   *pspec)
{
  JsonNode *retval = NULL;
  GValue value = { 0, };
  JsonNodeType node_type;

  switch (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (real_value)))
    {
    case G_TYPE_INT64:
    case G_TYPE_BOOLEAN:
    case G_TYPE_DOUBLE:
      /* JSON native types */
      retval = json_node_new (JSON_NODE_VALUE);
      g_value_init (&value, G_VALUE_TYPE (real_value));
      g_value_copy (real_value, &value);
      json_node_set_value (retval, &value);
      g_value_unset (&value);
      break;

    case G_TYPE_STRING:
      /* strings might be NULL, so we handle it differently */
      if (!g_value_get_string (real_value))
        retval = json_node_new (JSON_NODE_NULL);
      else
        {
          retval = json_node_new (JSON_NODE_VALUE);
          json_node_set_string (retval, g_value_get_string (real_value));
          break;
        }
      break;

    case G_TYPE_INT:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_int (retval, g_value_get_int (real_value));
      break;

    case G_TYPE_FLOAT:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_double (retval, g_value_get_float (real_value));
      break;

    case G_TYPE_BOXED:
      if (G_VALUE_HOLDS (real_value, G_TYPE_STRV))
        {
          gchar **strv = g_value_get_boxed (real_value);
          gint i, strv_len;
          JsonArray *array;

          strv_len = g_strv_length (strv);
          array = json_array_sized_new (strv_len);

          for (i = 0; i < strv_len; i++)
            {
              JsonNode *str = json_node_new (JSON_NODE_VALUE);

              json_node_set_string (str, strv[i]);
              json_array_add_element (array, str);
            }

          retval = json_node_new (JSON_NODE_ARRAY);
          json_node_take_array (retval, array);
        }
      else if (json_boxed_can_serialize (G_VALUE_TYPE (real_value), &node_type))
        {
          gpointer boxed = g_value_get_boxed (real_value);

          retval = json_boxed_serialize (G_VALUE_TYPE (real_value), boxed);
        }
      else
        g_warning ("Boxed type '%s' is not handled by JSON-GLib",
                   g_type_name (G_VALUE_TYPE (real_value)));
      break;

    case G_TYPE_UINT:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_int (retval, g_value_get_uint (real_value));
      break;

    case G_TYPE_LONG:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_int (retval, g_value_get_long (real_value));
      break;

    case G_TYPE_ULONG:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_int (retval, g_value_get_long (real_value));
      break;

    case G_TYPE_CHAR:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_int (retval, g_value_get_char (real_value));
      break;

    case G_TYPE_UCHAR:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_int (retval, g_value_get_uchar (real_value));
      break;

    case G_TYPE_ENUM:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_int (retval, g_value_get_enum (real_value));
      break;

    case G_TYPE_FLAGS:
      retval = json_node_new (JSON_NODE_VALUE);
      json_node_set_int (retval, g_value_get_flags (real_value));
      break;

    case G_TYPE_OBJECT:
      {
        GObject *object = g_value_get_object (real_value);

        if (object != NULL)
          {
            retval = json_node_new (JSON_NODE_OBJECT);
            json_node_take_object (retval, json_gobject_dump (object));
          }
        else
          retval = json_node_new (JSON_NODE_NULL);
      }
      break;

    case G_TYPE_NONE:
      retval = json_node_new (JSON_NODE_NULL);
      break;

    default:
      g_warning ("Unsupported type `%s'", g_type_name (G_VALUE_TYPE (real_value)));
      break;
    }

  return retval;
}
Exemple #4
0
/**
 * melo_jsonrpc_parse_request:
 * @request: the JSON-RPC requrest serialized in a string
 * @length: the length og @request, can be -1 for null-terminated string
 * @error: a pointer to a #GError which is set if an error occurred
 *
 * Parse a string @request containing a JSON-RPC serialized request, call the
 * registered callback which match the request method and present the result
 * as a JSON-RPC response serialized in a string.
 * If the method is not registered, a JSON-RPC response is generated with the
 * error MELO_JSONRPC_ERROR_METHOD_NOT_FOUND.
 *
 * Returns: (transfer full): a string containing the serialized #JsonNode
 * corresponding to the respond to the JSON-RPC request. Use g_free() after
 * usage.
 */
gchar *
melo_jsonrpc_parse_request (const gchar *request, gsize length, GError **error)
{
  JsonParser *parser;
  JsonNodeType type;
  JsonNode *req;
  JsonNode *res;
  GError *err = NULL;
  gchar *str;

  /* Create parser */
  parser = json_parser_new ();
  if (!parser)
    return melo_jsonrpc_build_error_str (MELO_JSONRPC_ERROR_INTERNAL_ERROR,
                                         "Internal error");

  /* Parse request */
  if (!json_parser_load_from_data (parser, request, length, &err) ||
      (req = json_parser_get_root (parser)) == NULL) {
    g_clear_error (&err);
    goto parse_error;
  }

  /* Get node type */
  type = json_node_get_node_type (req);

  /* Parse node */
  if (type == JSON_NODE_OBJECT) {
    /* Parse single request */
    res = melo_jsonrpc_parse_node (req);
  } else if (type == JSON_NODE_ARRAY) {
    /* Parse multiple requests: batch */
    JsonArray *req_array;
    JsonArray *res_array;
    JsonNode *node;
    guint count, i;

    /* Get array from node */
    req_array = json_node_get_array (req);
    count = json_array_get_length (req_array);
    if (!count)
      goto invalid;

    /* Create a new array for response */
    res_array = json_array_sized_new (count);
    res = json_node_new (JSON_NODE_ARRAY);
    json_node_take_array (res, res_array);

    /* Parse each elements of array */
    for (i = 0; i < count; i++) {
      /* Get element */
      node = json_array_get_element (req_array, i);

      /* Process requesit */
      node = melo_jsonrpc_parse_node (node);

      /* Add new response to array */
      if (node)
        json_array_add_element (res_array, node);
    }

    /* Check if array is empty */
    count = json_array_get_length (res_array);
    if (!count) {
      json_node_free (res);
      goto empty;
    }
  } else
    goto invalid;

  /* No response */
  if (!res)
    goto empty;

  /* Generate final string */
  str = melo_jsonrpc_node_to_string (res);

  /* Free parser and root node */
  json_node_free (res);
  g_object_unref (parser);

  return str;

parse_error:
  g_object_unref (parser);
  return melo_jsonrpc_build_error_str (MELO_JSONRPC_ERROR_PARSE_ERROR,
                                       "Parse error");
invalid:
  g_object_unref (parser);
  return melo_jsonrpc_build_error_str (MELO_JSONRPC_ERROR_INVALID_REQUEST,
                                       "Invalid request");
empty:
  g_object_unref (parser);
  return NULL;
}