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;
    }
Ejemplo n.º 2
0
static int
print_clfjson(void *ctx, str_t *str, logmeta_t *meta) {
    json_printer jp;
    str_clear(str);
    json_print_init(&jp, jp_callback, str); 

    char timestamp[50];
    memset(timestamp, 0, sizeof(timestamp));
    strftime(timestamp, sizeof(timestamp), "%d/%b/%Y:%H:%M:%S %z", &meta->utc_timestamp);

    json_print_raw(&jp, JSON_ARRAY_BEGIN, NULL, 0);    
        print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_C_IP));
        print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_CS_IDENT));
        print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_CS_USERNAME));
        json_print_raw(&jp, JSON_STRING, timestamp, strlen(timestamp));
        print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_CS_METHOD));
        print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_CS_URI_STEM));
        print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_SC_STATUS));
        print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_BYTES));
        if( ! logmeta_field_isempty(meta, LOGPIPE_CS_REFERER) ) {
            print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_CS_REFERER));
            if( ! logmeta_field_isempty(meta, LOGPIPE_CS_USER_AGENT) ) {
                print_strraw_or_null(&jp, logmeta_field(meta, LOGPIPE_CS_USER_AGENT));
            }
        }
    json_print_raw(&jp, JSON_ARRAY_END, NULL, 0);    
    json_print_free(&jp);
    return 1;
}
 virtual StringResultBase* Prettify(const ParseResultBase* parseResult) const {
     const VinenthzParseResult* pr = static_cast<const VinenthzParseResult*>(parseResult);
     VinenthzStringResult* sr = new VinenthzStringResult();
     json_printer printer;
     json_print_init(&printer, string_buffer_append, &sr->sb);
     tree_print(&printer, json_print_pretty, pr->root);
     json_print_free(&printer);
     sr->AppendEnds();
     return sr;
 }
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
0
 ~writer_context() {
     json_print_free(&printer_);
 }