/** * 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_double(TrgPrefs * p, const gchar * key, gdouble value, int flags) { JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags | TRG_PREFS_NEWNODE); json_node_set_double(node, value); trg_prefs_changed_emit_signal(p, key); }
/** * json_node_init_double: * @node: the #JsonNode to initialize * @value: a floating point 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_double (JsonNode *node, gdouble value) { g_return_val_if_fail (node != NULL, NULL); json_node_init (node, JSON_NODE_VALUE); json_node_set_double (node, value); return node; }
/** * json_array_add_double_element: * @array: a #JsonArray * @value: a floating point value * * Conveniently adds a floating point @value into @array * * See also: json_array_add_element(), json_node_set_double() * * Since: 0.8 */ void json_array_add_double_element (JsonArray *array, gdouble value) { JsonNode *node; g_return_if_fail (array != NULL); node = json_node_new (JSON_NODE_VALUE); json_node_set_double (node, value); g_ptr_array_add (array->elements, node); }
/** * json_object_set_double_member: * @object: a #JsonObject * @member_name: the name of the member * @value: the value of the member * * Convenience function for setting a floating point @value * of @member_name inside @object. * * See also: json_object_set_member() * * Since: 0.8 */ void json_object_set_double_member (JsonObject *object, const gchar *member_name, gdouble 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_double (node, value); object_set_member_internal (object, member_name, node); }
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::setDouble(const char* path, gdouble value){ JsonNode* node = getNode(path); json_node_set_double (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; }