/** * 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; } }
void trg_prefs_set_bool(TrgPrefs * p, const gchar * key, gboolean value, int flags) { JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags | TRG_PREFS_NEWNODE); json_node_set_boolean(node, value); trg_prefs_changed_emit_signal(p, key); }
/** * json_node_init_boolean: * @node: the #JsonNode to initialize * @value: a boolean 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_boolean (JsonNode *node, gboolean value) { g_return_val_if_fail (node != NULL, NULL); json_node_init (node, JSON_NODE_VALUE); json_node_set_boolean (node, value); return node; }
/** * json_array_add_boolean_element: * @array: a #JsonArray * @value: a boolean value * * Conveniently adds a boolean @value into @array * * See also: json_array_add_element(), json_node_set_boolean() * * Since: 0.8 */ void json_array_add_boolean_element (JsonArray *array, gboolean value) { JsonNode *node; g_return_if_fail (array != NULL); node = json_node_new (JSON_NODE_VALUE); json_node_set_boolean (node, value); g_ptr_array_add (array->elements, node); }
/** * json_object_set_boolean_member: * @object: a #JsonObject * @member_name: the name of the member * @value: the value of the member * * Convenience function for setting a boolean @value of * @member_name inside @object. * * See also: json_object_set_member() * * Since: 0.8 */ void json_object_set_boolean_member (JsonObject *object, const gchar *member_name, gboolean 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_boolean (node, value); object_set_member_internal (object, member_name, node); }
void Settings::setBool(const char* path, gboolean value){ JsonNode* node = getNode(path); json_node_set_boolean(node, value); json_node_free(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_statement (JsonParser *parser, JsonScanner *scanner) { JsonParserPrivate *priv = parser->priv; guint token; token = json_scanner_peek_next_token (scanner); switch (token) { case G_TOKEN_LEFT_CURLY: return json_parse_object (parser, scanner, &priv->root); case G_TOKEN_LEFT_BRACE: return json_parse_array (parser, scanner, &priv->root); /* some web APIs are not only passing the data structures: they are * also passing an assigment, which makes parsing horribly complicated * only because web developers are lazy, and writing "var foo = " is * evidently too much to request from them. */ case JSON_TOKEN_VAR: { guint next_token; gchar *name; /* swallow the 'var' token... */ token = json_scanner_get_next_token (scanner); /* ... swallow the variable name... */ next_token = json_scanner_get_next_token (scanner); if (next_token != G_TOKEN_IDENTIFIER) return G_TOKEN_IDENTIFIER; name = g_strdup (scanner->value.v_identifier); /* ... and finally swallow the '=' */ next_token = json_scanner_get_next_token (scanner); if (next_token != '=') return '='; priv->has_assignment = TRUE; priv->variable_name = name; token = json_parse_statement (parser, scanner); /* remove the trailing semi-colon */ next_token = json_scanner_peek_next_token (scanner); if (next_token == ';') { token = json_scanner_get_next_token (scanner); return G_TOKEN_NONE; } return token; } break; case JSON_TOKEN_NULL: priv->root = priv->current_node = json_node_new (JSON_NODE_NULL); json_scanner_get_next_token (scanner); return G_TOKEN_NONE; case JSON_TOKEN_TRUE: case JSON_TOKEN_FALSE: priv->root = priv->current_node = json_node_new (JSON_NODE_VALUE); json_node_set_boolean (priv->current_node, token == JSON_TOKEN_TRUE ? TRUE : FALSE); json_scanner_get_next_token (scanner); return G_TOKEN_NONE; case '-': { guint next_token; token = json_scanner_get_next_token (scanner); next_token = json_scanner_peek_next_token (scanner); if (next_token == G_TOKEN_INT || next_token == G_TOKEN_FLOAT) { priv->root = priv->current_node = json_node_new (JSON_NODE_VALUE); token = json_scanner_get_next_token (scanner); switch (token) { case G_TOKEN_INT: json_node_set_int (priv->current_node, scanner->value.v_int64 * -1); break; case G_TOKEN_FLOAT: json_node_set_double (priv->current_node, scanner->value.v_float * -1.0); break; default: return G_TOKEN_INT; } json_scanner_get_next_token (scanner); return G_TOKEN_NONE; } else return G_TOKEN_INT; } break; case G_TOKEN_INT: case G_TOKEN_FLOAT: case G_TOKEN_STRING: token = json_scanner_get_next_token (scanner); return json_parse_value (parser, scanner, token, &priv->root); default: json_scanner_get_next_token (scanner); return G_TOKEN_SYMBOL; } }
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; }