Example #1
0
int main( int argc, char *argv[] ) {
	(void) argc;
	GError *err = NULL;

	ruminate_init(argv[0], &err);
	die_if_error(err);

	RType *rt = ruminate_get_type(stdout, &err);
	die_if_error(err);

	JsonState *st = json_state_new();
	json_state_add_hook(st, g_quark_from_static_string("void"), &void_hook);
	json_state_add_hook(st, g_quark_from_static_string("char *"), &char_ptr_hook);
	json_state_set_flags(st, JSON_FLAG_SKIP_UNKNOWN);

	printf("Hello World!");
	json_t *serialized = json_serialize(st, rt, &stdout, &err);
	die_if_error(err);

	printf("\n");
	json_dumpf(serialized, stdout, 0);
	printf("\n");

	json_decref(serialized);
	r_type_unref(rt);
	return EXIT_SUCCESS;
}
Example #2
0
const char* MoBagelReport::body() {

    /* create json object for post */
    json_value *data = json_object_new(0);

    // property int
    for (map<string,int>::iterator it = _property_int.begin(); it!=_property_int.end(); ++it) {
        string key = it->first;
        int value = it->second;
        json_object_push(data, key.c_str(), json_integer_new(value));
    }

    // property double
    for (map<string,double>::iterator it = _property_double.begin(); it!=_property_double.end(); ++it) {
        string key = it->first;
        double value = it->second;
        json_object_push(data, key.c_str(), json_double_new(value));
    }

    // property string
    for (map<string,string>::iterator it = _property_string.begin(); it!=_property_string.end(); ++it) {
        string key = it->first;
        string value = it->second;
        json_object_push(data, key.c_str(), json_string_new(value.c_str()));
    }

    char * buf = (char*)malloc(json_measure(data));
    json_serialize(buf, data);
    return buf;
}
char * slayer_server_stats_tojson(slayer_server_stats_t *istats,apr_pool_t *mpool) {
	slayer_server_stats_t stats;
	slayer_server_stats_get(istats,&stats);

	json_value *container = json_object_create(mpool);
	json_object_add(container,"total_requests",json_long_create(mpool,stats.total_requests));
	json_value *hits = json_array_create(mpool,stats.nslice);
	json_value *slices = json_array_create(mpool,stats.nslice);
	int i;
	for ( i  = stats.offset+1; i < stats.nslice; i++) {
		if (stats.slices[i] != 0) {
			json_array_append(hits,json_long_create(mpool,stats.hits[i]));
			json_array_append(slices,json_long_create(mpool,stats.slices[i]));
		}
	}
	for ( i = 0; i < stats.offset; i++) {
		if (stats.slices[i] != 0) {
			json_array_append(hits,json_long_create(mpool,stats.hits[i]));
			json_array_append(slices,json_long_create(mpool,stats.slices[i]));
		}
	}
	json_object_add(container,"hits",hits);
	json_object_add(container,"slices",slices);
	json_object_add(container,"start_time",json_long_create(mpool,stats.start_time));
	json_object_add(container,"current_time",json_long_create(mpool,apr_time_now() / (1000*1000)));
	return json_serialize(mpool,container);
}
Example #4
0
/*
 * json_print_object() - serialize and print the nvObj array directly (w/o header & footer)
 *
 *	Ignores JSON verbosity settings and everything else - just serializes the list & prints
 *	Useful for reports and other simple output.
 *	Object list should be terminated by nv->nx == NULL
 */
void json_print_object(nvObj_t *nv)
{
#ifdef __SILENCE_JSON_RESPONSES
	return;
#endif

	json_serialize(nv, cs.out_buf, sizeof(cs.out_buf));
	fprintf(stderr, "%s", (char *)cs.out_buf);
}
Example #5
0
/*
 * Main application.
 *
 * Args:
 * [1] -- The path to the kinetics batch file (.bkn) to read.
 *
 */
int main(int argc, char **argv) {

    if (argc != 2) {
        fprintf(stdout, "Specify a single kinetics file to read.\n");
        return EXIT_SUCCESS;
    }
    
    // TODO: Verify file extension?
    // TODO: Make sure long is big enough for file offsets, if not use size_t
    
    kFile file; 
    char *method_prefix = "TContinuumStore";
    long method_start = 0; 
    dSet** sets = NULL;
    int sets_count = 0;
    
    // read file into a buffer
    read_file(&file, argv[1]);

    while (1) {
        method_start = buf_find(file.buffer, file.size, method_prefix, 15, method_start);

        if (method_start == -1)
            // we're at the end of the file
            break;
        
        sets = (dSet**)realloc(sets, sizeof(dSet*) * (sets_count + 1));
        
        if (sets == NULL) {
            fprintf(stderr, "Unable to allocate memory for data set %d\n.", sets_count);
            exit(EXIT_FAILURE);
        }
        
        sets[sets_count] = extract_method_data(&file, method_start);
        //display_set(sets[sets_count]);

        sets_count++;
    }

    //printf("--------------------------------------------\n");
    
    // output as JSON string
    char* json = json_serialize(sets, sets_count);
    fprintf(stdout, "%s\n", json);
    free(json);
    
    free_sets(sets, sets_count);

    free(file.buffer);

    //printf("Done.\n");
    //printf("Extracted data for %d methods.\n", sets_count);
    
    return EXIT_SUCCESS;
}
void slayer_server_log_add_error(slayer_server_log_manager_t *manager, apr_pool_t *mpool,
                                  const char *client_ip,apr_int64_t rtime,
                                  const char *request_line, const char *error_msg ) {

	json_value *container = json_object_create(mpool);
	json_object_add(container,"client_ip",json_string_create(mpool,client_ip));
	json_object_add(container,"request_time",json_long_create(mpool,rtime / (1000*1000)));
	json_object_add(container,"request",json_string_create(mpool,request_line));
	json_object_add(container,"error",json_string_create(mpool,error_msg));
	char *json_entry = strdup(json_serialize(mpool,container)); //we want our own copy of this data
	//smallest chunk in the mutex
	apr_thread_mutex_lock(manager->list_mutex);
	manager->offset++;
	if (manager->offset == manager->nentries)  manager->offset = 0;
	free(manager->entries[manager->offset].json_view);
	manager->entries[manager->offset].json_view = json_entry;
	apr_thread_mutex_unlock(manager->list_mutex);
}
Example #7
0
void json_print_response(uint8_t status)
{
#ifdef __SILENCE_JSON_RESPONSES
	return;
#endif

	if (js.json_verbosity == JV_SILENT) {				    // silent means no responses
        return;
    }
	if (js.json_verbosity == JV_EXCEPTIONS)	{				// cutout for JV_EXCEPTIONS mode
		if (status == STAT_OK) {
			if (cm.machine_state != MACHINE_INITIALIZING) {	// always do full echo during startup
				return;
            }
        }
    }

	// Body processing
	nvObj_t *nv = nv_body;
	if (status == STAT_JSON_SYNTAX_ERROR) {
		nv_reset_nv_list();
		nv_add_string((const char *)"err", escape_string(cs.bufp, cs.saved_buf));

	} else if (cm.machine_state != MACHINE_INITIALIZING) {	// always do full echo during startup
		uint8_t nv_type;
		do {
			if ((nv_type = nv_get_type(nv)) == NV_TYPE_NULL) break;

			if (nv_type == NV_TYPE_GCODE) {
				if (js.echo_json_gcode_block == false) {	// kill command echo if not enabled
					nv->valuetype = TYPE_EMPTY;
				}

//++++		} else if (nv_type == NV_TYPE_CONFIG) {			// kill config echo if not enabled
//fix me		if (js.echo_json_configs == false) {
//					nv->valuetype = TYPE_EMPTY;
//				}

			} else if (nv_type == NV_TYPE_MESSAGE) {		// kill message echo if not enabled
				if (js.echo_json_messages == false) {
					nv->valuetype = TYPE_EMPTY;
				}

			} else if (nv_type == NV_TYPE_LINENUM) {		// kill line number echo if not enabled
				if ((js.echo_json_linenum == false) || (fp_ZERO(nv->value))) { // do not report line# 0
					nv->valuetype = TYPE_EMPTY;
				}
			}
		} while ((nv = nv->nx) != NULL);
	}

	// Footer processing
	while(nv->valuetype != TYPE_EMPTY) {					// find a free nvObj at end of the list...
		if ((nv = nv->nx) == NULL) {						// oops! No free nvObj!
			rpt_exception(STAT_JSON_TOO_LONG, "json_print"); // report this as an exception
			return;
		}
	}
	char footer_string[NV_FOOTER_LEN];

    // in xio.cpp:xio.readline the CR||LF read from the host is not appended to the string.
    // to ensure that the correct number of bytes are reported back to the host we add a +1 to
    // cs.linelen so that the number of bytes received matches the number of bytes reported
    sprintf((char *)footer_string, "%d,%d,%d", 1, status, cs.linelen + 1);
    cs.linelen = 0;										    // reset linelen so it's only reported once

//	if (xio.enable_window_mode) {							// 2 footer styles are supported...
//		sprintf((char *)footer_string, "%d,%d,%d", 2, status, xio_get_window_slots());	//...windowing
//	} else {
//		sprintf((char *)footer_string, "%d,%d,%d", 1, status, cs.linelen);				//...streaming
//		cs.linelen = 0;										// reset linelen so it's only reported once
//	}

	nv_copy_string(nv, footer_string);						// link string to nv object
	nv->depth = 0;											// footer 'f' is a peer to response 'r' (hard wired to 0)
	nv->valuetype = TYPE_ARRAY;								// declare it as an array
	strcpy(nv->token, "f");									// set it to Footer
	nv->nx = NULL;											// terminate the list

	// serialize the JSON response and print it if there were no errors
	if (json_serialize(nv_header, cs.out_buf, sizeof(cs.out_buf)) >= 0) {
		fprintf(stderr, "%s", cs.out_buf);
	}
}
Example #8
0
int lwm2m_data_serialize(lwm2m_uri_t * uriP,
                         int size,
                         lwm2m_data_t * dataP,
                         lwm2m_media_type_t * formatP,
                         uint8_t ** bufferP)
{
    LOG_URI(uriP);
    LOG_ARG("size: %d, formatP: %s", size, STR_MEDIA_TYPE(*formatP));

    // Check format
    if (*formatP == LWM2M_CONTENT_TEXT
     || *formatP == LWM2M_CONTENT_OPAQUE)
    {
        if (size != 1
         || (uriP != NULL && !LWM2M_URI_IS_SET_RESOURCE(uriP))
         || dataP->type == LWM2M_TYPE_OBJECT
         || dataP->type == LWM2M_TYPE_OBJECT_INSTANCE
         || dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE)
        {
#ifdef LWM2M_SUPPORT_JSON
            *formatP = LWM2M_CONTENT_JSON;
#else
            *formatP = LWM2M_CONTENT_TLV;
#endif
        }
    }

    if (*formatP == LWM2M_CONTENT_OPAQUE
     && dataP->type != LWM2M_TYPE_OPAQUE)
    {
        LOG("Opaque format is reserved to opaque resources.");
        return -1;
    }

    LOG_ARG("Final format: %s", STR_MEDIA_TYPE(*formatP));

    switch (*formatP)
    {
    case LWM2M_CONTENT_TEXT:
        return prv_textSerialize(dataP, bufferP);

    case LWM2M_CONTENT_OPAQUE:
        *bufferP = (uint8_t *)lwm2m_malloc(dataP->value.asBuffer.length);
        if (*bufferP == NULL) return -1;
        memcpy(*bufferP, dataP->value.asBuffer.buffer, dataP->value.asBuffer.length);
        return (int)dataP->value.asBuffer.length;

    case LWM2M_CONTENT_TLV:
    case LWM2M_CONTENT_TLV_OLD:
    {
            bool isResourceInstance;

            if (uriP != NULL && LWM2M_URI_IS_SET_RESOURCE(uriP)
             && (size != 1 || dataP->id != uriP->resourceId))
            {
                isResourceInstance = true;
            }
            else
            {
                isResourceInstance = false;
            }
            return tlv_serialize(isResourceInstance, size, dataP, bufferP);
        }

#ifdef LWM2M_CLIENT_MODE
    case LWM2M_CONTENT_LINK:
        return discover_serialize(NULL, uriP, NULL, size, dataP, bufferP);
#endif
#ifdef LWM2M_SUPPORT_JSON
    case LWM2M_CONTENT_JSON:
    case LWM2M_CONTENT_JSON_OLD:
        return json_serialize(uriP, size, dataP, bufferP);
#endif

    default:
        return -1;
    }
}