Exemplo n.º 1
0
void ScriptDialog::saveJson()
{
	json_t *root = json_new_object();
	json_t *t;
	t = json_new_number((const char*)m_id.toLatin1());
	json_insert_pair_into_object(root, "id", t);

	t = json_new_string((const char*)m_title.toLocal8Bit());
	json_insert_pair_into_object(root, "title", t);

	t = json_new_string((const char*)m_url.toLocal8Bit());
	json_insert_pair_into_object(root, "url", t);

	json_t *seqNode = json_new_array();
	for(int i=0, len=m_stepSeq.size(); i<len; ++i){
		QString s = m_stepSeq.at(i);
		json_t *v = json_new_string((const char*)s.toLocal8Bit());
		json_insert_child(seqNode, v);
	}

	json_t *stepsNode = json_new_object();
	json_insert_pair_into_object(stepsNode, "seq", seqNode);
	QMapIterator<QString, QString> iter(m_steps);
	while(iter.hasNext()){
		iter.next();
		if(iter.value().isEmpty())continue;

		QByteArray ba_k = iter.key().toLocal8Bit();
		const char *k = (const char*)ba_k;
		QByteArray ba_v = iter.value().toLocal8Bit();
		char *v = ba_v.data();

		QByteArray ba_t = m_stepTests.value(iter.key()).toLocal8Bit();
		char *t = ba_t.data();
		
		v = json_escape(v);
		t = json_escape(t);
		json_t *vn = json_new_string( v );
		json_t *tn = json_new_string( t );

		json_t *step = json_new_object();
		json_insert_pair_into_object(step, "test", tn);
		json_insert_pair_into_object(step, "script", vn);
		json_insert_pair_into_object(stepsNode, k, step);
	}
	json_insert_pair_into_object(root, "steps", stepsNode);

	BatDownUtils::writeJsonToFile(root, m_fn);
}
Exemplo n.º 2
0
std::string UniValue::write(unsigned int prettyIndent,
                            unsigned int indentLevel) const
{
    std::string s;
    s.reserve(1024);

    unsigned int modIndent = indentLevel;
    if (modIndent == 0)
        modIndent = 1;

    switch (typ) {
    case VNULL:
        s += "null";
        break;
    case VOBJ:
        writeObject(prettyIndent, modIndent, s);
        break;
    case VARR:
        writeArray(prettyIndent, modIndent, s);
        break;
    case VSTR:
        s += "\"" + json_escape(val) + "\"";
        break;
    case VNUM:
        s += val;
        break;
    case VBOOL:
        s += (val == "1" ? "true" : "false");
        break;
    }

    return s;
}
Exemplo n.º 3
0
void latin1_json(FILE* in, FILE* out) {
    int c;
    while ((c = getc(in)) != EOF) {
	int e = -1;
	switch (c) {
	    case 0x22:
	    case 0x5C:
	    case 0x2F: e = c;  break;
	    case 0x08: e = 0x62; break;
	    case 0x0C: e = 0x66; break;
	    case 0x0A: e = 0x6E; break;
	    case 0x0D: e = 0x72; break;
	    case 0x09: e = 0x74; break;
	}
	if (e != -1) {
	    json_escape(e, out);
	} else if (c <= 0x1F) {
	    fprintf(out, "\\u00%.2x", c);
	} else if (c <= 0x7F) {
	    putc(c, out);
	} else {
	    putc(0xC0 | (c >> 6), out);
	    putc(0x80 | (c & 0x3F), out);
	}
    }
}
Exemplo n.º 4
0
static int http_format_metric_response(struct evbuffer *evb, struct metric_history *mh)
{
    struct hist_file *h;
    struct sample *series = NULL;
    int series_index, series_len, series_head, start, i;

    h = mh->h;

    /* TODO: select which series to get based on the request time frame */
    series_index = 0;
    if (hist_file_get_series(h, series_index, &series, &series_len, &series_head) != S_OK)
    {
        http_format_error_response(evb,"cannot load metric data");
    }
    else
    {
        evbuffer_add_printf(evb, "{\"metric\":\"%s\",", json_escape(mh->name));
        evbuffer_add_printf(evb, "\"results\":[");
        for (i = start = SERIES_WRAP(series_head-1,series_len); i != series_head; i = SERIES_WRAP(i-1,series_len))
        {
            evbuffer_add_printf(evb,"%s[%d,%lld]", ((i == start) ? "" : ","), series[i].sample_time, (long long) series[i].value);
        }
        evbuffer_add_printf(evb, "]}\n");
    }

    return S_OK;
}
Exemplo n.º 5
0
std::string& CReg::EncodeJson( std::string &s, int tabs, int array )
{
	tabstr( s, tabs ); 
	s += array ? "[\r\n" : "{\r\n";
	
	LPREGKEY prk = NULL;
	while ( NULL != ( prk = (LPREGKEY)GetNext( prk ) ) )
	{
		int nt = tabs + 1;
		if ( !array )
		{
			tabstr( s, nt++ );
			
			// Key
			s += "\""; s += json_escape( prk->cpkey ); s += "\":\r\n";

		} // end if
	
		// Encode this block
		prk->key->EncodeJson( s, nt );

		// Next?
		if ( prk->pNext ) s += ","; s += "\r\n";
		
	} // end while
		
	tabstr( s, tabs ); 
	s += array ? "]\r\n" : "}\r\n";

	return s;
}
Exemplo n.º 6
0
static void json_dump_L(JSON *v, int L) {
	char buffer[256];
	switch(v->type) {
		case j_string: {
			printf("\"%s\"", json_escape(v->value, buffer, sizeof buffer));
		} break;
		case j_number: printf("%s", (const char *)v->value); break;
		case j_object: {
			Hash_Tbl *h = v->value;
			const char *key = ht_next (h, NULL);			
			puts("{");
			while(key) {
				printf("%*c\"%s\" : ", L*2 + 1, ' ', json_escape(key, buffer, sizeof buffer));
				json_dump_L(ht_get(h, key), L + 1);
				key = ht_next(h, key);
				if(key) 
					puts(",");
			}
			printf("}");
		} break;
		case j_array: {
			JSON *m = v->value;
			puts("[");
			while(m) {
				printf("%*c", L*2, ' ');
				json_dump_L(m, L + 1);
				m = m->next;
				if(m)
					puts(",");
			}
			printf("]");
		} break;
		case j_true: {
			printf("true");
		} break;
		case j_false: {
			printf("false");
		} break;
		case j_null: {
			printf("null");
		} break;
		default: break;
	}
}
Exemplo n.º 7
0
static void ls_long_output_json(struct sbuf *sb)
{
	static char buf[2048];
	char *esc_fname=NULL;
	char *esc_lname=NULL;
	char *fname=sb->path.buf;
	char *lname=sb->link.buf;
	struct stat *statp=&sb->statp;
	*buf='\0';

	if(fname) esc_fname=json_escape(fname);
	if(lname) esc_lname=json_escape(lname);
	open_tag(4, NULL);
	printf( "     \"name\": \"%s\",\n"
		"     \"link\": \"%s\",\n"
		"     \"st_dev\": %lu,\n"
		"     \"st_ino\": %lu,\n"
		"     \"st_mode\": %u,\n"
		"     \"st_nlink\": %lu,\n"
		"     \"st_uid\": %u,\n"
		"     \"st_gid\": %u,\n"
		"     \"st_rdev\": %lu,\n"
		"     \"st_size\": %ld,\n"
		"     \"st_atime\": %ld,\n"
		"     \"st_mtime\": %ld,\n"
		"     \"st_ctime\": %ld",
		esc_fname?esc_fname:"",
		esc_lname?esc_lname:"",
		(long unsigned int)statp->st_dev,
		(long unsigned int)statp->st_ino,
		(unsigned int)statp->st_mode,
		(long unsigned int)statp->st_nlink,
		(unsigned int)statp->st_uid,
		(unsigned int)statp->st_gid,
		(long unsigned int)statp->st_rdev,
		(long int)statp->st_size,
		(long int)statp->st_atime,
		(long int)statp->st_mtime,
		(long int)statp->st_ctime);
	if(esc_fname) free(esc_fname);
	if(esc_lname) free(esc_lname);
}
Exemplo n.º 8
0
void AddStringPair(json_t *parent, const char *name, const char *s)
{
	if (!s)
	{
		json_insert_pair_into_object(parent, name, json_new_string(""));
	}
	else
	{
		json_insert_pair_into_object(
			parent, name, json_new_string(json_escape(s)));
	}
}
Exemplo n.º 9
0
void json_stats_update(const char *mount, const char *artist, const char *title, long listeners)
{
	FILE *json;
    ice_config_t *config = config_get_config();
	if (config->json_stats_dir && (json = fopen(json_filename(config->json_stats_dir, mount), "w"))) {
		static const char mid_str[] = " - ";
		int mid = artist && title ? sizeof(mid_str) - 1 : 0;
		char *p, *t = malloc(json_escape_strlen(artist) + json_escape_strlen(title) + mid + 1);
		p = json_escape(artist, t);
		if (mid) {
			memcpy(p, mid_str, sizeof(mid_str) - 1);
			p += sizeof(mid_str) - 1;
		}
		p = json_escape(title, p);
		*p =0;
		fprintf(json, "{mount: \"%s\", title: \"%s\", listeners: %ld}\n",
				mount[0] == '/' ? mount + 1 : mount,
				t,
				listeners);
		free(t);
		fclose(json);
	}
	config_release_config();
}
Exemplo n.º 10
0
json_t*
YtkUtils::mapToJson(const YtkUtils::map_t& map)
{
    json_t* root = json_new_object();
    YtkUtils::map_t::const_iterator pos;
    for(pos = map.begin(); pos != map.end(); ++pos) {
        const char* k = pos->first.c_str();
        const char* val = pos->second.c_str();
        char* t = new char[strlen(val)+1];
        strcpy(t, val);
        char* s = json_escape( t );
        delete[] t;

        json_t *v = json_new_string(s);
        json_insert_pair_into_object(root, k, v);
    }
    return root;
}
Exemplo n.º 11
0
/**
   @brief Called by the parser when it is in the ESCAPE state.
   @param a Parser data.
   @param wc Character.
 */
static void json_string_escape(struct parser_arg *a, wchar_t wc)
{
  wchar_t esc = json_escape(wc);
  if (wc == L'\0') {
    set_state(a, END);
    a->error = JSONERR_PREMATURE_EOF;
    a->textidx--;
  } else if (wc == L'u') {
    set_state(a, UESC0);
  } else if (esc != L'\0') {
    set_state(a, INSTRING);
    set_output(a, esc);
  } else {
    set_state(a, END);
    a->error = JSONERR_UNEXPECTED_TOKEN;
    a->textidx--;
  }
}
Exemplo n.º 12
0
static inline void B(bstring headers, const bstring k, const bstring v, int *first)
{
    if(v)
    {
        if(*first) {
            bcatcstr(headers, "\"");
            *first = 0;
        } else {
            bcatcstr(headers, ",\"");
        }
        bconcat(headers, k);
        bconcat(headers, &JSON_OBJSEP);

        bstring vstr = json_escape(v);
        bconcat(headers, vstr);
        bcatcstr(headers, "\"");

        bdestroy(vstr);
    }
}
Exemplo n.º 13
0
void UniValue::writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const
{
    s += "{";
    if (prettyIndent)
        s += "\n";

    for (unsigned int i = 0; i < keys.size(); i++) {
        if (prettyIndent)
            indentStr(prettyIndent, indentLevel, s);
        s += "\"" + json_escape(keys[i]) + "\":";
        if (prettyIndent)
            s += " ";
        s += values.at(i).write(prettyIndent, indentLevel + 1);
        if (i != (values.size() - 1))
            s += ",";
        if (prettyIndent)
            s += "\n";
    }

    if (prettyIndent)
        indentStr(prettyIndent, indentLevel - 1, s);
    s += "}";
}
Exemplo n.º 14
0
void stat_header_received(HURLPath *path,
                          int response_code,
                          HURLHeader *headers,
                          size_t header_len)
{
    HURLHeader *h;
    ElementStat *stat = (ElementStat *)path->tag;
    struct tm date;
    Buffer *json = NULL;
    char *tmp;
    char *escaped;
    size_t tmp_len;

    /* Initialize HTTP statistics */
    if (!stat->http)
    {
        stat->http = calloc(1, sizeof(HTTPStat));
    }

    if (!buffer_init(&json, 1024, 256))
    {
        return;
    }

    buffer_insert_strlen(json, "{");

    stat->http->response_code = response_code;
    stat->http->header_size = (int)header_len;

    /* Check if header data should be ignored. */

    h = headers;
    while (h != NULL)
    {
        if (strcasecmp("date", h->key) == 0)
        {
            strptime(h->value, "%a, %d %b %Y %T %z", &date);
            stat->http->date = mktime(&date);
        }
        else if (strcasecmp("expires", h->key) == 0)
        {
            strptime(h->value, "%a, %d %b %Y %T %z", &date);
            stat->http->expiry_date = mktime(&date);
        }
        else if (test->stats.http.all_headers
            || hurl_header_exists(test->stat_headers, h->key))
        {
            escaped = json_escape(h->value);
            tmp_len = strlen(escaped) + strlen(h->key) + strlen("\"\":\"\",")
                + 1;
            tmp = malloc(sizeof(char) * tmp_len);
            snprintf(tmp, tmp_len, "\"%s\":\"%s\",", h->key, escaped);
            buffer_insert_strlen(json, tmp);
            free(escaped);
            free(tmp);
        }
        /* Specifically extract content type header */
        if (strcasecmp("content-type", h->key) == 0)
        {
            stat->http->content_type = allocstrcpy(h->value,
                                                   strlen(h->value),
                                                   1);
        }
        h = h->next;

    }
    /* Remove last comma */
    if (json->data_len > 1)
    {
        buffer_rewind(json, 1);
    }
    buffer_insert_strlen(json, "}");
    buffer_trim(json);
    stat->http->headers = json->head;
    free(json);
}
Exemplo n.º 15
0
Arquivo: json.c Projeto: hnkien/river
char *
json_msg(const char *channel, size_t channel_len,
		const unsigned long long seq,
		const char *data, size_t data_len,
		size_t *out_len) {

	size_t needed = 0;
	char *buffer = NULL, *pos;

	char fmt0[] = "[\"msg\", {\"channel\": \"";
	char fmt1[] = "\", \"seq\": ";
	char fmt2[] = ", \"data\": \"";
	char fmt3[] = "\"}]";

	char seq_str[40];
	int seq_len = sprintf(seq_str, "%lld", seq);

	/* escape data */
	char *esc_data, *esc_channel;
	esc_data = json_escape(data, data_len, &data_len);
	esc_channel = json_escape(channel, channel_len, &channel_len);

	needed = sizeof(fmt0)-1
		+ channel_len
		+ sizeof(fmt1)-1
		+ seq_len
		+ sizeof(fmt2)-1
		+ data_len
		+ sizeof(fmt3)-1;

	pos = buffer = rcalloc(needed + 1, 1);
	buffer[needed] = 0;

	memcpy(pos, fmt0, sizeof(fmt0)-1);
	pos += sizeof(fmt0)-1;

	memcpy(pos, esc_channel, channel_len);
	pos += channel_len;

	memcpy(pos, fmt1, sizeof(fmt1)-1);
	pos += sizeof(fmt1)-1;

	memcpy(pos, seq_str, seq_len);
	pos += seq_len;

	memcpy(pos, fmt2, sizeof(fmt2)-1);
	pos += sizeof(fmt2)-1;

	memcpy(pos, esc_data, data_len);
	pos += data_len;

	memcpy(pos, fmt3, sizeof(fmt3)-1);

	if(out_len) {
		*out_len = needed;
	}

	rfree(esc_data);
	rfree(esc_channel);

	return buffer;
}
Exemplo n.º 16
0
/* JSON metadata dumping */
static void amf_to_json(const amf_data * data, json_t ** object) {
    if (data != NULL) {
        json_t * value;
        amf_node * node;
        time_t time;
        struct tm * t;
        char str[128];
        char * escaped_str;

        switch (data->type) {
            case AMF_TYPE_NUMBER:
                sprintf(str, "%.12g", data->number_data);
                *object = json_new_number(str);
                break;
            case AMF_TYPE_BOOLEAN:
                *object = (data->boolean_data) ? json_new_true() : json_new_false();
                break;
            case AMF_TYPE_STRING:
                escaped_str = json_escape((char *)amf_string_get_bytes(data));
                *object = json_new_string(escaped_str);
                free(escaped_str);
                break;
            case AMF_TYPE_OBJECT:
                *object = json_new_object();
                node = amf_object_first(data);
                while (node != NULL) {
                    amf_to_json(amf_object_get_data(node), &value);
                    escaped_str = json_escape((char *)amf_string_get_bytes(amf_object_get_name(node)));
                    json_insert_pair_into_object(*object, escaped_str, value);
                    free(escaped_str);
                    node = amf_object_next(node);
                }
                break;
            case AMF_TYPE_NULL:
            case AMF_TYPE_UNDEFINED:
                *object = json_new_null();
                break;
            case AMF_TYPE_ASSOCIATIVE_ARRAY:
                *object = json_new_object();
                node = amf_associative_array_first(data);
                while (node != NULL) {
                    amf_to_json(amf_associative_array_get_data(node), &value);
                    json_insert_pair_into_object(*object, (const char *)amf_string_get_bytes(amf_associative_array_get_name(node)), value);
                    node = amf_associative_array_next(node);
                }
                break;
            case AMF_TYPE_ARRAY:
                *object = json_new_array();
                node = amf_array_first(data);
                while (node != NULL) {
                    amf_to_json(amf_array_get(node), &value);
                    json_insert_child(*object, value);
                    node = amf_array_next(node);
                }
                break;
            case AMF_TYPE_DATE:
                time = amf_date_to_time_t(data);
                tzset();
                t = localtime(&time);
                strftime(str, sizeof(str), "%Y-%m-%dT%H:%M:%S", t);
                *object = json_new_string(str);
                break;
            case AMF_TYPE_XML: break;
            case AMF_TYPE_CLASS: break;
            default: break;
        }
    }
}
Exemplo n.º 17
0
bool message_to_stream(uint32_t streamid, stream_t *stream, void *userdata)
{
    struct topic_message *tm = (struct topic_message *)userdata;
    char *s = NULL;

    LOG_DEBUG("rx check: %s %s", stream->topic_patt, tm->topic);

    if (topic_match_string(stream->topic_patt, tm->topic))
    {
        size_t len;

        LOG_DEBUG("%s:%s -> stream %u", tm->topic, tm->msg, streamid);

        len = strlen(tm->topic)*2 + strlen(tm->msg)*2 + 8;  // {"":""} + null // FIXME

        if (NULL == (s = (char *)malloc(len)))
            LOG_ERROR("out of mem");
        else
        {
            uint32_t connid;
            ebb_connection_info *conninfo = NULL;
            bool connected;

            connected = stream_get_connection(stream, &connid);

            if (connected)
            {
                if (NULL == (conninfo = httpd_get_conninfo(connid)))
                {
                    LOG_ERROR("bad connid");
                    return false;
                }
                if (conninfo->finished) // if conn is in process of closing, 
                    connected = false;
            }

            if (conninfo && conninfo->rawmode)
            {
                // just the data
                memcpy(s, tm->msg, strlen(tm->msg)+1);
            }
            else
            {
                // wrapped in JSON obj
                cJSON *mj = NULL;
                char *msgesc;
                char *topicesc;

                if (NULL == (topicesc = json_escape(tm->topic)))
                {
                    conninfo->http_status = 503;
                    httpd_close(connid);
                    return false;
                }

                if (tm->msg[0] == 0)
                {
                    snprintf(s, len, "{%s:\"\"}", topicesc);
                }
                else
                if (0==strcmp(tm->msg, "inf"))
                {
                    snprintf(s, len, "{%s:\"inf\"}", topicesc);
                }
                else
                if (NULL != (mj = cJSON_Parse(tm->msg)))
                {
                    snprintf(s, len, "{%s:%s}", topicesc, cJSON_PrintUnformatted(mj));
                    cJSON_Delete(mj);
                }
                else
                {
                    if (NULL == (msgesc = json_escape(tm->msg)))
                    {
                        conninfo->http_status = 503;
                        httpd_close(connid);
                        free(topicesc);
                        return false;
                    }
                    else
                    {
                        snprintf(s, len, "{%s:%s}", topicesc, msgesc);
                        free(msgesc);
                    }
                }
                free(topicesc);
            }
            if (0 != stream_push(stream, s))
                LOG_ERROR("stream_push %u failed", streamid);
            else
            {
                if (connected)
                {
                    if (!conninfo->rawmode)
                        httpd_printf(connid, "[");
                    stream_drain(stream, stream_drainer, (void *)(uintptr_t)connid);
                    if (!conninfo->rawmode)
                        httpd_printf(connid, "]");
                    stream_clear_connection(stream);
                    httpd_close(connid);
                }
                else
                {
                    LOG_DEBUG("streamid %u not connected", stream->streamid);
                }
            }
        }
    }
    if (NULL != s)
        free(s);
    return false;
}