static JSValueRef js_emit (JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) { JsonObject *obj; JsonNode *node; gchar *buffer; if (argumentCount != 1) { *exception = JSValueMakeNumber (ctx, 1); return NULL; } if (JSValueIsObject (ctx, arguments[0]) == false) { *exception = JSValueMakeNumber (ctx, 1); return NULL; } obj = json_object_new(); js_value (ctx, (JSObjectRef) arguments[0], &node); json_object_set_member (obj, "emit", node); JsonNode *node1 = json_node_new (JSON_NODE_OBJECT); if (node1 == NULL) { json_object_unref (obj); return NULL; } json_node_set_object (node1, obj); JsonGenerator *gen = json_generator_new(); if (gen == NULL) { json_node_free (node1); return NULL; } json_generator_set_root (gen, node1 ); buffer = json_generator_to_data (gen,NULL); if (buffer == NULL) { json_node_free (node1); g_object_unref (gen); return NULL; } json_node_free (node1); g_object_unref (gen); puts (buffer); g_free (buffer); return NULL; /* shouldn't be an object ? */ }
/** * json_builder_add_value: * @builder: a #JsonBuilder * @node: (transfer full): the value of the member or element * * If called after json_builder_set_member_name(), sets @node as member of the * most recent opened object, otherwise @node is added as element of the most * recent opened array. * * The builder will take ownership of the #JsonNode. * * Return value: (transfer none): the #JsonBuilder, or %NULL if the call was inconsistent */ JsonBuilder * json_builder_add_value (JsonBuilder *builder, JsonNode *node) { JsonBuilderState *state; g_return_val_if_fail (JSON_IS_BUILDER (builder), NULL); g_return_val_if_fail (node != NULL, 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_member (state->data.object, state->member_name, node); g_free (state->member_name); state->member_name = NULL; state->mode = JSON_BUILDER_MODE_OBJECT; break; case JSON_BUILDER_MODE_ARRAY: json_array_add_element (state->data.array, node); break; default: g_assert_not_reached (); } return builder; }
JsonNode* cometd_new_publish_message(const cometd* h, const char* channel, JsonNode* data) { gint64 seed = ++(h->conn->msg_id_seed); JsonNode* root = json_node_new(JSON_NODE_OBJECT); JsonObject* obj = json_object_new(); json_object_set_int_member(obj, COMETD_MSG_ID_FIELD, seed); json_object_set_string_member(obj, COMETD_MSG_CHANNEL_FIELD, channel); json_object_set_string_member(obj, COMETD_MSG_CLIENT_ID_FIELD, cometd_conn_client_id(h->conn)); json_object_set_member(obj, COMETD_MSG_DATA_FIELD, json_node_copy(data)); json_node_take_object(root, obj); return root; }
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); }
static gboolean snra_add_struct_object (GQuark field_id, const GValue * value, JsonObject * o) { JsonNode *n = snra_json_value_to_node (value); if (n) json_object_set_member (o, g_quark_to_string (field_id), n); return TRUE; }
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); }
static JsonObject * json_gobject_dump (GObject *gobject) { JsonSerializableIface *iface = NULL; JsonSerializable *serializable = NULL; gboolean serialize_property = FALSE; JsonObject *object; GParamSpec **pspecs; guint n_pspecs, i; if (JSON_IS_SERIALIZABLE (gobject)) { serializable = JSON_SERIALIZABLE (gobject); iface = JSON_SERIALIZABLE_GET_IFACE (gobject); serialize_property = (iface->serialize_property != NULL); } object = json_object_new (); pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (gobject), &n_pspecs); for (i = 0; i < n_pspecs; i++) { GParamSpec *pspec = pspecs[i]; GValue value = { 0, }; JsonNode *node = NULL; /* read only what we can */ if (!(pspec->flags & G_PARAM_READABLE)) continue; g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec)); g_object_get_property (gobject, pspec->name, &value); /* if there is a serialization vfunc, then it is completely responsible * for serializing the property, possibly by calling the implementation * of the default JsonSerializable interface through chaining up */ if (serialize_property) { node = iface->serialize_property (serializable, pspec->name, &value, pspec); } else node = json_serialize_pspec (&value, pspec); if (node) json_object_set_member (object, pspec->name, node); g_value_unset (&value); } g_free (pspecs); return object; }
void searpc_set_object_to_ret_object (JsonObject *object, GObject *ret) { if (ret == NULL) json_object_set_null_member (object, "ret"); else { json_object_set_member (object, "ret", json_gobject_serialize(ret)); g_object_unref (ret); } }
static void gvariant_to_json_object_foreach (GVariant *variant_child, gpointer user_data) { gchar *member_name; JsonNode *json_child; JsonObject *object = (JsonObject *) user_data; json_child = gvariant_dict_entry_to_json (variant_child, &member_name); json_object_set_member (object, member_name, json_child); g_free (member_name); }
static void glide_json_object_add_image_properties (JsonObject *obj, GlideImage *image) { JsonNode *n = json_node_new (JSON_NODE_OBJECT); JsonObject *img_obj = json_object_new (); json_node_set_object (n, img_obj); glide_json_object_set_string (img_obj, "filename", image->priv->filename); json_object_set_member (obj, "image-properties", n); }
void snarf_alert_add_double_field(snarf_alert_t *alert, const char *name, double value) { JsonObject * rootobj = json_node_get_object (alert->msg); JsonObject * bodyobj = json_object_get_object_member(rootobj,"body"); JsonNode * doublenode = json_node_alloc(); doublenode=json_node_init_double(doublenode,value); json_object_set_member(bodyobj,name,doublenode); }
void snarf_alert_add_int_field(snarf_alert_t *alert, const char *name, int64_t value) { JsonObject * rootobj = json_node_get_object (alert->msg); JsonObject * bodyobj = json_object_get_object_member(rootobj,"body"); JsonNode * intnode = json_node_alloc(); intnode=json_node_init_int(intnode,value); json_object_set_member(bodyobj,name,intnode); }
static gboolean process_open_dynamic_peer (CockpitRouter *self, const gchar *channel, JsonObject *options, GBytes *data, gpointer user_data) { CockpitPeer *peer = NULL; DynamicKey key = { NULL, NULL }; DynamicPeer *dp = user_data; JsonObject *config = NULL; GList *l, *names = NULL; if (dp->spawn) add_dynamic_args_to_array (&key.argv, dp->spawn, options); if (dp->env) add_dynamic_args_to_array (&key.environ, dp->env, options); peer = g_hash_table_lookup (dp->peers, &key); if (!peer) { config = json_object_new (); names = json_object_get_members (dp->config); for (l = names; l != NULL; l = g_list_next (l)) { if (!g_str_equal (l->data, "spawn") && !g_str_equal (l->data, "environ")) json_object_set_member (config, l->data, json_object_dup_member (dp->config, l->data)); } if (key.argv) json_object_set_array_member (config, "spawn", strv_to_json_array (key.argv)); if (key.environ) json_object_set_array_member (config, "environ", strv_to_json_array (key.environ)); peer = cockpit_peer_new (self->transport, config); g_hash_table_insert (dp->peers, g_memdup (&key, sizeof (DynamicKey)), peer); } else { g_strfreev (key.argv); g_strfreev (key.environ); } if (config) json_object_unref (config); g_list_free (names); return cockpit_peer_handle (peer, channel, options, data); }
void snarf_alert_add_text_field(snarf_alert_t *alert, const char *name, const char *value) { // Add text field to body JsonNode *node; JsonObject * rootobj = json_node_get_object (alert->msg); JsonObject * bodyobj = json_object_get_object_member(rootobj,"body"); JsonNode * stringnode = json_node_alloc(); stringnode=json_node_init_string(stringnode,value); json_object_set_member(bodyobj,name,stringnode); }
void snarf_alert_add_ip_field_v4(snarf_alert_t *alert, const char *name, uint32_t value) { // Add IP field to body JsonNode *node; JsonObject * rootobj = json_node_get_object (alert->msg); JsonObject * bodyobj = json_object_get_object_member(rootobj,"body"); JsonNode * stringnode = json_node_alloc(); struct in_addr ip; ip.s_addr=htonl(value); stringnode=json_node_init_string(stringnode,inet_ntoa(ip)); json_object_set_member(bodyobj,name,stringnode); }
static JsonNode *trg_prefs_get_value_inner(JsonObject * obj, const gchar * key, int type, int flags) { if (json_object_has_member(obj, key)) { if ((flags & TRG_PREFS_REPLACENODE)) json_object_remove_member(obj, key); else return json_object_get_member(obj, key); } if ((flags & TRG_PREFS_NEWNODE) || (flags & TRG_PREFS_REPLACENODE)) { JsonNode *newNode = json_node_new(type); json_object_set_member(obj, key, newNode); return newNode; } return NULL; }
static void glide_document_json_obj_set_slides (GlideDocument *document, JsonObject *obj) { JsonNode *node = json_node_new (JSON_NODE_ARRAY); JsonArray *array = json_array_new (); GList *s; for (s = document->priv->slides; s; s = s->next) { JsonNode *n; GlideSlide *slide = (GlideSlide *)(s->data); n = glide_actor_serialize (GLIDE_ACTOR (slide)); json_array_add_element (array, n); } json_node_take_array (node, array); json_object_set_member (obj, "slides", node); }
static void js_obj (JSContextRef ctx, JSObjectRef object, JsonObject * obj) { JSPropertyNameArrayRef props; gsize nprops, i; props = JSObjectCopyPropertyNames (ctx, object); nprops = JSPropertyNameArrayGetCount (props); for (i = 0; i < nprops; i++) { JSStringRef prop = JSPropertyNameArrayGetNameAtIndex (props, i); JSValueRef value; JsonNode *node; gchar *p; p = js_string (prop); g_message("Getting obj property %s\n",p); value = JSObjectGetProperty (ctx, object, prop, NULL); js_value (ctx, value, &node); g_message("Got obj property %s\n",p); debug_print_json_node ( "js_obj(): ", node ); g_message("obj=%p\n",obj); json_object_set_member (obj, p, node); g_free (p); JSStringRelease (prop); } g_message("Done get properties\n"); JSPropertyNameArrayRelease (props); }
void snarf_alert_add_flow_v4(snarf_alert_t *alert, uint64_t stime, uint32_t elapsed, uint32_t sip, uint32_t dip, uint16_t sport, uint16_t dport, uint8_t proto, uint32_t packets, uint32_t bytes, uint8_t flags, uint8_t flags_initial, uint16_t application_id, char *sensor_name, char *flow_class, char *flow_type) { JsonBuilder *builder; // Populate the envelope builder=json_builder_new(); json_builder_begin_array(builder); json_builder_begin_object(builder); json_builder_set_member_name(builder,"stime"); char jsontime[32]; time_t sectime = stime / (1000*1000); struct tm gmt; gmtime_r(§ime,&gmt); strftime(jsontime,21,"%Y-%m-%dT%H:%M:%SZ",&gmt); json_builder_add_string_value(builder,jsontime); json_builder_set_member_name(builder,"elapsed"); json_builder_add_int_value(builder,elapsed); struct in_addr ip; ip.s_addr=htonl(sip); json_builder_set_member_name(builder,"sip"); json_builder_add_string_value(builder,inet_ntoa(ip)); ip.s_addr=htonl(dip); json_builder_set_member_name(builder,"dip"); json_builder_add_string_value(builder,inet_ntoa(ip)); json_builder_set_member_name(builder,"sport"); json_builder_add_int_value(builder,sport); json_builder_set_member_name(builder,"dport"); json_builder_add_int_value(builder,dport); json_builder_set_member_name(builder,"proto"); json_builder_add_int_value(builder,proto); json_builder_set_member_name(builder,"packets"); json_builder_add_int_value(builder,packets); json_builder_set_member_name(builder,"bytes"); json_builder_add_int_value(builder,bytes); // flags json_builder_set_member_name(builder,"flags"); GString *flag_string = snarf_alert_flags_to_string(flags); json_builder_add_string_value(builder,flag_string->str); g_string_free(flag_string,TRUE); // flags initial json_builder_set_member_name(builder,"flags_initial"); GString *flag_initial_string = snarf_alert_flags_to_string(flags_initial); json_builder_add_string_value(builder,flag_initial_string->str); //g_print(flag_initial_string); g_string_free(flag_initial_string,TRUE); // application id json_builder_set_member_name(builder,"application_id"); json_builder_add_int_value(builder,application_id); // sensor json_builder_set_member_name(builder,"sensor_name"); json_builder_add_string_value(builder,sensor_name); // flow class json_builder_set_member_name(builder,"flow_class"); json_builder_add_string_value(builder,flow_class); // flow type json_builder_set_member_name(builder,"flow_type"); json_builder_add_string_value(builder,flow_type); json_builder_end_object(builder); json_builder_end_array(builder); //json_builder_end_object(builder); JsonNode *root = json_builder_get_root(builder); JsonObject * rootobj = json_node_get_object (alert->msg); JsonObject * bodyobj = json_object_get_object_member(rootobj,"body"); json_object_set_member(bodyobj,"flow",root); }
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_new (JSON_NODE_OBJECT); 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_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); /* a colon separates names from values */ next_token = json_scanner_peek_next_token (scanner); if (next_token != ':') { 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: token = json_parse_array (parser, scanner, &member); break; case G_TOKEN_LEFT_CURLY: token = json_parse_object (parser, scanner, &member); break; case G_TOKEN_INT: case G_TOKEN_FLOAT: case G_TOKEN_STRING: case '-': case JSON_TOKEN_TRUE: case JSON_TOKEN_FALSE: case JSON_TOKEN_NULL: token = json_scanner_get_next_token (scanner); token = json_parse_value (parser, scanner, token, &member); break; default: /* once a member name is defined we need a value */ token = G_TOKEN_SYMBOL; 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) { 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) { json_object_unref (object); json_node_free (member); json_node_free (priv->current_node); priv->current_node = old_current; return G_TOKEN_COMMA; } 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; }
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; }
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; }
gint main (gint argc, gchar ** argv) { JSGlobalContextRef ctx; JSStringRef str; JSObjectRef func; JSValueRef result; JSValueRef exception = NULL; JsonObject *obj; JsonNode *node; g_type_init(); gchar *buffer; if (argc != 2) { printf ("Usage: %s <script>\n", argv[0]); return 1; } ctx = JSGlobalContextCreate (NULL); str = JSStringCreateWithUTF8CString ("emitIntermediate"); func = JSObjectMakeFunctionWithCallback (ctx, str, js_emitIntermediate); JSObjectSetProperty (ctx, JSContextGetGlobalObject (ctx), str, func, kJSPropertyAttributeNone, NULL); JSStringRelease (str); str = JSStringCreateWithUTF8CString ("emit"); func = JSObjectMakeFunctionWithCallback (ctx, str, js_emit); JSObjectSetProperty (ctx, JSContextGetGlobalObject (ctx), str, func, kJSPropertyAttributeNone, NULL); JSStringRelease (str); str = JSStringCreateWithUTF8CString (argv[1]); result = JSEvaluateScript (ctx, str, NULL, NULL, 0, &exception); JSStringRelease (str); obj = json_object_new (); if (!result || exception) { js_value (ctx, exception, &node); json_object_set_member (obj, "error", node); json_object_set_boolean_member (obj, "status", FALSE); JSGlobalContextRelease (ctx); } else { json_object_set_boolean_member (obj, "status", TRUE); } JsonNode *node1 = json_node_new (JSON_NODE_OBJECT); if (node1 == NULL) { json_object_unref (obj); JSGlobalContextRelease (ctx); return 1; } json_node_set_object (node1, obj); JsonGenerator *gen = json_generator_new(); if (gen == NULL) { json_node_free (node1); JSGlobalContextRelease (ctx); return 1; } json_generator_set_root (gen, node1 ); buffer = json_generator_to_data (gen,NULL); if (buffer == NULL) { json_node_free (node1); JSGlobalContextRelease (ctx); return 1; } json_node_free (node1); puts (buffer); g_free (buffer); JSGlobalContextRelease (ctx); return 0; }
static gboolean process_includes (RpmOstreeTreeComposeContext *self, GFile *treefile_path, guint depth, JsonObject *root, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; const char *include_path; const guint maxdepth = 50; if (depth > maxdepth) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Exceeded maximum include depth of %u", maxdepth); goto out; } { g_autoptr(GFile) parent = g_file_get_parent (treefile_path); gboolean existed = FALSE; if (self->treefile_context_dirs->len > 0) { GFile *prev = self->treefile_context_dirs->pdata[self->treefile_context_dirs->len-1]; if (g_file_equal (parent, prev)) existed = TRUE; } if (!existed) { g_ptr_array_add (self->treefile_context_dirs, parent); parent = NULL; /* Transfer ownership */ } } if (!_rpmostree_jsonutil_object_get_optional_string_member (root, "include", &include_path, error)) goto out; if (include_path) { g_autoptr(GFile) treefile_dirpath = g_file_get_parent (treefile_path); g_autoptr(GFile) parent_path = g_file_resolve_relative_path (treefile_dirpath, include_path); glnx_unref_object JsonParser *parent_parser = json_parser_new (); JsonNode *parent_rootval; JsonObject *parent_root; GList *members; GList *iter; if (!json_parser_load_from_file (parent_parser, gs_file_get_path_cached (parent_path), error)) goto out; parent_rootval = json_parser_get_root (parent_parser); if (!JSON_NODE_HOLDS_OBJECT (parent_rootval)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Treefile root is not an object"); goto out; } parent_root = json_node_get_object (parent_rootval); if (!process_includes (self, parent_path, depth + 1, parent_root, cancellable, error)) goto out; members = json_object_get_members (parent_root); for (iter = members; iter; iter = iter->next) { const char *name = iter->data; JsonNode *parent_val = json_object_get_member (parent_root, name); JsonNode *val = json_object_get_member (root, name); g_assert (parent_val); if (!val) json_object_set_member (root, name, json_node_copy (parent_val)); else { JsonNodeType parent_type = json_node_get_node_type (parent_val); JsonNodeType child_type = json_node_get_node_type (val); if (parent_type != child_type) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Conflicting element type of '%s'", name); goto out; } if (child_type == JSON_NODE_ARRAY) { JsonArray *parent_array = json_node_get_array (parent_val); JsonArray *child_array = json_node_get_array (val); JsonArray *new_child = json_array_new (); guint i, len; len = json_array_get_length (parent_array); for (i = 0; i < len; i++) json_array_add_element (new_child, json_node_copy (json_array_get_element (parent_array, i))); len = json_array_get_length (child_array); for (i = 0; i < len; i++) json_array_add_element (new_child, json_node_copy (json_array_get_element (child_array, i))); json_object_set_array_member (root, name, new_child); } } } json_object_remove_member (root, "include"); } ret = TRUE; out: return ret; }
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); }
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; }