JsonNode* cometd_ping_ls(cometd* h, char *target) { JsonObject *adviceObject = json_object_new(); JsonNode *adviceNode = json_node_new(JSON_NODE_OBJECT); JsonNode *adviceRoot = json_node_init_object(adviceNode, adviceObject); adviceObject = json_node_get_object(adviceRoot); json_object_set_string_member(adviceObject, "folder", "/"); JsonNode* root = json_node_new(JSON_NODE_OBJECT); JsonObject* obj = json_object_new(); gint64 seed = ++(h->conn->msg_id_seed); char* connection_type = cometd_current_transport(h)->name; json_object_set_int_member (obj, COMETD_MSG_ID_FIELD, seed); json_object_set_string_member(obj, COMETD_MSG_CHANNEL_FIELD, target); json_object_set_string_member(obj, "connectionType", connection_type); json_object_set_member(obj, "data", adviceRoot); json_object_set_string_member(obj, "clientId", cometd_conn_client_id(h->conn)); json_node_take_object(root, obj); return(root); }
void _cockpit_assert_json_eq_msg (const char *domain, const char *file, int line, const char *func, JsonObject *object, const gchar *expect) { GError *error = NULL; JsonNode *node; JsonNode *exnode; gchar *escaped; gchar *msg; node = json_node_init_object (json_node_alloc (), object); exnode = cockpit_json_parse (expect, -1, &error); if (error) g_assertion_message_error (domain, file, line, func, "error", error, 0, 0); g_assert (exnode); if (!cockpit_json_equal (exnode, node)) { escaped = cockpit_json_write (node, NULL); msg = g_strdup_printf ("%s != %s", escaped, expect); g_assertion_message (domain, file, line, func, msg); g_free (escaped); g_free (msg); } json_node_free (node); json_node_free (exnode); }
/** * json_array_add_object_element: * @array: a #JsonArray * @value: (transfer full): a #JsonObject * * Conveniently adds an object into @array. The @array takes ownership * of the newly added #JsonObject * * See also: json_array_add_element(), json_node_take_object() * * Since: 0.8 */ void json_array_add_object_element (JsonArray *array, JsonObject *value) { JsonNode *node; g_return_if_fail (array != NULL); node = json_node_alloc (); if (value != NULL) { json_node_init_object (node, value); json_object_unref (value); } else json_node_init_null (node); g_ptr_array_add (array->elements, node); }
/** * json_object_set_object_member: * @object: a #JsonObject * @member_name: the name of the member * @value: (transfer full): the value of the member * * Convenience function for setting an object @value of * @member_name inside @object. * * The @object will take ownership of the passed #JsonObject * * See also: json_object_set_member() * * Since: 0.8 */ void json_object_set_object_member (JsonObject *object, const gchar *member_name, JsonObject *value) { JsonNode *node; g_return_if_fail (object != NULL); g_return_if_fail (member_name != NULL); node = json_node_alloc (); if (value != NULL) { json_node_init_object (node, value); json_object_unref (value); } else json_node_init_null (node); object_set_member_internal (object, member_name, node); }
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 * js_util_tojsonnode(js_State *state, int idx) { const char *s; JsonNode *node; JsonNode *tmp; JsonObject *object; JsonArray *array; unsigned int i, length; node = json_node_alloc(); if (js_isstring(state, idx)) { json_node_init_string(node, js_tostring(state, idx)); } else if (js_isnumber(state, idx)) { json_node_init_int(node, js_tointeger(state, idx)); } else if (js_isboolean(state, idx)) { json_node_init_boolean(node, js_toboolean(state, idx)); } else if (js_isarray(state, idx)) { length = js_getlength(state, idx); array = json_array_new(); json_node_init_array(node, array); for (i = 0; i < length; i++) { js_getindex(state, idx, i); tmp = js_util_tojsonnode(state, -1); if (tmp) json_array_add_element(array, tmp); js_pop(state, 1); } json_array_unref(array); } else if (js_isobject(state, idx)) { object = json_object_new(); json_node_init_object(node, object); js_pushiterator(state, idx, 1); while((s = js_nextiterator(state, -1)) != NULL) { if (idx > 0) js_getproperty(state, idx, s); else js_getproperty(state, idx - 1, s); tmp = js_util_tojsonnode(state, -1); if (tmp) json_object_set_member(object, s, tmp); js_pop(state, 1); } js_pop(state, 1); json_object_unref(object); } else { json_node_free(node); return NULL; } return node; }
JsonNode* cometd_new_handshake_message(const cometd* h) { gint64 seed = ++(h->conn->msg_id_seed); /////////////////Data Concatenation///////////// JsonObject* dataObject = json_object_new(); JsonNode* dataNode = json_node_new(JSON_NODE_OBJECT); JsonNode* dataRoot = json_node_init_object(dataNode, dataObject); dataObject = json_node_get_object(dataRoot); json_object_set_string_member(dataObject, "login", "test2"); json_object_set_string_member(dataObject, "password", "password"); ////////////////Data Concatenation//////////// /////////////////Auth///////////////// // I begin from the most inner JsonObject of my Json File //here i initialize my Root JsonNode with an object in it and a JsonObject to be able to get the object from the JsonNode JsonObject* authObject = json_object_new(); JsonNode* authNode = json_node_new(JSON_NODE_OBJECT); JsonNode* authRoot = json_node_init_object(authNode, authObject); // Here i retrieve the initialized JsonObject that is inside my JsonNode authObject = json_node_get_object(authRoot); // And here some few insertion of strings in the object json_object_set_string_member(authObject, "action", "authenticate"); json_object_set_string_member(authObject, "type", "GmY-HuzW.KZyH.simple"); json_object_set_string_member(authObject, "resource", "zetapushTuto"); json_object_set_member(authObject, "data", dataRoot); ///////////////Auth///////////////// //////////////First Concatenation : Authentication/////////////// //Here i make the authentication of my first JsonObject with a upper membrane. JsonObject *extMembrane = json_object_new(); JsonNode *contactMembrane = json_node_new(JSON_NODE_OBJECT); JsonNode *secondRootMembrane = json_node_init_object(contactMembrane, extMembrane); extMembrane = json_node_get_object(secondRootMembrane); json_object_set_member(extMembrane, "authentication", authRoot); /////////////First Concatenation//////////////// ////////////Second Concatenation : Ext/////////////// // Here i finnaly concatenante my last JsonObject and encapsulate the overall file inside the root JsonNode JsonObject *rootObjectMembrane = json_object_new(); JsonNode *initRootMembrane = json_node_new(JSON_NODE_OBJECT); JsonNode *rootMembrane = json_node_init_object(initRootMembrane, rootObjectMembrane); rootObjectMembrane = json_node_get_object(rootMembrane); json_object_set_member(rootObjectMembrane, "ext", secondRootMembrane); // I still needs to create a object under Ext JsonNode, then an JsonArray to supportedConnectionTypes //Then add the advice object with "timeout" and "interval" json_object_set_int_member(rootObjectMembrane, "id", seed); json_object_set_string_member(rootObjectMembrane, "version", "1.0"); json_object_set_string_member(rootObjectMembrane, "minimumVersion", "1.0"); json_object_set_string_member(rootObjectMembrane, "channel", "/meta/handshake"); JsonArray* json_transports = json_array_new(); GList* entry = h->config->transports; while (entry){ cometd_transport* t = entry->data; json_array_add_string_element(json_transports, t->name); entry = g_list_next(entry); } //json_array_add_string_element(json_transports, "long-polling"); json_object_set_array_member(rootObjectMembrane, "supportedConnectionTypes", json_transports); //////////Advice//////// JsonObject *adviceMembrane = json_object_new(); JsonNode *adviceNodeMembrane = json_node_new(JSON_NODE_OBJECT); JsonNode *adviceRootMembrane = json_node_init_object(adviceNodeMembrane, adviceMembrane); gint64 interval = 0; gint64 timeout = 60000; adviceMembrane = json_node_get_object(adviceRootMembrane); json_object_set_int_member(adviceMembrane, "timeout", timeout); json_object_set_int_member(adviceMembrane, "interval", interval); json_object_set_member(rootObjectMembrane, "advice", adviceRootMembrane); /////////Advice//////// ////////////Second Concatenation////////////// // call extensions with message - TODO: implement extensions first //json_node_take_object(root, obj); return rootMembrane; }
static guint json_parse_object (JsonParser *parser, JsonScanner *scanner, JsonNode **node) { JsonParserPrivate *priv = parser->priv; JsonObject *object; JsonNode *old_current; guint token; old_current = priv->current_node; priv->current_node = json_node_init_object (json_node_alloc (), NULL); object = json_object_new (); token = json_scanner_get_next_token (scanner); g_assert (token == G_TOKEN_LEFT_CURLY); g_signal_emit (parser, parser_signals[OBJECT_START], 0); while (token != G_TOKEN_RIGHT_CURLY) { guint next_token = json_scanner_peek_next_token (scanner); JsonNode *member = NULL; gchar *name; /* we need to abort here because empty objects do not * have member names */ if (next_token == G_TOKEN_RIGHT_CURLY) break; /* parse the member's name */ if (next_token != G_TOKEN_STRING) { JSON_NOTE (PARSER, "Missing object member name"); priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD; json_object_unref (object); json_node_free (priv->current_node); priv->current_node = old_current; return G_TOKEN_STRING; } /* member name */ token = json_scanner_get_next_token (scanner); name = g_strdup (scanner->value.v_string); if (name == NULL || *name == '\0') { JSON_NOTE (PARSER, "Empty object member name"); priv->error_code = JSON_PARSER_ERROR_EMPTY_MEMBER_NAME; json_object_unref (object); json_node_free (priv->current_node); priv->current_node = old_current; return G_TOKEN_STRING; } JSON_NOTE (PARSER, "Object member '%s'", name); /* a colon separates names from values */ next_token = json_scanner_peek_next_token (scanner); if (next_token != ':') { JSON_NOTE (PARSER, "Missing object member name separator"); priv->error_code = JSON_PARSER_ERROR_MISSING_COLON; g_free (name); json_object_unref (object); json_node_free (priv->current_node); priv->current_node = old_current; return ':'; } /* we swallow the ':' */ token = json_scanner_get_next_token (scanner); g_assert (token == ':'); next_token = json_scanner_peek_next_token (scanner); /* parse the member's value */ switch (next_token) { case G_TOKEN_LEFT_BRACE: JSON_NOTE (PARSER, "Nested array at member %s", name); token = json_parse_array (parser, scanner, &member); break; case G_TOKEN_LEFT_CURLY: JSON_NOTE (PARSER, "Nested object at member %s", name); token = json_parse_object (parser, scanner, &member); break; default: /* once a member name is defined we need a value */ token = json_scanner_get_next_token (scanner); token = json_parse_value (parser, scanner, token, &member); break; } if (token != G_TOKEN_NONE || member == NULL) { /* the json_parse_* functions will have set the error code */ g_free (name); json_object_unref (object); json_node_free (priv->current_node); priv->current_node = old_current; return token; } next_token = json_scanner_peek_next_token (scanner); if (next_token == G_TOKEN_COMMA) { token = json_scanner_get_next_token (scanner); next_token = json_scanner_peek_next_token (scanner); /* look for trailing commas */ if (next_token == G_TOKEN_RIGHT_CURLY) { priv->error_code = JSON_PARSER_ERROR_TRAILING_COMMA; json_object_unref (object); json_node_free (member); json_node_free (priv->current_node); priv->current_node = old_current; return G_TOKEN_RIGHT_BRACE; } } else if (next_token == G_TOKEN_STRING) { priv->error_code = JSON_PARSER_ERROR_MISSING_COMMA; json_object_unref (object); json_node_free (member); json_node_free (priv->current_node); priv->current_node = old_current; return G_TOKEN_COMMA; } JSON_NOTE (PARSER, "Object member '%s' completed", name); json_node_set_parent (member, priv->current_node); json_object_set_member (object, name, member); g_signal_emit (parser, parser_signals[OBJECT_MEMBER], 0, object, name); g_free (name); token = next_token; } json_scanner_get_next_token (scanner); json_node_take_object (priv->current_node, object); json_node_set_parent (priv->current_node, old_current); g_signal_emit (parser, parser_signals[OBJECT_END], 0, object); if (node != NULL && *node == NULL) *node = priv->current_node; priv->current_node = old_current; return G_TOKEN_NONE; }
int main (int argc, char *argv[]) { GOptionContext *context = NULL; /* owned */ ExitStatus retval = EXIT_SUCCESS; guint i; GPtrArray/*<owned WblSchema>*/ *schemas = NULL; /* owned */ WblSchema *meta_schema = NULL; /* owned */ WblMetaSchemaType meta_schema_type; const gchar *meta_schema_name; GError *error = NULL; #if !GLIB_CHECK_VERSION (2, 35, 0) g_type_init (); #endif setlocale (LC_ALL, ""); /* Redirect debug output to stderr so that stdout is purely useful. */ g_log_set_default_handler (wbl_log, NULL); /* Command line parsing. */ context = g_option_context_new (_("— validate JSON schemas")); g_option_context_set_summary (context, _("Validate one or more JSON Schemas, " "checking for well-formedness, and " "validating against a JSON " "meta-schema.\n\nThere are two " "meta-schemas:\n • meta-schema: Used " "for schemas written for pure " "validation.\n • hyper-meta-schema: " "Used for schemas written for " "validation and hyper-linking.\n\nThe " "hyper-meta-schema is used by default, " "and is a superset of meta-schema.\n\n" "Read about JSON Schema here: " "http://json-schema.org/.")); g_option_context_add_main_entries (context, entries, PACKAGE_NAME); if (!g_option_context_parse (context, &argc, &argv, &error)) { gchar *message; message = g_strdup_printf (_("Option parsing failed: %s"), error->message); g_printerr ("%s: %s\n", argv[0], message); g_free (message); g_clear_error (&error); retval = EXIT_INVALID_OPTIONS; goto done; } /* Load the schemas. */ schemas = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); for (i = 0; option_schema_filenames != NULL && option_schema_filenames[i] != NULL; i++) { WblSchema *schema = NULL; /* owned */ schema = wbl_schema_new (); wbl_schema_load_from_file (schema, option_schema_filenames[i], &error); /* Print error or other messages from validation. */ wbl_print_validate_messages (schema, TRUE /* use colour */); if (error != NULL) { if (!option_quiet) { gchar *message; message = g_strdup_printf (_("Invalid JSON schema ‘%s’: %s"), option_schema_filenames[i], error->message); g_printerr ("%s: %s\n", argv[0], message); g_free (message); } if (retval == EXIT_SUCCESS) { retval = EXIT_INVALID_SCHEMA; } g_clear_error (&error); if (!option_ignore_errors) { g_object_unref (schema); goto done; } else { g_object_unref (schema); continue; } } g_object_set_data (G_OBJECT (schema), "filename", option_schema_filenames[i]); g_ptr_array_add (schemas, schema); /* transfer */ } /* Choose which meta-schema to use. */ if (option_no_hyper) { meta_schema_type = WBL_META_SCHEMA_META_SCHEMA; meta_schema_name = "meta-schema"; } else { meta_schema_type = WBL_META_SCHEMA_HYPER_META_SCHEMA; meta_schema_name = "hyper-meta-schema"; } meta_schema = wbl_meta_schema_load_schema (meta_schema_type, &error); g_assert_no_error (error); /* Validate each of the JSON schemas against the meta-schema. */ for (i = 0; i < schemas->len; i++) { WblSchema *schema; /* unowned */ const gchar *schema_filename; WblSchemaNode *schema_root; /* unowned */ JsonNode *schema_root_node = NULL; /* owned */ JsonObject *schema_root_object; /* unowned */ schema = schemas->pdata[i]; schema_filename = g_object_get_data (G_OBJECT (schema), "filename"); if (!option_quiet) { gchar *message; message = g_strdup_printf (_("Validating ‘%s’ against %s…"), schema_filename, meta_schema_name); g_print ("%s ", message); g_free (message); } /* Try applying the meta-schema to the schema. */ schema_root = wbl_schema_get_root (schema); schema_root_object = wbl_schema_node_get_root (schema_root); schema_root_node = json_node_alloc (); json_node_init_object (schema_root_node, schema_root_object); wbl_schema_apply (meta_schema, schema_root_node, &error); json_node_free (schema_root_node); if (!option_quiet) { g_print ("%s", (error == NULL) ? _("OK") : _("FAIL")); g_print ("\n"); } if (error != NULL) { if (!option_quiet) { gchar *message; message = g_strdup_printf (_("Validation error for ‘%s’ against %s: %s"), schema_filename, meta_schema_name, error->message); g_printerr ("%s: %s\n", argv[0], message); g_free (message); } if (retval == EXIT_SUCCESS) { retval = EXIT_SCHEMA_VALIDATION_FAILED; } g_clear_error (&error); if (!option_ignore_errors) { goto done; } } } done: if (context != NULL) { g_option_context_free (context); } if (schemas != NULL) { g_ptr_array_unref (schemas); } g_clear_object (&meta_schema); return retval; }