/** * json_gvariant_serialize: * @variant: A #GVariant to convert * * Converts @variant to a JSON tree. * * Return value: (transfer full): A #JsonNode representing the root of the * JSON data structure obtained from @variant * * Since: 0.14 */ JsonNode * json_gvariant_serialize (GVariant *variant) { JsonNode *json_node = NULL; GVariantClass class; g_return_val_if_fail (variant != NULL, NULL); class = g_variant_classify (variant); if (! g_variant_is_container (variant)) { json_node = json_node_new (JSON_NODE_VALUE); switch (class) { case G_VARIANT_CLASS_BOOLEAN: json_node_set_boolean (json_node, g_variant_get_boolean (variant)); break; case G_VARIANT_CLASS_BYTE: json_node_set_int (json_node, g_variant_get_byte (variant)); break; case G_VARIANT_CLASS_INT16: json_node_set_int (json_node, g_variant_get_int16 (variant)); break; case G_VARIANT_CLASS_UINT16: json_node_set_int (json_node, g_variant_get_uint16 (variant)); break; case G_VARIANT_CLASS_INT32: json_node_set_int (json_node, g_variant_get_int32 (variant)); break; case G_VARIANT_CLASS_UINT32: json_node_set_int (json_node, g_variant_get_uint32 (variant)); break; case G_VARIANT_CLASS_INT64: json_node_set_int (json_node, g_variant_get_int64 (variant)); break; case G_VARIANT_CLASS_UINT64: json_node_set_int (json_node, g_variant_get_uint64 (variant)); break; case G_VARIANT_CLASS_HANDLE: json_node_set_int (json_node, g_variant_get_handle (variant)); break; case G_VARIANT_CLASS_DOUBLE: json_node_set_double (json_node, g_variant_get_double (variant)); break; case G_VARIANT_CLASS_STRING: case G_VARIANT_CLASS_OBJECT_PATH: case G_VARIANT_CLASS_SIGNATURE: json_node_set_string (json_node, g_variant_get_string (variant, NULL)); break; default: break; } }
static JsonNode* create_fault_msg_response(int error_num, const char *message, int id) { JsonNode *msg_node = json_node_new(JSON_NODE_VALUE); json_node_set_string(msg_node, message); JsonNode *node = create_fault_value_response(error_num, msg_node, id); json_node_free(msg_node); return node; }
static void glide_document_json_obj_set_name (GlideDocument *document, JsonObject *obj) { JsonNode *node = json_node_new (JSON_NODE_VALUE); json_node_set_string (node, document->priv->name); json_object_set_member (obj, "name", node); }
void trg_prefs_set_string(TrgPrefs * p, const gchar * key, const gchar * value, int flags) { JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags | TRG_PREFS_NEWNODE); json_node_set_string(node, value); trg_prefs_changed_emit_signal(p, key); }
/** * json_node_init_string: * @node: the #JsonNode to initialize * @value: (allow-none): a string value * * Initializes @node to %JSON_NODE_VALUE and sets @value into it. * * If the node has already been initialized once, it will be reset to * the given type, and any data contained will be cleared. * * Return value: (transfer none): the initialized #JsonNode * * Since: 0.16 */ JsonNode * json_node_init_string (JsonNode *node, const char *value) { g_return_val_if_fail (node != NULL, NULL); json_node_init (node, JSON_NODE_VALUE); json_node_set_string (node, value); return node; }
static JsonNode* to_json_string (const gchar *str) { gchar *escaped = json_strescape (str); JsonNode *node = json_node_new (JSON_NODE_VALUE); json_node_set_string (node, escaped); g_free (escaped); return node; }
/** * json_object_set_string_member: * @object: a #JsonObject * @member_name: the name of the member * @value: the value of the member * * Convenience function for setting a string @value of * @member_name inside @object. * * See also: json_object_set_member() * * Since: 0.8 */ void json_object_set_string_member (JsonObject *object, const gchar *member_name, const gchar *value) { JsonNode *node; g_return_if_fail (object != NULL); g_return_if_fail (member_name != NULL); node = json_node_new (JSON_NODE_VALUE); json_node_set_string (node, value); object_set_member_internal (object, member_name, node); }
// Create a gchar** into a json array JsonNode *glib_jsonrpc_json_strv_to_json_array(gchar **strv) { JsonArray *array = json_array_new(); gchar **p = strv; while(*p) { JsonNode *node = json_node_new(JSON_NODE_VALUE); json_node_set_string(node, *p); json_array_add_element(array,node); p++; } JsonNode *node = json_node_new(JSON_NODE_ARRAY); json_node_take_array(node, array); return node; }
/** * json_array_add_string_element: * @array: a #JsonArray * @value: a string value * * Conveniently adds a string @value into @array * * See also: json_array_add_element(), json_node_set_string() * * Since: 0.8 */ void json_array_add_string_element (JsonArray *array, const gchar *value) { JsonNode *node; g_return_if_fail (array != NULL); g_return_if_fail (value != NULL); if (value != NULL) { node = json_node_new (JSON_NODE_VALUE); json_node_set_string (node, value); } else node = json_node_new (JSON_NODE_NULL); g_ptr_array_add (array->elements, node); }
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; }
static JsonNode * builder_options_serialize_property (JsonSerializable *serializable, const gchar *property_name, const GValue *value, GParamSpec *pspec) { if (strcmp (property_name, "arch") == 0) { BuilderOptions *self = BUILDER_OPTIONS (serializable); JsonNode *retval = NULL; if (self->arch && g_hash_table_size (self->arch) > 0) { JsonObject *object; GHashTableIter iter; gpointer key, value; object = json_object_new (); g_hash_table_iter_init (&iter, self->arch); while (g_hash_table_iter_next (&iter, &key, &value)) { JsonNode *child = json_gobject_serialize (value); json_object_set_member (object, (char *)key, child); } retval = json_node_init_object (json_node_alloc (), object); json_object_unref (object); } return retval; } else if (strcmp (property_name, "env") == 0) { BuilderOptions *self = BUILDER_OPTIONS (serializable); JsonNode *retval = NULL; if (self->env && g_strv_length (self->env) > 0) { JsonObject *object; int i; object = json_object_new (); for (i = 0; self->env[i] != NULL; i++) { JsonNode *str = json_node_new (JSON_NODE_VALUE); const char *equal; g_autofree char *member = NULL; equal = strchr (self->env[i], '='); if (equal) { json_node_set_string (str, equal + 1); member = g_strndup (self->env[i], equal - self->env[i]); } else { json_node_set_string (str, ""); member = g_strdup (self->env[i]); } json_object_set_member (object, member, str); } retval = json_node_init_object (json_node_alloc (), object); json_object_unref (object); } return retval; } else return json_serializable_default_serialize_property (serializable, property_name, value, pspec); }
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; }
void Settings::setString(const char* path, const char* value){ JsonNode* node = getNode(path); json_node_set_string(node, value); json_node_free(node); }
// Create a string node JsonNode *glib_jsonrpc_json_new_string_node(const gchar *str) { JsonNode *node = json_node_new(JSON_NODE_VALUE); json_node_set_string(node, str); return node; }
static void js_value (JSContextRef ctx, JSValueRef value, JsonNode ** v) { switch (JSValueGetType (ctx, value)) { case kJSTypeUndefined: case kJSTypeNull: *v = json_node_new (JSON_NODE_NULL); break; case kJSTypeBoolean: *v = json_node_new (JSON_NODE_VALUE); json_node_set_boolean (*v, JSValueToBoolean (ctx, value) == true ? TRUE : FALSE); break; case kJSTypeNumber: *v = json_node_new (JSON_NODE_VALUE); json_node_set_double (*v, (gdouble) JSValueToNumber (ctx, value, NULL)); break; case kJSTypeString: { JSStringRef string; gchar *str; string = JSValueToStringCopy (ctx, value, NULL); str = js_string (string); JSStringRelease (string); *v = json_node_new (JSON_NODE_VALUE); json_node_set_string (*v, str); g_free (str); break; } case kJSTypeObject: { *v = json_node_new (JSON_NODE_OBJECT); JsonObject *o = json_object_new (); js_obj (ctx, JSValueToObject (ctx, value, NULL), o); json_node_take_object (*v, o); break; } } /* FIXME: array?!? integer?!? -> probably arrays are considered instances of Array() Javascript object ?! see http://developer.apple.com/library/mac/#documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/JSValueRef_h/index.html%23//apple_ref/c/func/JSValueGetType */ debug_print_json_node ( "js_value(): ", *v ); }
static guint json_parse_value (JsonParser *parser, JsonScanner *scanner, guint token, JsonNode **node) { JsonNode *current_node = parser->priv->current_node; gboolean is_negative = FALSE; if (token == '-') { guint next_token = json_scanner_peek_next_token (scanner); if (next_token == G_TOKEN_INT || next_token == G_TOKEN_FLOAT) { is_negative = TRUE; token = json_scanner_get_next_token (scanner); } else return G_TOKEN_INT; } switch (token) { case G_TOKEN_INT: *node = json_node_new (JSON_NODE_VALUE); json_node_set_int (*node, is_negative ? scanner->value.v_int64 * -1 : scanner->value.v_int64); break; case G_TOKEN_FLOAT: *node = json_node_new (JSON_NODE_VALUE); json_node_set_double (*node, is_negative ? scanner->value.v_float * -1.0 : scanner->value.v_float); break; case G_TOKEN_STRING: *node = json_node_new (JSON_NODE_VALUE); json_node_set_string (*node, scanner->value.v_string); break; case JSON_TOKEN_TRUE: case JSON_TOKEN_FALSE: *node = json_node_new (JSON_NODE_VALUE); json_node_set_boolean (*node, token == JSON_TOKEN_TRUE ? TRUE : FALSE); break; case JSON_TOKEN_NULL: *node = json_node_new (JSON_NODE_NULL); break; default: { JsonNodeType cur_type; *node = NULL; cur_type = json_node_get_node_type (current_node); if (cur_type == JSON_NODE_ARRAY) return G_TOKEN_RIGHT_BRACE; else if (cur_type == JSON_NODE_OBJECT) return G_TOKEN_RIGHT_CURLY; else return G_TOKEN_SYMBOL; } } return G_TOKEN_NONE; }