Exemple #1
0
static JsonNode *
melo_jsonrpc_build_response_node (JsonNode *result, JsonNode *error,
                                  const gchar *id, gint64 nid)
{
  JsonBuilder *builder;
  JsonNode *node;

  /* Create new builder */
  builder = json_builder_new ();
  if (!builder)
    return NULL;

  /* Begin a new object */
  json_builder_begin_object (builder);

  /* Add jsonrpc member */
  json_builder_set_member_name (builder, "jsonrpc");
  json_builder_add_string_value (builder, "2.0");

  /* Set result or error */
  if (error) {
    /* Add error member */
    json_builder_set_member_name (builder, "error");
    json_builder_add_value (builder, error);

    /* Free result if exists */
    if (result)
      json_node_free (result);
  } else if (result) {
    /* Add result member */
    json_builder_set_member_name (builder, "result");
    json_builder_add_value (builder, result);
  }

  /* Add id member: we assume ID cannot be negative */
  json_builder_set_member_name (builder, "id");
  if (nid < 0 || id)
    json_builder_add_string_value (builder, id);
  else
    json_builder_add_int_value (builder, nid);

  json_builder_end_object (builder);

  /* Get final object */
  node = json_builder_get_root (builder);

  /* Free builder */
  g_object_unref (builder);

  return node;
}
static JsonNode* create_fault_value_response(int error_num, JsonNode *msg_node, int id)
{
  JsonBuilder *builder = json_builder_new ();

  json_builder_begin_object (builder);
  json_builder_set_member_name (builder, "jsonrpc");
  json_builder_add_string_value (builder, "2.0");
  json_builder_set_member_name (builder, "error");
  json_builder_begin_object(builder);
  json_builder_set_member_name (builder, "code");
  json_builder_add_int_value(builder, error_num);
  json_builder_set_member_name (builder, "message");
  json_builder_add_value (builder, json_node_copy(msg_node));
  json_builder_end_object (builder);
  json_builder_set_member_name(builder, "id");
  if (id < 0)
    json_builder_add_null_value(builder);
  else
    json_builder_add_int_value(builder, id);
  json_builder_end_object (builder);
  JsonNode *node = json_node_copy(json_builder_get_root (builder));
  
  g_object_unref(builder);
  return node;
}
// Creates a response. Transfers ownership of reply.
static JsonNode* create_response(JsonNode *reply, int id)
{
  JsonBuilder *builder = json_builder_new ();

  // {"jsonrpc": "2.0", "result": 19, "id": 1}
  json_builder_begin_object (builder);
  json_builder_set_member_name (builder, "jsonrpc");
  json_builder_add_string_value (builder, "2.0");
  
  json_builder_set_member_name (builder, "id");
  json_builder_add_int_value (builder, id);

  json_builder_set_member_name (builder, "result");

  if (reply)
    json_builder_add_value (builder, json_node_copy(reply));
  else
    // default "ok" message
    json_builder_add_string_value (builder, "ok");

  json_builder_end_object (builder);
  
  JsonNode *node = json_node_copy(json_builder_get_root (builder));

  g_object_unref(builder);
  return node;
}
Exemple #4
0
static void do_get_action_address(IpcamIAjax *iajax, JsonBuilder *builder)
{
	JsonNode *addr_node = query_network_address(iajax);
	if (addr_node) {
		json_builder_set_member_name(builder, "address");
		json_builder_add_value(builder, addr_node);
	}
}
Exemple #5
0
void
JSONBuilder::add_JsonNode_member(const gchar *member_name,
                                 Node JsonNode_value) {
  std::unique_lock<std::mutex> lock(thread_safe_);
  json_builder_set_member_name(builder_, member_name);
  JsonNode *copy = json_node_copy(JsonNode_value->get());
  json_builder_add_value(builder_, copy);
}
Exemple #6
0
static void
consume_array_member (JsonBuilder *bob, const char* elm, JsonArray *arr)
{
	JsonNode *node;

	if (!arr)
		return; /* nothing to do */

	node = json_node_new (JSON_NODE_ARRAY);
	json_node_init_array (node, arr);
	json_array_unref (arr);

	bob = json_builder_set_member_name (bob, elm);
	bob = json_builder_add_value (bob, node); /* consumes */
}
Exemple #7
0
static JsonNode *
clutter_paint_node_to_json (ClutterPaintNode *node)
{
  JsonBuilder *builder;
  JsonNode *res;

  builder = json_builder_new ();

  json_builder_begin_object (builder);

  json_builder_set_member_name (builder, "type");
  json_builder_add_string_value (builder, g_type_name (G_TYPE_FROM_INSTANCE (node)));

  json_builder_set_member_name (builder, "name");
  json_builder_add_string_value (builder, node->name);

  json_builder_set_member_name (builder, "node-data");
  json_builder_add_value (builder, clutter_paint_node_serialize (node));

  json_builder_set_member_name (builder, "operations");
  json_builder_begin_array (builder);

  if (node->operations != NULL)
    {
      guint i;

      for (i = 0; i < node->operations->len; i++)
        {
          const ClutterPaintOperation *op;

          op = &g_array_index (node->operations, ClutterPaintOperation, i);
          json_builder_begin_object (builder);

          switch (op->opcode)
            {
            case PAINT_OP_TEX_RECT:
              json_builder_set_member_name (builder, "texrect");
              json_builder_begin_array (builder);
              json_builder_add_double_value (builder, op->op.texrect[0]);
              json_builder_add_double_value (builder, op->op.texrect[1]);
              json_builder_add_double_value (builder, op->op.texrect[2]);
              json_builder_add_double_value (builder, op->op.texrect[3]);
              json_builder_add_double_value (builder, op->op.texrect[4]);
              json_builder_add_double_value (builder, op->op.texrect[5]);
              json_builder_add_double_value (builder, op->op.texrect[6]);
              json_builder_add_double_value (builder, op->op.texrect[7]);
              json_builder_end_array (builder);
              break;

            case PAINT_OP_PATH:
              json_builder_set_member_name (builder, "path");
              json_builder_add_int_value (builder, (gint64) op->op.path);
              break;

            case PAINT_OP_PRIMITIVE:
              json_builder_set_member_name (builder, "primitive");
              json_builder_add_int_value (builder, (gint64) op->op.primitive);
              break;

            case PAINT_OP_INVALID:
              break;
            }

          json_builder_end_object (builder);
        }
    }

  json_builder_end_array (builder);

  json_builder_set_member_name (builder, "children");
  json_builder_begin_array (builder);

  if (node->first_child != NULL)
    {
      ClutterPaintNode *child;

      for (child = node->first_child;
           child != NULL;
           child = child->next_sibling)
        {
          JsonNode *n = clutter_paint_node_to_json (child);

          json_builder_add_value (builder, n);
        }
    }

  json_builder_end_array (builder);

  json_builder_end_object (builder);

  res = json_builder_get_root (builder);

  g_object_unref (builder);

  return res;
}
Exemple #8
0
void JSONBuilder::add_node_value(JsonNode *node_value) {
  std::unique_lock<std::mutex> lock(thread_safe_);
  JsonNode *copy = json_node_copy(node_value);
  json_builder_add_value(builder_, copy);
}
Exemple #9
0
static void
build_json (JsonBuilder *builder,
            GVariant *value)
{
  const GVariantType *type;
  const GVariantType *element_type;

  g_variant_ref_sink (value);

  switch (g_variant_classify (value))
    {
    case G_VARIANT_CLASS_BOOLEAN:
      json_builder_add_boolean_value (builder, g_variant_get_boolean (value));
      break;

    case G_VARIANT_CLASS_BYTE:
      json_builder_add_int_value (builder, g_variant_get_byte (value));
      break;

    case G_VARIANT_CLASS_INT16:
      json_builder_add_int_value (builder, g_variant_get_int16 (value));
      break;

    case G_VARIANT_CLASS_UINT16:
      json_builder_add_int_value (builder, g_variant_get_uint16 (value));
      break;

    case G_VARIANT_CLASS_INT32:
      json_builder_add_int_value (builder, g_variant_get_int32 (value));
      break;

    case G_VARIANT_CLASS_UINT32:
      json_builder_add_int_value (builder, g_variant_get_uint32 (value));
      break;

    case G_VARIANT_CLASS_INT64:
      json_builder_add_int_value (builder, g_variant_get_int64 (value));
      break;

    case G_VARIANT_CLASS_UINT64:
      json_builder_add_int_value (builder, g_variant_get_uint64 (value));
      break;

    case G_VARIANT_CLASS_HANDLE:
      json_builder_add_int_value (builder, g_variant_get_handle (value));
      break;

    case G_VARIANT_CLASS_DOUBLE:
      json_builder_add_double_value (builder, g_variant_get_double (value));
      break;

    case G_VARIANT_CLASS_STRING:      /* explicit fall-through */
    case G_VARIANT_CLASS_OBJECT_PATH: /* explicit fall-through */
    case G_VARIANT_CLASS_SIGNATURE:
      {
        /* HACK: We can't use json_builder_add_string_value here since
           it turns empty strings into 'null' values inside arrays.

           https://bugzilla.gnome.org/show_bug.cgi?id=730803
        */
        JsonNode *string_element = json_node_alloc ();
        json_node_init_string (string_element, g_variant_get_string (value, NULL));
        json_builder_add_value (builder, string_element);
      }
      break;

    case G_VARIANT_CLASS_VARIANT:
      build_json_variant (builder, value);
      break;

    case G_VARIANT_CLASS_ARRAY:
      type = g_variant_get_type (value);
      element_type = g_variant_type_element (type);
      if (g_variant_type_is_dict_entry (element_type))
        build_json_dictionary (builder, element_type, value);
      else
        build_json_array_or_tuple (builder, value);
      break;

    case G_VARIANT_CLASS_TUPLE:
      build_json_array_or_tuple (builder, value);
      break;

    case G_VARIANT_CLASS_DICT_ENTRY:
    case G_VARIANT_CLASS_MAYBE:
    default:
      g_return_if_reached ();
      break;
    }

  g_variant_unref (value);
}