JsonNode* cometd_new_handshake_message(const cometd* h) { 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, COMETD_CHANNEL_META_HANDSHAKE); json_object_set_string_member(obj, COMETD_MSG_VERSION_FIELD, COMETD_VERSION); json_object_set_string_member(obj, COMETD_MSG_MIN_VERSION_FIELD, COMETD_MIN_VERSION); // construct advice - TODO: these values should not be hardcoded JsonObject* advice = json_object_new(); json_object_set_int_member(advice, "timeout", 60000); json_object_set_int_member(advice, "interval", 0); json_object_set_object_member(obj, COMETD_MSG_ADVICE_FIELD, advice); // construct supported transports 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_object_set_array_member(obj, "supportedConnectionTypes", json_transports); // call extensions with message - TODO: implement extensions first json_node_take_object(root, obj); return root; }
static char * file_list_to_json (GList *files) { JsonNode *root; JsonArray *array; GList *ptr; char *file; JsonGenerator *gen; char *json_data; gsize len; root = json_node_new (JSON_NODE_ARRAY); array = json_array_new (); for (ptr = files; ptr; ptr = ptr->next) { file = ptr->data; json_array_add_string_element (array, file); } json_node_set_array (root, array); gen = json_generator_new (); json_generator_set_root (gen, root); json_data = json_generator_to_data (gen, &len); json_node_free (root); g_object_unref (gen); return json_data; }
void snarf_alert_add_tags(snarf_alert_t *alert, char *tags) { gchar **taglist = NULL; char **p = NULL; int tagcount = 0; char *tag = NULL; JsonObject * rootobj = json_node_get_object (alert->msg); JsonObject * envelopeobj = json_object_get_object_member(rootobj,"envelope"); JsonArray *analysis_tags=json_array_new(); taglist = g_strsplit_set(tags, ", \t\n\v\f\r", 0); for (p = taglist, tagcount = 0; p && *p; p++) { tag=*p; if (!strlen(tag)) { continue; } json_array_add_string_element(analysis_tags,tag); } json_object_set_array_member(envelopeobj,"analysis_tags",analysis_tags); }
static void trg_tracker_announce_edited(GtkCellRendererText * renderer, gchar * path, gchar * new_text, gpointer user_data) { TrgTrackersTreeViewPrivate *priv = TRG_TRACKERS_TREE_VIEW_GET_PRIVATE(user_data); GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(user_data)); gint64 torrentId = trg_trackers_model_get_torrent_id(TRG_TRACKERS_MODEL(model)); JsonArray *torrentIds = json_array_new(); JsonArray *trackerModifiers = json_array_new(); gint64 trackerId; JsonNode *req; JsonObject *args; GtkTreeIter iter; gchar *icon; gtk_tree_model_get_iter_from_string(model, &iter, path); gtk_list_store_set(GTK_LIST_STORE(model), &iter, TRACKERCOL_ANNOUNCE, new_text, -1); gtk_tree_model_get(model, &iter, TRACKERCOL_ID, &trackerId, TRACKERCOL_ICON, &icon, -1); json_array_add_int_element(torrentIds, torrentId); req = torrent_set(torrentIds); args = node_get_arguments(req); if (!g_strcmp0(icon, GTK_STOCK_ADD)) { json_array_add_string_element(trackerModifiers, new_text); json_object_set_array_member(args, "trackerAdd", trackerModifiers); } else { json_array_add_int_element(trackerModifiers, trackerId); json_array_add_string_element(trackerModifiers, new_text); json_object_set_array_member(args, "trackerReplace", trackerModifiers); } g_free(icon); dispatch_async(priv->client, req, on_trackers_update, user_data); }
JsonNode* cometd_msg_wrap(JsonNode* msg) { JsonNode* payload = json_node_new(JSON_NODE_ARRAY); JsonArray* arr = json_array_new(); json_array_add_element(arr, msg); json_node_take_array(payload, arr); return payload; }
static gboolean cockpit_channel_ensure_capable (CockpitChannel *channel, JsonObject *options) { gchar **capabilities = NULL; JsonObject *close_options = NULL; // owned by channel gboolean missing = FALSE; gboolean ret = FALSE; gint len; gint i; if (!cockpit_json_get_strv (options, "capabilities", NULL, &capabilities)) { g_message ("got invalid capabilities field in open message"); cockpit_channel_close (channel, "protocol-error"); goto out; } if (!capabilities) { ret = TRUE; goto out; } len = g_strv_length (capabilities); for (i = 0; i < len; i++) { if (channel->priv->capabilities == NULL || !strv_contains(channel->priv->capabilities, capabilities[i])) { g_message ("unsupported capability required: %s", capabilities[i]); missing = TRUE; } } if (missing) { JsonArray *arr = json_array_new (); // owned by closed options if (channel->priv->capabilities != NULL) { len = g_strv_length (channel->priv->capabilities); for (i = 0; i < len; i++) json_array_add_string_element (arr, channel->priv->capabilities[i]); } close_options = cockpit_channel_close_options (channel); json_object_set_array_member (close_options, "capabilities", arr); cockpit_channel_close (channel, "not-supported"); } ret = !missing; out: g_free (capabilities); return ret; }
void trg_prefs_load(TrgPrefs * p) { TrgPrefsPrivate *priv = p->priv; JsonParser *parser = json_parser_new(); JsonNode *root; guint n_profiles; JsonArray *profiles; gboolean parsed = json_parser_load_from_file(parser, priv->file, NULL); if (!parsed) { trg_prefs_empty_init(p); g_object_unref(parser); return; } root = json_parser_get_root(parser); if (root) { priv->user = json_node_copy(root); priv->userObj = json_node_get_object(priv->user); } g_object_unref(parser); if (!root) { trg_prefs_empty_init(p); return; } if (!json_object_has_member(priv->userObj, TRG_PREFS_KEY_PROFILES)) { profiles = json_array_new(); json_object_set_array_member(priv->userObj, TRG_PREFS_KEY_PROFILES, profiles); } else { profiles = json_object_get_array_member(priv->userObj, TRG_PREFS_KEY_PROFILES); } n_profiles = json_array_get_length(profiles); if (n_profiles < 1) { priv->profile = trg_prefs_new_profile_object(); json_array_add_object_element(profiles, priv->profile); trg_prefs_set_int(p, TRG_PREFS_KEY_PROFILE_ID, 0, TRG_PREFS_GLOBAL); } else { gint profile_id = trg_prefs_get_int(p, TRG_PREFS_KEY_PROFILE_ID, TRG_PREFS_GLOBAL); if (profile_id >= n_profiles) trg_prefs_set_int(p, TRG_PREFS_KEY_PROFILE_ID, profile_id = 0, TRG_PREFS_GLOBAL); priv->profile = json_array_get_object_element(profiles, profile_id); } }
void send_ports(Network *network, SoupWebsocketConnection *ws) { g_return_if_fail(network); g_return_if_fail(network->graph); GHashTableIter iter; gpointer key = NULL; gpointer value = NULL; JsonObject *payload = json_object_new(); json_object_set_string_member(payload, "graph", network->graph->id); // Inports JsonArray *inports = json_array_new(); json_object_set_array_member(payload, "inPorts", inports); g_hash_table_iter_init(&iter, network->graph->inports); while (g_hash_table_iter_next(&iter, &key, &value)) { const gchar *exported_port = (const gchar *)(key); JsonObject *port = json_object_new(); json_object_set_string_member(port, "id", exported_port); json_object_set_string_member(port, "type", "any"); // TODO: should be that of the target json_object_set_string_member(port, "description", ""); json_object_set_boolean_member(port, "addressable", FALSE); json_object_set_boolean_member(port, "required", FALSE); json_array_add_object_element(inports, port); } // Outports JsonArray *outports = json_array_new(); json_object_set_array_member(payload, "outPorts", outports); g_hash_table_iter_init(&iter, network->graph->outports); while (g_hash_table_iter_next(&iter, &key, &value)) { const gchar *exported_port = (const gchar *)(key); JsonObject *port = json_object_new(); json_object_set_string_member(port, "id", exported_port); json_object_set_string_member(port, "type", "any"); // TODO: should be that of the target json_object_set_string_member(port, "description", ""); json_object_set_boolean_member(port, "addressable", FALSE); json_object_set_boolean_member(port, "required", FALSE); json_array_add_object_element(outports, port); } send_response(ws, "runtime", "ports", payload); }
static JsonArray * strv_to_json_array (gchar **strv) { gint i; JsonArray *array = json_array_new (); g_assert (strv != NULL); for (i = 0; strv[i] != NULL; i++) json_array_add_string_element (array, strv[i]); return array; }
json_value* Group::ToJson() { json_value * array = json_array_new(0); json_array_push(array, json_string_new(this->name.c_str())); for(std::vector<Json*>::iterator it = this->children.begin(); it != this->children.end(); ++it) { json_array_push(array, (*it)->ToJson()); } return array; }
void add_file_id_to_array(JsonObject * args, const gchar * key, gint index) { JsonArray *array; if (json_object_has_member(args, key)) { array = json_object_get_array_member(args, key); } else { array = json_array_new(); json_object_set_array_member(args, key, array); } json_array_add_int_element(array, index); }
json_value *otl_gsub_dump_reverse(const otl_Subtable *_subtable) { const subtable_gsub_reverse *subtable = &(_subtable->gsub_reverse); json_value *_st = json_object_new(3); json_value *_match = json_array_new(subtable->matchCount); for (tableid_t j = 0; j < subtable->matchCount; j++) { json_array_push(_match, Coverage.dump(subtable->match[j])); } json_object_push(_st, "match", _match); json_object_push(_st, "to", Coverage.dump(subtable->to)); json_object_push(_st, "inputIndex", json_integer_new(subtable->inputIndex)); return _st; }
int main (int argc, char *argv[]) { MyFlowArrow *arrow; MySystem *system; JsonGenerator *gen = json_generator_new (); JsonNode *node, *root; JsonArray *array; gchar *data; gsize len; gtk_init (NULL, NULL); libgoffice_init (); root = json_node_new (JSON_NODE_ARRAY); array = json_array_new (); arrow = g_object_new (MY_TYPE_FLOW_ARROW, "x0", 100.0, "x1", 200.0, "y0", 200.0, "y1", 300.0, "energy-quantity", -22.0, "anchor", MY_ANCHOR_NORTH, "label-text", "test", NULL); node = json_gobject_serialize (G_OBJECT (arrow)); json_array_add_element (array, node); system = g_object_new (MY_TYPE_SYSTEM, "x", 100.0, "y", 200.0, NULL); node = json_gobject_serialize (G_OBJECT (system)); json_array_add_element (array, node); json_node_set_array (root, array); json_generator_set_root (gen, root); json_generator_set_pretty (gen, TRUE); data = json_generator_to_data (gen, &len); g_print ("%s\n", data); libgoffice_shutdown (); return 0; }
// 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; }
static void skypeweb_xfer_send_init(PurpleXfer *xfer) { PurpleConnection *pc = purple_account_get_connection(purple_xfer_get_account(xfer)); SkypeWebAccount *sa = purple_connection_get_protocol_data(pc); gchar *basename = g_path_get_basename(purple_xfer_get_local_filename(xfer)); gchar *id, *post, *headers; SkypeWebFileTransfer *swft = purple_xfer_get_protocol_data(xfer); JsonObject *obj = json_object_new(); JsonObject *permissions = json_object_new(); JsonArray *userpermissions = json_array_new(); purple_xfer_set_filename(xfer, basename); purple_xfer_ref(xfer); json_object_set_string_member(obj, "type", "sharing/file"); json_object_set_string_member(obj, "filename", basename); if (SKYPEWEB_BUDDY_IS_MSN(swft->from)) { id = g_strconcat("1:", swft->from, NULL); } else { id = g_strconcat("8:", swft->from, NULL); } json_array_add_string_element(userpermissions, "read"); json_object_set_array_member(permissions, id, userpermissions); json_object_set_object_member(obj, "permissions", permissions); post = skypeweb_jsonobj_to_string(obj); //POST to api.asm.skype.com /v1/objects //{"type":"sharing/file","permissions":{"8:eionrobb":["read"]},"filename":"GiantLobsterMoose.txt"} headers = g_strdup_printf("POST /v1/objects HTTP/1.0\r\n" "Connection: close\r\n" "Authorization: skype_token %s\r\n" //slightly different to normal! "Host: " SKYPEWEB_XFER_HOST "\r\n" "Content-Length: %d\r\n" "Content-Type: application/json\r\n" "\r\n\r\n%s", sa->skype_token, strlen(post), post); skypeweb_fetch_url_request(sa, "https://" SKYPEWEB_XFER_HOST, TRUE, NULL, FALSE, headers, FALSE, -1, skypeweb_got_object_for_file, swft); g_free(post); json_object_unref(obj); g_free(id); g_free(basename); }
static void ui_connection_handle_message(UiConnection *self, const gchar *protocol, const gchar *command, JsonObject *payload, SoupWebsocketConnection *ws) { if (g_strcmp0(protocol, "graph") == 0) { handle_graph_message(self, command, payload, ws); } else if (g_strcmp0(protocol, "network") == 0) { handle_network_message(self, command, payload, ws); } else if (g_strcmp0(protocol, "component") == 0 && g_strcmp0(command, "list") == 0) { // Our special Processor component JsonObject *processor = library_processor_component(); send_response(ws, "component", "component", processor); // Components for all available GEGL operations guint no_ops = 0; gchar **operation_names = gegl_list_operations(&no_ops); if (no_ops == 0) { g_warning("No GEGL operations found"); } for (int i=0; i<no_ops; i++) { const gchar *op = operation_names[i]; if (g_strcmp0(op, "gegl:seamless-clone-compose") == 0) { // FIXME: reported by GEGL but cannot be instantiated... continue; } JsonObject *component = library_component(op); send_response(ws, "component", "component", component); } } else if (g_strcmp0(protocol, "runtime") == 0 && g_strcmp0(command, "getruntime") == 0) { JsonObject *runtime = json_object_new(); json_object_set_string_member(runtime, "version", "0.4"); // protocol version json_object_set_string_member(runtime, "type", "imgflo"); JsonArray *capabilities = json_array_new(); json_array_add_string_element(capabilities, "protocol:component"); json_object_set_array_member(runtime, "capabilities", capabilities); send_response(ws, "runtime", "runtime", runtime); } else { g_printerr("Unhandled message: protocol='%s', command='%s'", protocol, command); } }
static void add_contact (JsonArray **arr, MuMsgContact *c) { JsonObject *cell; if (!*arr) *arr = json_array_new (); cell = json_object_new (); if (c->name) json_object_set_string_member (cell, "name", c->name); if (c->email) json_object_set_string_member (cell, "email", c->email); json_array_add_object_element (*arr, cell); /* consumes */ }
static void postal_http_reply_devices (PostalHttp *http, SoupMessage *message, guint status, GPtrArray *devices) { JsonGenerator *g; PostalDevice *device; JsonArray *ar; JsonNode *node; JsonNode *child; gchar *json_buf; gsize length; guint i; g_assert(SOUP_IS_MESSAGE(message)); g_assert(devices); ar = json_array_new(); node = json_node_new(JSON_NODE_ARRAY); for (i = 0; i < devices->len; i++) { device = g_ptr_array_index(devices, i); if ((child = postal_device_save_to_json(device, NULL))) { json_array_add_element(ar, child); } } json_node_set_array(node, ar); json_array_unref(ar); g = json_generator_new(); json_generator_set_root(g, node); json_node_free(node); json_generator_set_indent(g, 2); json_generator_set_pretty(g, TRUE); json_buf = json_generator_to_data(g, &length); g_object_unref(g); soup_message_set_response(message, "application/json", SOUP_MEMORY_TAKE, json_buf, length); soup_message_set_status(message, status ?: SOUP_STATUS_OK); soup_server_unpause_message(http->priv->server, message); }
static JsonNode * gvariant_to_json_array (GVariant *variant) { JsonArray *array; JsonNode *json_node; array = json_array_new (); json_node = json_node_new (JSON_NODE_ARRAY); json_node_set_array (json_node, array); json_array_unref (array); gvariant_foreach (variant, gvariant_to_json_array_foreach, array); return json_node; }
void trg_prefs_empty_init(TrgPrefs * p) { TrgPrefsPrivate *priv = p->priv; JsonArray *profiles = json_array_new(); priv->user = json_node_new(JSON_NODE_OBJECT); priv->userObj = json_object_new(); json_node_take_object(priv->user, priv->userObj); priv->profile = trg_prefs_new_profile_object(); json_array_add_object_element(profiles, priv->profile); json_object_set_array_member(priv->userObj, TRG_PREFS_KEY_PROFILES, profiles); json_object_set_int_member(priv->userObj, TRG_PREFS_KEY_PROFILE_ID, 0); }
static void on_web_socket_open (WebSocketConnection *connection, CockpitWebService *self) { CockpitSocket *socket; JsonArray *capabilities; GBytes *command; JsonObject *object; JsonObject *info; g_info ("New connection to session from %s", cockpit_creds_get_rhost (self->creds)); socket = cockpit_socket_lookup_by_connection (&self->sockets, connection); g_return_if_fail (socket != NULL); object = json_object_new (); json_object_set_string_member (object, "command", "init"); json_object_set_int_member (object, "version", 1); json_object_set_string_member (object, "channel-seed", socket->id); json_object_set_string_member (object, "host", "localhost"); json_object_set_string_member (object, "csrf-token", cockpit_creds_get_csrf_token (self->creds)); capabilities = json_array_new (); json_array_add_string_element (capabilities, "multi"); json_array_add_string_element (capabilities, "credentials"); json_array_add_string_element (capabilities, "binary"); json_object_set_array_member (object, "capabilities", capabilities); info = json_object_new (); json_object_set_string_member (info, "version", PACKAGE_VERSION); json_object_set_string_member (info, "build", COCKPIT_BUILD_INFO); json_object_set_object_member (object, "system", info); command = cockpit_json_write_bytes (object); json_object_unref (object); web_socket_connection_send (connection, WEB_SOCKET_DATA_TEXT, self->control_prefix, command); g_bytes_unref (command); /* Do we have an authorize password? if so tell the frontend */ if (cockpit_creds_get_password (self->creds)) send_socket_hints (self, "credential", "password"); g_signal_connect (connection, "message", G_CALLBACK (on_web_socket_message), self); }
void searpc_set_objlist_to_ret_object (JsonObject *object, GList *ret) { GList *ptr; if (ret == NULL) json_object_set_null_member (object, "ret"); else { JsonArray *array = json_array_new (); for (ptr = ret; ptr; ptr = ptr->next) json_array_add_element (array, json_gobject_serialize (ptr->data)); json_object_set_array_member (object, "ret", array); for (ptr = ret; ptr; ptr = ptr->next) g_object_unref (ptr->data); g_list_free (ret); } }
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); }
void gt_favourites_manager_save(GtFavouritesManager* self) { GtFavouritesManagerPrivate* priv = gt_favourites_manager_get_instance_private(self); if (g_list_length(self->favourite_channels) == 0) return; JsonArray* jarr = json_array_new(); JsonGenerator* gen = json_generator_new(); JsonNode* final = json_node_new(JSON_NODE_ARRAY); gchar* fp = FAV_CHANNELS_FILE; for (GList* l = self->favourite_channels; l != NULL; l = l->next) { JsonNode* node = json_gobject_serialize(l->data); json_array_add_element(jarr, node); } final = json_node_init_array(final, jarr);
json_value *caryll_gpos_mark_to_ligature_to_json(otl_subtable *st) { subtable_gpos_mark_to_ligature *subtable = &(st->gpos_mark_to_ligature); json_value *_subtable = json_object_new(3); json_value *_marks = json_object_new(subtable->marks->numGlyphs); json_value *_bases = json_object_new(subtable->bases->numGlyphs); for (uint16_t j = 0; j < subtable->marks->numGlyphs; j++) { json_value *_mark = json_object_new(3); sds markClassName = sdscatfmt(sdsempty(), "ac_%i", subtable->markArray->records[j].markClass); json_object_push(_mark, "class", json_string_new_length((uint32_t)sdslen(markClassName), markClassName)); sdsfree(markClassName); json_object_push(_mark, "x", json_integer_new(subtable->markArray->records[j].anchor.x)); json_object_push(_mark, "y", json_integer_new(subtable->markArray->records[j].anchor.y)); json_object_push(_marks, subtable->marks->glyphs[j].name, preserialize(_mark)); } for (uint16_t j = 0; j < subtable->bases->numGlyphs; j++) { mark_to_ligature_base *base = subtable->ligArray[j]; json_value *_base = json_array_new(base->componentCount); for (uint16_t k = 0; k < base->componentCount; k++) { json_value *_bk = json_object_new(subtable->classCount); for (uint16_t m = 0; m < subtable->classCount; m++) { if (base->anchors[k][m].present) { json_value *_anchor = json_object_new(2); json_object_push(_anchor, "x", json_integer_new(base->anchors[k][m].x)); json_object_push(_anchor, "y", json_integer_new(base->anchors[k][m].y)); sds markClassName = sdscatfmt(sdsempty(), "ac_%i", m); json_object_push_length(_bk, (uint32_t)sdslen(markClassName), markClassName, _anchor); sdsfree(markClassName); } } json_array_push(_base, _bk); } json_object_push(_bases, subtable->bases->glyphs[j].name, preserialize(_base)); } json_object_push(_subtable, "classCount", json_integer_new(subtable->classCount)); json_object_push(_subtable, "marks", _marks); json_object_push(_subtable, "bases", _bases); return _subtable; }
/** * json_builder_begin_array: * @builder: a #JsonBuilder * * Opens a subarray inside the given @builder. When done adding members to * the subarray, json_builder_end_array() must be called. * * Can be called for first or only if the call is associated to an object member * or an array element. * * Return value: (transfer none): the #JsonBuilder, or %NULL if the call was inconsistent */ JsonBuilder * json_builder_begin_array (JsonBuilder *builder) { JsonArray *array; JsonBuilderState *state; JsonBuilderState *cur_state; g_return_val_if_fail (JSON_IS_BUILDER (builder), NULL); g_return_val_if_fail (builder->priv->root == NULL, NULL); g_return_val_if_fail (g_queue_is_empty (builder->priv->stack) || json_builder_is_valid_add_mode (builder), NULL); array = json_array_new (); cur_state = g_queue_peek_head (builder->priv->stack); if (cur_state) { switch (cur_state->mode) { case JSON_BUILDER_MODE_ARRAY: json_array_add_array_element (cur_state->data.array, json_array_ref (array)); break; case JSON_BUILDER_MODE_MEMBER: json_object_set_array_member (cur_state->data.object, cur_state->member_name, json_array_ref (array)); g_free (cur_state->member_name); cur_state->member_name = NULL; cur_state->mode = JSON_BUILDER_MODE_OBJECT; break; default: g_assert_not_reached (); } } state = g_slice_new (JsonBuilderState); state->data.array = array; state->mode = JSON_BUILDER_MODE_ARRAY; g_queue_push_head (builder->priv->stack, state); return builder; }
/* Export play list to a json object */ JsonArray *plist_export_to_json( plist_t *pl ) { JsonArray *js_plist = json_array_new(); for ( int i = 0; i < pl->m_len; i ++ ) { song_t *s = pl->m_list[i]; JsonObject *js_song = json_object_new(); json_object_set_string_member(js_song, "name", song_get_name(s)); json_object_set_int_member(js_song, "length", s->m_full_len); json_object_set_int_member(js_song, "start_time", s->m_start_time); json_object_set_int_member(js_song, "end_time", s->m_end_time); if (s->m_default_title) json_object_set_string_member(js_song, "title", s->m_default_title); if (s->m_info && (s->m_info->m_flags & SI_INITIALIZED)) { song_info_t *si = s->m_info; JsonObject *js_si = json_object_new(); json_object_set_string_member(js_si, "artist", si->m_artist); json_object_set_string_member(js_si, "name", si->m_name); json_object_set_string_member(js_si, "album", si->m_album); json_object_set_string_member(js_si, "year", si->m_year); json_object_set_string_member(js_si, "comments", si->m_comments); json_object_set_string_member(js_si, "track", si->m_track); if (si->m_own_data) json_object_set_string_member(js_si, "own_data", si->m_own_data); json_object_set_object_member(js_song, "song_info", js_si); if (s->m_flags & SONG_STATIC_INFO) json_object_set_int_member(js_song, "static_info", 1); } json_array_add_object_element(js_plist, js_song); } return js_plist; }
/** * Created 07/25/2015 * @brief main json parser function * @param text: string contains json text * @return 0 - succes, pointer to json object - succes */ json_object_t * json_parser(const char *text) { struct yy_buffer_state * my_string_buffer; /* * initiate stacks and json object * TODO: add no memory exception */ stack_new(&stack_array,1024*100); stack_new(&stack_object,1024*100); json_object = json_object_new(); json_array = json_array_new(); /* * set the pointer for the folloinwg variables in the file yjson.y since, in both files * the current one and yjson.y the following valiables to set are declared as static */ set_stack_array(stack_array); set_stack_object(stack_object); set_json_object(json_object); set_json_array(json_array); // run flex code from file my_string_buffer = yy_scan_string(text); yy_switch_to_buffer( my_string_buffer ); // switch flex to the buffer we just created yyparse(); // yy_delete_buffer(my_string_buffer ); // pop top module from stack andfree it stack_free(stack_array); stack_free(stack_object); return json_object; }
/** * push_gcm_client_deliver_async: * @client: (in): A #PushGcmClient. * @identities: (element-type PushGcmIdentity*): A #GList of #PushGcmIdentity. * @message: A #PushGcmMessage. * @cancellable: (allow-none): A #GCancellable or %NULL. * @callback: A #GAsyncReadyCallback. * @user_data: User data for @callback. * * Asynchronously deliver a #PushGcmMessage to one or more GCM enabled * devices. */ void push_gcm_client_deliver_async (PushGcmClient *client, GList *identities, PushGcmMessage *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { PushGcmClientPrivate *priv; GSimpleAsyncResult *simple; SoupMessage *request; const gchar *registration_id; const gchar *collapse_key; JsonGenerator *g; JsonObject *obj; JsonObject *data; JsonObject *mdata; JsonArray *ar; JsonNode *node; GList *iter; GList *list; gchar *str; gsize length; guint time_to_live; ENTRY; g_return_if_fail(PUSH_IS_GCM_CLIENT(client)); g_return_if_fail(identities); g_return_if_fail(PUSH_IS_GCM_MESSAGE(message)); g_return_if_fail(!cancellable || G_IS_CANCELLABLE(cancellable)); g_return_if_fail(callback); priv = client->priv; request = soup_message_new("POST", PUSH_GCM_CLIENT_URL); ar = json_array_new(); for (iter = identities; iter; iter = iter->next) { g_assert(PUSH_IS_GCM_IDENTITY(iter->data)); registration_id = push_gcm_identity_get_registration_id(iter->data); json_array_add_string_element(ar, registration_id); } str = g_strdup_printf("key=%s", priv->auth_token); soup_message_headers_append(request->request_headers, "Authorization", str); g_free(str); soup_message_headers_append(request->request_headers, "Accept", "application/json"); data = json_object_new(); if ((collapse_key = push_gcm_message_get_collapse_key(message))) { json_object_set_string_member(data, "collapse_key", collapse_key); } json_object_set_boolean_member(data, "delay_while_idle", push_gcm_message_get_delay_while_idle(message)); json_object_set_boolean_member(data, "dry_run", push_gcm_message_get_dry_run(message)); if ((time_to_live = push_gcm_message_get_time_to_live(message))) { json_object_set_int_member(data, "time_to_live", time_to_live); } if ((mdata = push_gcm_message_get_data(message))) { json_object_set_object_member(data, "data", mdata); } obj = json_object_new(); json_object_set_array_member(obj, "registration_ids", ar); json_object_set_object_member(obj, "data", data); node = json_node_new(JSON_NODE_OBJECT); json_node_set_object(node, obj); json_object_unref(obj); g = json_generator_new(); json_generator_set_pretty(g, TRUE); json_generator_set_indent(g, 2); json_generator_set_root(g, node); str = json_generator_to_data(g, &length); json_node_free(node); g_object_unref(g); g_print("REQUEST: \"%s\"\n", str); soup_message_set_request(request, "application/json", SOUP_MEMORY_TAKE, str, length); simple = g_simple_async_result_new(G_OBJECT(client), callback, user_data, push_gcm_client_deliver_async); /* * Keep the list of identities around until we receive our result. * We need them to key with the resulting array. */ list = g_list_copy(identities); g_list_foreach(list, (GFunc)g_object_ref, NULL); g_object_set_data_full(G_OBJECT(simple), "identities", list, _push_gcm_identities_free); soup_session_queue_message(SOUP_SESSION(client), request, push_gcm_client_deliver_cb, simple); EXIT; }
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; }