virtual ParseResultBase* Parse(const char* json, size_t length) const {
        VinenthzParseResult* pr = new VinenthzParseResult;

        json_config config;
        memset(&config, 0, sizeof(json_config));
        config.max_nesting = 0;
        config.max_data = 0;
        config.allow_c_comments = 0;
        config.allow_yaml_comments = 0;

        json_parser parser;
        json_parser_dom dom;
        json_parser_dom_init(&dom, tree_create_structure, tree_create_data, tree_append);
        json_parser_init(&parser, &config, json_parser_dom_callback, &dom);
        uint32_t processed;
        if (!json_parser_string(&parser, json, (uint32_t)length, &processed))
            pr->root = (json_val_t*)dom.root_structure;
        else {
            delete pr;
            pr = 0;
        }
        json_parser_free(&parser);
        json_parser_dom_free(&dom);
        return pr;
    }
    virtual StringResultBase* SaxRoundtrip(const char* json, size_t length) const {
        VinenthzStringResult* sr = new VinenthzStringResult();
        json_printer printer;
        json_print_init(&printer, string_buffer_append, &sr->sb);

        json_config config;
        memset(&config, 0, sizeof(json_config));
        config.max_nesting = 0;
        config.max_data = 0;
        config.allow_c_comments = 0;
        config.allow_yaml_comments = 0;

        json_parser parser;
        json_parser_init(&parser, &config, (json_parser_callback)json_print_raw, &printer);

        uint32_t processed;
        if (json_parser_string(&parser, json, (uint32_t)length, &processed) == 0)
            sr->AppendEnds();
        else {
            delete sr;
            sr = 0;
        }

        json_parser_free(&parser);
        json_print_free(&printer);
        return sr;
    }
Esempio n. 3
0
static void tt_bufevent_free(struct json_rpc_tt *jt)
{
	struct jrpc_bufevent *jb = (struct jrpc_bufevent *)jt->impl;

	json_parser_free(jb->jp);
	bufevent_free(jb->bufev);

	free(jb);
}
Esempio n. 4
0
static void fb_send_im_cb(FacebookAccount *fba, const gchar *data, gsize data_len, gpointer user_data)
{
	FacebookOutgoingMessage *msg = user_data;
	JsonParser *parser;
	JsonObject *object;
	PurpleConversation *conv;
	gchar *error = NULL;

	purple_debug_misc("facebook", "sent im response: %s\n", data);
	
	parser = fb_get_parser(data, data_len);
	if (!parser) {
		// We didn't get data, but this isn't necessarily fatal.
		purple_debug_warning("facebook", "bad data while parsing sent IM\n");
		return;
	}
	object = fb_get_json_object(parser, &error);
	
	if (error)
	{
		purple_debug_error("facebook", "sent im error: %s\n", error);
		/* there was an error, either report it or retry */
		if (msg->retry_count++ < FB_MAX_MSG_RETRY)
		{
			msg->resend_timer = purple_timeout_add_seconds(1, (GSourceFunc)fb_resend_im_fom, msg);
			fba->resending_messages = g_slist_prepend(fba->resending_messages, msg);
			json_parser_free(parser);
			return;
		}
		else
		{
			conv = purple_conversation_new(PURPLE_CONV_TYPE_IM,
					fba->account, msg->who);
			purple_conversation_write(conv, NULL, error,
					PURPLE_MESSAGE_ERROR, msg->time.tv_sec);
		}
		
		g_hash_table_remove(fba->sent_messages_hash, msg->message);
	}

	json_parser_free(parser);
	fb_msg_destroy(msg);
}
Esempio n. 5
0
static int do_format(json_config *config, const char *filename, const char *outputfile)
{
    FILE *input, *output;
    json_parser parser;
    json_printer printer;
    int ret;
    int col, lines;

    input = open_filename(filename, "r", 1);
    if (!input)
        return 2;

    output = open_filename(outputfile, "a+", 0);
    if (!output)
        return 2;

    /* initialize printer and parser structures */
    ret = json_print_init(&printer, printchannel, stdout);
    if (ret) {
        fprintf(stderr, "error: initializing printer failed: [code=%d] %s\n", ret, string_of_errors[ret]);
        return ret;
    }
    if (indent_string)
        printer.indentstr = indent_string;

    ret = json_parser_init(&parser, config, &prettyprint, &printer);
    if (ret) {
        fprintf(stderr, "error: initializing parser failed: [code=%d] %s\n", ret, string_of_errors[ret]);
        return ret;
    }

    ret = process_file(&parser, input, &lines, &col);
    if (ret) {
        fprintf(stderr, "line %d, col %d: [code=%d] %s\n",
                lines, col, ret, string_of_errors[ret]);
        return 1;
    }

    ret = json_parser_is_done(&parser);
    if (!ret) {
        fprintf(stderr, "syntax error\n");
        return 1;
    }

    /* cleanup */
    json_parser_free(&parser);
    json_print_free(&printer);
    fwrite("\n", 1, 1, stdout);
    close_filename(filename, input);
    return 0;
}
Esempio n. 6
0
xmmsv_t *
xmmsv_from_json (const char *spec)
{
	json_config conf = {
		0, /* buffer_initial_size (0=default) */
		0, /* max_nesting (0=no limit) */
		0, /* max_data (0=no limit) */
		1, /* allow_c_comments */
		0, /* allow_yaml_comments */
		NULL, /* user_calloc */
		NULL /* user_realloc */
	};
	json_parser_dom dom;
	json_parser parser;
	xmmsv_t *value;
	int error;

	json_parser_dom_init (&dom,
						  (json_parser_dom_create_structure) create_structure,
						  (json_parser_dom_create_data) create_data,
						  (json_parser_dom_append) append);
	json_parser_init (&parser, &conf, json_parser_dom_callback, &dom);

	error = json_parser_string (&parser, spec, strlen (spec), NULL);
	if (error != 0) {
		switch (error) {
			case JSON_ERROR_BAD_CHAR:
				fprintf (stderr, "Failed to parse due to bad character!\n");
				break;
			case JSON_ERROR_UNEXPECTED_CHAR:
				fprintf (stderr, "Failed to parse due to unexpected character!\n");
				break;
			case JSON_ERROR_NO_MEMORY:
				fprintf (stderr, "Failed to parse (%d)!\n", error);
				break;
		}

		return NULL;
	}

	assert (dom.root_structure != NULL);
	assert (dom.stack_offset == 0);

	value = (xmmsv_t *) dom.root_structure;

	json_parser_dom_free (&dom);
	json_parser_free (&parser);

	return value;
}
Esempio n. 7
0
static void create_list_cb(FacebookAccount *fba, const gchar *data,
	gsize data_len, gpointer userdata)
{
	// NOTE: this method can also be used for movements between
	// friend lists if necessary.

	JsonParser *parser;
	JsonObject *objnode;
	MoveRequest *request;

	// Parse out old data.
	parser = fb_get_parser(data, data_len);
	if (parser == NULL) {
		return;
	}

	objnode = fb_get_json_object(parser, NULL);
	if (!objnode ||
	    !json_object_has_member(objnode, "payload"))
	{
		json_parser_free(parser);
		return;
	}

	objnode = json_node_get_object(json_object_get_member(
			objnode, "payload"));
	fb_process_friend_lists(fba, objnode);

	json_parser_free(parser);

	// Move Friend
	request = (MoveRequest *) userdata;
	if (request) {
		handle_move_request(fba, request);
	}
}
Esempio n. 8
0
static int jsonParse(json_parser *parser, json_printer *printer, char* buf, int len, int done)
{
	int ret;
	u32 processed;
//printf("jsonParse: %s, len=%d\n", buf, len);
	ret = json_parser_string(parser, buf, len, &processed);
	if(ret)
	{
		printf("jsonParse: Error ret=%d\n", ret);
	}
	if(done)
	{
		json_parser_free(parser);
		json_print_free(printer);
	}
	return ret;
}
Esempio n. 9
0
static int
parse_clfjson(void *ctx, str_t *str, logmeta_t *meta) {
	clfjson_state_t state = {0, 0, 0, str, meta};
	json_parser parser;
	size_t processed = 0;
	logmeta_clear(meta);
	logmeta_hash(meta, str);
	json_parser_init(&parser, NULL, clfjson_state, &state);
	json_parser_string(&parser, (const char *)str->ptr, str->len, &processed);
	str_ptime_rfc1123(logmeta_field(meta, LOGPIPE_TIMESTAMP), &meta->utc_timestamp);
	json_parser_free(&parser);
	return ! logmeta_field_isempty(meta, LOGPIPE_C_IP)
		&& ! logmeta_field_isempty(meta, LOGPIPE_TIMESTAMP)
		&& ! logmeta_field_isempty(meta, LOGPIPE_CS_METHOD)
		&& ! logmeta_field_isempty(meta, LOGPIPE_CS_URI_STEM)
		&& ! logmeta_field_isempty(meta, LOGPIPE_SC_STATUS);
}
Esempio n. 10
0
static int do_tree(json_config *config, const char *filename, json_val_t **root_structure)
{
    FILE *input;
    json_parser parser;
    json_parser_dom dom;
    int ret;
    int col, lines;

    input = open_filename(filename, "r", 1);
    if (!input)
        return 2;

    ret = json_parser_dom_init(&dom, tree_create_structure, tree_create_data, tree_append);
    if (ret) {
        fprintf(stderr, "error: initializing helper failed: [code=%d] %s\n", ret, string_of_errors[ret]);
        return ret;
    }

    ret = json_parser_init(&parser, config, json_parser_dom_callback, &dom);
    if (ret) {
        fprintf(stderr, "error: initializing parser failed: [code=%d] %s\n", ret, string_of_errors[ret]);
        return ret;
    }

    ret = process_file(&parser, input, &lines, &col);
    if (ret) {
        fprintf(stderr, "line %d, col %d: [code=%d] %s\n",
                lines, col, ret, string_of_errors[ret]);

        return 1;
    }

    ret = json_parser_is_done(&parser);
    if (!ret) {
        fprintf(stderr, "syntax error\n");
        return 1;
    }

    if (root_structure)
        *root_structure = dom.root_structure;

    /* cleanup */
    json_parser_free(&parser);
    close_filename(filename, input);
    return 0;
}
    virtual bool SaxStatistics(const char* json, size_t length, Stat* stat) const {
        json_config config;
        memset(&config, 0, sizeof(json_config));
        config.max_nesting = 0;
        config.max_data = 0;
        config.allow_c_comments = 0;
        config.allow_yaml_comments = 0;

        memset(stat, 0, sizeof(Stat));
        StatContext context;
        context.stat = stat;
        context.after_key = true;

        json_parser parser;
        json_parser_init(&parser, &config, (json_parser_callback)stat_callback, &context);

        uint32_t processed;
        bool ret = (json_parser_string(&parser, json, (uint32_t)length, &processed) == 0);

        json_parser_free(&parser);
        return ret;
    }
Esempio n. 12
0
jobj * jobj_parse(const char * json_str)
{
	int res;
	jobj * o = NULL;
	json_parser_dom dom;
	json_parser p;
	json_config config;

	memset(&config, 0, sizeof(json_config));

	res = json_parser_dom_init(&dom, dom_mknode, dom_mkval, dom_append);
	if(res) return NULL;
	res = json_parser_init(&p, &config, json_parser_dom_callback, &dom);
	if(res) return NULL;

	res = json_parser_string(&p, json_str, strlen(json_str), NULL);
	if(!res && json_parser_is_done(&p)) o = dom.root_structure;
	
	json_parser_free(&p);
	json_parser_dom_free(&dom);
	return o;	
}
Esempio n. 13
0
 ~parser_context() {
     json_parser_free(&parser_);
 }
Esempio n. 14
0
void got_reconnect_json(FacebookAccount *fba, const gchar *data, gsize data_len, gpointer userdata)
{
	JsonParser *parser;
	JsonObject *objnode;
	gchar *error_message;

	parser = fb_get_parser(data, data_len);

	if (!parser) {
		purple_debug_error("facebook", "couldn't parse reconnect data\n");
		purple_debug_info("facebook", "page content: %s\n", data);
		purple_connection_error_reason(fba->pc,
				PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
				_("Chat service currently unavailable"));
		return;
	}

	objnode = fb_get_json_object(parser, &error_message);
	
	if (error_message != NULL)
	{
		if (json_node_get_int(json_object_get_member(objnode, "error")) == 1356007)
		{
			//There'll normally be an error message if chat is down for maintenance
			purple_connection_error_reason(fba->pc,
				PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE,
				error_message);
			g_free(error_message);
			json_parser_free(parser);
			return;
		}
	}

	JsonObject *payload = json_node_get_object(json_object_get_member(objnode, "payload"));
	
	/* eg {"host":"channel01"} */
	const gchar *new_channel_host = json_node_get_string(json_object_get_member(payload, "host"));

	if (new_channel_host == NULL)
	{
		purple_debug_error("facebook", "couldn't find new channel number\n");
		purple_debug_info("facebook", "page content: %s\n", data);
		purple_connection_error_reason(fba->pc,
				PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
				_("Error fetching channel; did you log in elsewhere?"));
		json_parser_free(parser);
		return;
	}
	
	g_free(fba->channel_number);
	fba->channel_number = g_strdup(new_channel_host);
	
	gint new_seq = json_node_get_int(json_object_get_member(payload, "seq"));
	fba->message_fetch_sequence = new_seq;
	
	/*
	 * Now that we have a channel number we can start looping and
	 * waiting for messages
	 */
	fb_get_new_messages(fba);
	json_parser_free(parser);
}
Esempio n. 15
0
static void got_new_messages(FacebookAccount *fba, const gchar *data,
		gsize data_len, gpointer userdata)
{
	JsonParser *parser;

	PurpleConnection *pc = userdata;

	purple_debug_misc("facebook", "got new messages:\n%s\n", data);

	/* for (;;);{"t":"msg","c":"p_800753867","ms":[{"type":"msg",
		"msg":{"text":"yes","time":1211176515861,"clientTime":1211176514750,
		"msgID":"367146364"},"from":596176850,"to":800753867,
		"from_name":"Jeremy Lawson","to_name":"Eion Robb",
		"from_first_name":"Jeremy","to_first_name":"Eion"}]} */
	/* for (;;);{"t":"refresh"} */
	/* for (;;);{"t":"msg","c":"p_800753867","ms":[{"type":"msg",
		"msg":{"text":"p**n head","time":1211177326689,"clientTime":1211177325,
		"msgID":"-1992480367"},"from":800753867,"to":596176850,
		"from_name":"Eion Robb","to_name":"Jeremy Lawson",
		"from_first_name":"Eion","to_first_name":"Jeremy"}]} */
	/* for (;;);{"t":"msg","c":"p_800753867","ms":[{"type":"typ","st":1,
		"from":596176850,"to":800753867},{"type":"msg","msg":{"text":"nubile!",
		"time":1211177334019,"clientTime":1211177326690,"msgID":"696260545"},
		"from":596176850,"to":800753867,"from_name":"Jeremy Lawson",
		"to_name":"Eion Robb","from_first_name":"Jeremy","to_first_name":"Eion"},
		{"type":"msg","msg":{"text":"test2","time":1211177336688,
		"clientTime":1211177326691,"msgID":"1527815367"},"from":596176850,
		"to":800753867,"from_name":"Jeremy Lawson","to_name":"Eion Robb",
		"from_first_name":"Jeremy","to_first_name":"Eion"},{"type":"msg",
		"msg":{"text":"ahhhhhhh!","time":1211177344361,"clientTime":1211177326692,
		"msgID":"4028916254"},"from":596176850,"to":800753867,
		"from_name":"Jeremy Lawson","to_name":"Eion Robb",
		"from_first_name":"Jeremy","to_first_name":"Eion"}]} */
	/* for (;;);{"t":"msg","c":"p_800753867","ms":[{"type":"msg",
		"msg":{"text":"2","time":1211178167261,"clientTime":1211178164656,
		"msgID":"3382240259"},"from":596176850,"to":800753867,
		"from_name":"Jeremy Lawson","to_name":"Eion Robb",
		"from_first_name":"Jeremy","to_first_name":"Eion"}]} */
	/* for (;;);{"t":"refresh", "seq":1} */

	parser = fb_get_parser(data, data_len);
	if (!parser)
	{
		/* Sometimes proxies will return incorrect data, so we just shrug 
		 * it off.
		 * TODO: Only do this for proxies.  And when we do it, keep track
		 * of consecutive failures in the case something is actually
		 * wrong with Facebook.  Eventually this condition should cause
		 * failure */
		/* Continue looping, waiting for more messages */
		purple_debug_error("facebook",
				"got data back, but it's not even json\n");
				
		fb_get_new_messages(fba);
		return;
	}

	JsonObject *objnode = fb_get_json_object(parser, NULL);

	if (json_object_has_member(objnode, "t")) {
		const gchar* command = json_node_get_string(json_object_get_member(objnode, "t"));
		if (g_str_equal(command, "refresh")) {
			if (json_object_has_member(objnode, "seq")) {
				fba->message_fetch_sequence = json_node_get_int(
					json_object_get_member(objnode, "seq"));
			}

			/* grab history items for all open conversations */
			GList *conversations = purple_get_conversations();
			while (conversations != NULL) {
				PurpleConversation *conv =
					(PurpleConversation *)conversations->data;
				if (fb_conversation_is_fb(conv)) {
					purple_debug_info("facebook",
						"checking for dropped messages with %s\n",
						conv->name);
					fb_history_fetch(fba, conv->name, FALSE);
				}
				conversations = conversations->next;
			}

			/* refresh means that the channel is invalid */
			fb_reconnect(fba);
			json_parser_free(parser);
			return;
		} else if (g_str_equal(command, "continue")) {
			/* continue means that the server wants us to remake the connection.
 			 * continue the loop and wait for messages. noop. */
		} else if (g_str_equal(command, "msg")) {
			parse_new_messages(pc, fba,
				json_node_get_array(json_object_get_member(objnode, "ms")));
		}
	}
	
	if (json_object_has_member(objnode, "s"))
	{
		gint new_seq = json_node_get_int(json_object_get_member(objnode, "s"));
		fba->message_fetch_sequence = new_seq;
	}

	json_parser_free(parser);

	/* Continue looping, waiting for more messages */
	fb_get_new_messages(fba);
}