Example #1
0
    int parse(const std::string& str, uint32_t& line, uint32_t& column) {
        if (err_code_ != 0) {
            return err_code_;
        }

        uint32_t processed = 0;
        err_code_ = json_parser_string(&parser_,
                                       str.c_str(),
                                       static_cast<uint32_t>(str.size()),
                                       &processed);
        if (err_code_ || !json_parser_is_done(&parser_)) {
            line = 1;
            column = 1;
            for (uint32_t i = 0; i < processed; ++i) {
                if (str[i] == '\n') {
                    ++line;
                    column = 1;
                } else {
                    ++column;
                }
            }
            err_code_ = err_code_ ? err_code_ : JSON_ERROR_UTF8 + 1;
        }

        return err_code_;
    }
Example #2
0
static int do_parse(json_config *config, const char *filename)
{
    FILE *input;
    json_parser parser;
    int ret;
    int col, lines;

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

    /* initialize the parser structure. we don't need a callback in verify */
    ret = json_parser_init(&parser, config, NULL, NULL);
    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;
    }

    close_filename(filename, input);
    return 0;
}
Example #3
0
    int parse(std::istream& stream, uint32_t &line, uint32_t &column) {
        if (err_code_ != 0) {
            return err_code_;
        }

        char buffer[4096];

        line = 1;
        column = 1;
        for ( ; ; ) {
            stream.read(buffer, sizeof(buffer));
            std::streamsize read = stream.gcount();
            if (read <= 0) {
                break;
            }

            uint32_t processed = 0;
            err_code_ = json_parser_string(&parser_,
                                           buffer,
                                           static_cast<uint32_t>(read),
                                           &processed);

            // check for early return
            if (!err_code_ && json_parser_is_done(&parser_)) {
                return err_code_;
            }

            for (uint32_t i = 0; i < processed; ++i) {
                if (buffer[i] == '\n') {
                    ++line;
                    column = 1;
                } else {
                    ++column;
                }
            }

            if (err_code_) {
                break;
            }
        }

        if (err_code_ || !json_parser_is_done(&parser_)) {
            err_code_ = err_code_ ? err_code_ : JSON_ERROR_UTF8 + 1;
        }

        return err_code_;
    }
int
jsonrpc_recv(struct jsonrpc *rpc, struct jsonrpc_msg **msgp)
{
    *msgp = NULL;
    if (rpc->status) {
        return rpc->status;
    }

    while (!rpc->received) {
        if (byteq_is_empty(&rpc->input)) {
            size_t chunk;
            int retval;

            chunk = byteq_headroom(&rpc->input);
            retval = stream_recv(rpc->stream, byteq_head(&rpc->input), chunk);
            if (retval < 0) {
                if (retval == -EAGAIN) {
                    return EAGAIN;
                } else {
                    VLOG_WARN_RL(&rl, "%s: receive error: %s",
                                 rpc->name, strerror(-retval));
                    jsonrpc_error(rpc, -retval);
                    return rpc->status;
                }
            } else if (retval == 0) {
                jsonrpc_error(rpc, EOF);
                return EOF;
            }
            byteq_advance_head(&rpc->input, retval);
        } else {
            size_t n, used;

            if (!rpc->parser) {
                rpc->parser = json_parser_create(0);
            }
            n = byteq_tailroom(&rpc->input);
            used = json_parser_feed(rpc->parser,
                                    (char *) byteq_tail(&rpc->input), n);
            byteq_advance_tail(&rpc->input, used);
            if (json_parser_is_done(rpc->parser)) {
                jsonrpc_received(rpc);
                if (rpc->status) {
                    const struct byteq *q = &rpc->input;
                    if (q->head <= BYTEQ_SIZE) {
                        stream_report_content(q->buffer, q->head,
                                              STREAM_JSONRPC,
                                              THIS_MODULE, rpc->name);
                    }
                    return rpc->status;
                }
            }
        }
    }

    *msgp = rpc->received;
    rpc->received = NULL;
    return 0;
}
Example #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;
}
Example #6
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;
}
Example #7
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;	
}
Example #8
0
/* Attempts to receive a message from 'rpc'.
 *
 * If successful, stores the received message in '*msgp' and returns 0.  The
 * caller takes ownership of '*msgp' and must eventually destroy it with
 * jsonrpc_msg_destroy().
 *
 * Otherwise, stores NULL in '*msgp' and returns one of the following:
 *
 *   - EAGAIN: No message has been received.
 *
 *   - EOF: The remote end closed the connection gracefully.
 *
 *   - Otherwise an errno value that represents a JSON-RPC protocol violation
 *     or another error fatal to the connection.  'rpc' will not send or
 *     receive any more messages.
 */
int
jsonrpc_recv(struct jsonrpc *rpc, struct jsonrpc_msg **msgp)
{
    int i;

    *msgp = NULL;
    if (rpc->status) {
        return rpc->status;
    }

    for (i = 0; i < 50; i++) {
        size_t n, used;

        /* Fill our input buffer if it's empty. */
        if (byteq_is_empty(&rpc->input)) {
            size_t chunk;
            int retval;

            chunk = byteq_headroom(&rpc->input);
            retval = stream_recv(rpc->stream, byteq_head(&rpc->input), chunk);
            if (retval < 0) {
                if (retval == -EAGAIN) {
                    return EAGAIN;
                } else {
                    VLOG_WARN_RL(&rl, "%s: receive error: %s",
                                 rpc->name, ovs_strerror(-retval));
                    jsonrpc_error(rpc, -retval);
                    return rpc->status;
                }
            } else if (retval == 0) {
                jsonrpc_error(rpc, EOF);
                return EOF;
            }
            byteq_advance_head(&rpc->input, retval);
        }

        /* We have some input.  Feed it into the JSON parser. */
        if (!rpc->parser) {
            rpc->parser = json_parser_create(0);
        }
        n = byteq_tailroom(&rpc->input);
        used = json_parser_feed(rpc->parser,
                                (char *) byteq_tail(&rpc->input), n);
        byteq_advance_tail(&rpc->input, used);

        /* If we have complete JSON, attempt to parse it as JSON-RPC. */
        if (json_parser_is_done(rpc->parser)) {
            *msgp = jsonrpc_parse_received_message(rpc);
            if (*msgp) {
                return 0;
            }

            if (rpc->status) {
                const struct byteq *q = &rpc->input;
                if (q->head <= q->size) {
                    stream_report_content(q->buffer, q->head, STREAM_JSONRPC,
                                          THIS_MODULE, rpc->name);
                }
                return rpc->status;
            }
        }
    }

    return EAGAIN;
}