コード例 #1
0
ファイル: json-builder.c プロジェクト: Distrotech/json-glib
/**
 * json_builder_add_boolean_value:
 * @builder: a #JsonBuilder
 * @value: the value of the member or element
 *
 * If called after json_builder_set_member_name(), sets @value as member of the
 * most recent opened object, otherwise @value is added as element of the most
 * recent opened array.
 *
 * See also: json_builder_add_value()
 *
 * Return value: (transfer none): the #JsonBuilder, or %NULL if the call was inconsistent
 */
JsonBuilder *
json_builder_add_boolean_value (JsonBuilder *builder,
                                gboolean     value)
{
  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_is_valid_add_mode (builder), NULL);

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

  switch (state->mode)
    {
    case JSON_BUILDER_MODE_MEMBER:
      json_object_set_boolean_member (state->data.object, state->member_name, value);
      g_free (state->member_name);
      state->member_name = NULL;
      state->mode = JSON_BUILDER_MODE_OBJECT;
      break;

    case JSON_BUILDER_MODE_ARRAY:
      json_array_add_boolean_element (state->data.array, value);
      break;

    default:
      g_assert_not_reached ();
    }

  return builder;
}
コード例 #2
0
ファイル: melo_jsonrpc.c プロジェクト: dillya/melo
/* Params utils */
static gboolean
melo_jsonrpc_add_node (JsonNode *node, JsonObject *schema,
                       JsonObject *obj, JsonArray *array)
{
  GType vtype = G_TYPE_INVALID;
  const gchar *s_name;
  const gchar *s_type;
  JsonNodeType type;

  /* Get name and type from schema */
  s_name = json_object_get_string_member (schema, "name");
  s_type = json_object_get_string_member (schema, "type");
  if (!s_name || !s_type)
    return FALSE;

  /* Get type */
  type = json_node_get_node_type (node);
  if (type == JSON_NODE_VALUE)
    vtype = json_node_get_value_type (node);

  /* Check type:
   * We check only first letter of the type string.
   */
  switch (s_type[0]) {
    case 'b':
      /* Boolean: check type */
      if (vtype != G_TYPE_BOOLEAN)
        return FALSE;

      /* Add to object / array */
      if (obj || array) {
        gboolean v;
        v = json_node_get_boolean (node);
        if (obj)
          json_object_set_boolean_member (obj, s_name, v);
        else
          json_array_add_boolean_element (array, v);
        break;
      }
      break;
    case 'i':
      /* Integer: check type */
      if (vtype != G_TYPE_INT64)
        return FALSE;

      /* Add to object / array */
      if (obj || array) {
        gint64 v;
        v = json_node_get_int (node);
        if (obj)
          json_object_set_int_member (obj, s_name, v);
        else
          json_array_add_int_element (array, v);
      }
      break;
    case 'd':
      /* Double: check type */
      if (vtype != G_TYPE_DOUBLE)
        return FALSE;

      /* Add to object / array */
      if (obj || array) {
        gdouble v;
        v = json_node_get_double (node);
        if (obj)
          json_object_set_double_member (obj, s_name, v);
        else
          json_array_add_double_element (array, v);
      }
      break;
    case 's':
      /* String: check type */
      if (vtype != G_TYPE_STRING)
        return FALSE;

      /* Add to object / array */
      if (obj || array) {
        const gchar *v;
        v = json_node_get_string (node);
        if (obj)
          json_object_set_string_member (obj, s_name, v);
        else
          json_array_add_string_element (array, v);
      }
      break;
    case 'o':
      /* Object: check type */
      if (type != JSON_NODE_OBJECT)
        return FALSE;

      /* Add to object / array */
      if (obj || array) {
        JsonObject *v;
        v = json_node_dup_object (node);
        if (obj)
          json_object_set_object_member (obj, s_name, v);
        else
          json_array_add_object_element (array, v);
      }
      break;
    case 'a':
      /* Array: check type */
      if (type != JSON_NODE_ARRAY)
        return FALSE;

      /* Add to object / array */
      if (obj || array) {
        JsonArray *v;
        v = json_node_dup_array (node);
        if (obj)
          json_object_set_array_member (obj, s_name, v);
        else
          json_array_add_array_element (array, v);
      }
      break;
    default:
      return FALSE;
  }
  return TRUE;
}
コード例 #3
0
ファイル: util_test.c プロジェクト: 01org/cc-oci-runtime
} END_TEST

/* FIXME: more tests required for:
 *
 * - array containing an array (of various types).
 * - array containing an object (containing various types).
 */
START_TEST(test_cc_oci_json_arr_to_string) {
	gchar *str;
	JsonArray *array;
	gboolean ret;

	ck_assert (! cc_oci_json_arr_to_string (NULL, false));
	ck_assert (! cc_oci_json_arr_to_string (NULL, true));

	array = json_array_new ();

	/* empty array, non-pretty */
	str = cc_oci_json_arr_to_string (array, false);
	ck_assert (str);
	ck_assert (! g_strcmp0 (str, "[]"));
	g_free (str);

	/* empty array, pretty */
	str = cc_oci_json_arr_to_string (array, true);
	ck_assert (str);

	ret = g_regex_match_simple (
			/* pair of braces containing optional
			 * whitespace.
			 */
			"[\\s*]",
			str, 0, 0);
	ck_assert (ret);

	g_free (str);

	json_array_add_null_element (array);
	json_array_add_null_element (array);
	json_array_add_boolean_element (array, false);
	json_array_add_boolean_element (array, true);
	json_array_add_int_element (array, 123);
	json_array_add_double_element (array, 3.1412);
	json_array_add_string_element (array, "foo bar");

	str = cc_oci_json_arr_to_string (array, false);
	ck_assert (str);

	ret = g_regex_match_simple (
			"[\\s*"                  /* opening bracket */
			"\\s*null\\s*,"          /* null */
			"\\s*null\\s*,"          /* null */
			"\\s*false\\s*,"         /* false */
			"\\s*true\\s*,"          /* true */
			"\\s*123\\s*,"           /* int */
			"\\s*3\\.1412\\s*,"      /* double */
			"\\s*\"foo\\ bar\"\\s*," /* string */
			"\\s*]",                 /* closing bracket */
			str, 0, 0);
	ck_assert (ret);
	g_free (str);

	/* clean up */
	json_array_unref (array);

} END_TEST