int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { if (!size || data[size - 1]) { return 0; } struct json *j1 = json_from_string((const char *)data); if (j1->type == JSON_STRING) { json_destroy(j1); return 0; } free(json_to_string(j1, JSSF_SORT | JSSF_PRETTY)); struct jsonrpc_msg *msg; char *error = jsonrpc_msg_from_json(j1, &msg); /* Frees 'j1'. */ if (error) { free(error); return 0; } struct json *j2 = jsonrpc_msg_to_json(msg); /* Frees 'msg'. */ if (j2->type == JSON_STRING) { json_destroy(j2); return 0; } free(json_to_string(j2, JSSF_SORT | JSSF_PRETTY)); json_destroy(j2); return 0; }
static char * cell_to_text(struct cell *cell, const struct table_style *style) { if (!cell->text) { if (cell->json) { if (style->cell_format == CF_JSON || !cell->type) { cell->text = json_to_string(cell->json, JSSF_SORT); } else { struct ovsdb_datum datum; struct ovsdb_error *error; struct ds s; error = ovsdb_datum_from_json(&datum, cell->type, cell->json, NULL); if (!error) { ds_init(&s); if (style->cell_format == CF_STRING) { ovsdb_datum_to_string(&datum, cell->type, &s); } else { ovsdb_datum_to_bare(&datum, cell->type, &s); } ovsdb_datum_destroy(&datum, cell->type); cell->text = ds_steal_cstr(&s); } else { cell->text = json_to_string(cell->json, JSSF_SORT); ovsdb_error_destroy(error); } } } else { cell->text = xstrdup(""); } } return cell->text; }
static void print_json(struct json *json) { char *string = json_to_string(json, table_style.json_flags); fputs(string, stdout); free(string); }
/* Schedules 'msg' to be sent on 'rpc' and returns 'rpc''s status (as with * jsonrpc_get_status()). * * If 'msg' cannot be sent immediately, it is appended to a buffer. The caller * is responsible for ensuring that the amount of buffered data is somehow * limited. (jsonrpc_get_backlog() returns the amount of data currently * buffered in 'rpc'.) * * Always takes ownership of 'msg', regardless of success. */ int jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg) { struct ofpbuf *buf; struct json *json; size_t length; char *s; if (rpc->status) { jsonrpc_msg_destroy(msg); return rpc->status; } jsonrpc_log_msg(rpc, "send", msg); json = jsonrpc_msg_to_json(msg); s = json_to_string(json, 0); length = strlen(s); json_destroy(json); buf = xmalloc(sizeof *buf); ofpbuf_use(buf, s, length); buf->size = length; list_push_back(&rpc->output, &buf->list_node); rpc->backlog += length; if (rpc->backlog == length) { jsonrpc_run(rpc); } return rpc->status; }
RAW *forge_raw(const char *raw, json_item *jlist) { RAW *new_raw; char unixtime[16]; struct jsontring *string; json_item *jstruct = NULL; sprintf(unixtime, "%li", time(NULL)); jstruct = json_new_object(); json_set_property_strN(jstruct, "time", 4, unixtime, strlen(unixtime)); json_set_property_strN(jstruct, "raw", 3, raw, strlen(raw)); json_set_property_objN(jstruct, "data", 4, jlist); string = json_to_string(jstruct, NULL, 1); new_raw = xmalloc(sizeof(*new_raw)); new_raw->len = string->len; new_raw->next = NULL; new_raw->priority = RAW_PRI_LO; new_raw->refcount = 0; new_raw->data = string->jstring; free(string); return new_raw; }
inline strbuffer_t * json_array_to_string(json_t* array) { size_t i = 0; json_t* elem; strbuffer_t* sb = strbuffer_new(); strbuffer_t* tempString; strbuffer_append(sb, "[ "); for(i = 0; i < array->size; i++ ) { if(i != 0) { strbuffer_append(sb, ", "); } elem = json_array_get(array, i); tempString = json_to_string(elem); strbuffer_append(sb, tempString->value); strbuffer_destroy(tempString); } strbuffer_append(sb, " ]"); return sb; }
inline strbuffer_t * json_object_to_string(json_t* object){ json_t *key; size_t i = 0; strbuffer_t* sb = strbuffer_new(); strbuffer_t* tempString; strbuffer_append(sb, "{ "); if(object->children != NULL) { for(key = object->children; key != NULL; key = key->next) { if(i++ != 0) { strbuffer_append(sb, ", "); } strbuffer_append(sb, "\"" ); strbuffer_append(sb, key->value ); strbuffer_append(sb, "\" : " ); tempString = json_to_string(key->children); strbuffer_append(sb, tempString->value); strbuffer_destroy(tempString); } } strbuffer_append(sb, " }"); return sb; }
std::string Collector::serve_request(std::string req) { Json::Value root; Json::Reader reader; if (reader.parse(req, root, false) == false) { return generate_error("bad_json"); } if (root["status"] == "have_work") { std::cout << "work: " << root["work"] << std::endl; /* Add the pixels to the pixmap. */ process_work(root["work"], root["pixels"]); Json::Value root; root["success"] = true; return json_to_string(root); } else if (root["status"] == "need_work") { /* We want to reduce network slowdown by only sending the scene when necessary. */ return generate_work(root["have_scene"].asBool()); } else { return generate_error("bad_request"); } }
std::string Collector::generate_error(std::string type) { Json::Value error; error["error"] = true; error["type"] = type; return json_to_string(error); }
const char *json_string_value(const json_t *json) { if(!json_is_string(json)) return NULL; return json_to_string(json)->value; }
u_char * dbrelay_exec_query(dbrelay_connection_t *conn, char *database, char *sql, unsigned long flags) { json_t *json = json_new(); u_char *ret; if (flags & DBRELAY_FLAG_PP) json_pretty_print(json, 1); if (flags & DBRELAY_FLAG_EMBEDCSV) json_set_mode(json, DBRELAY_JSON_MODE_CSV); api->change_db(conn->db, database); if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_BEGIN, NULL)); if (api->exec(conn->db, sql)) { dbrelay_db_fill_data(json, conn); if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_COMMIT, NULL)); } else { if (flags & DBRELAY_FLAG_XACT) api->exec(conn->db, api->catalogsql(DBRELAY_DBCMD_ROLLBACK, NULL)); return NULL; } ret = (u_char *) json_to_string(json); json_free(json); return ret; }
int main(void) { struct json *reply, *item1, *item2, *params; struct jrpc_client *client = &my_client; int ret = jrpc_client_init(client, HOST, PORT); if (ret != 0) { exit(ret); } item1 = json_create_object(); json_add_number_to_object(item1, "A", 3); json_add_number_to_object(item1, "B", 10); item2 = json_create_object(); json_add_number_to_object(item2, "A", 1); json_add_number_to_object(item2, "B", 2); params = json_create_array(); json_add_item_to_object(item1,"S", item2); json_add_item_to_array(params, item1); // jrpc_client_call will free params if ((ret = jrpc_client_call(client, "foo", params, &reply)) != 0) { exit(ret); } printf("%s\n", json_to_string(reply)); json_delete(reply); jrpc_client_close(client); return 0; }
static void print_and_free_json(struct json *json) { char *string = json_to_string(json, JSSF_SORT); json_destroy(json); puts(string); free(string); }
static int tt_ws_write(struct json_rpc_tt *jt, struct json_object *obj) { struct jrpc_ws *jw = (struct jrpc_ws *)jt->impl; char *str = json_to_string(obj); int ret = ws_connection_send_message(jw->conn, (u_char *)str); free(str); return ret; }
json_t *json_true(void) { static json_t the_true = { .type = JSON_TRUE, .refcount = (unsigned int)1 }; return &the_true; } json_t *json_false(void) { static json_t the_false = { .type = JSON_FALSE, .refcount = (unsigned int)1 }; return &the_false; } json_t *json_null(void) { static json_t the_null = { .type = JSON_NULL, .refcount = (unsigned int)1 }; return &the_null; } /*** deletion ***/ void json_delete(json_t *json) { if(json_is_object(json)) json_delete_object(json_to_object(json)); else if(json_is_array(json)) json_delete_array(json_to_array(json)); else if(json_is_string(json)) json_delete_string(json_to_string(json)); else if(json_is_integer(json)) json_delete_integer(json_to_integer(json)); else if(json_is_real(json)) json_delete_real(json_to_real(json)); /* json_delete is not called for true, false or null */ }
static int tt_bufevent_write(struct json_rpc_tt *jt, struct json_object *obj) { struct jrpc_bufevent *jb = (struct jrpc_bufevent *)jt->impl; char *json_str = json_to_string(obj); int ret = bufevent_write(jb->bufev, json_str, strlen(json_str)); if (ret == -1) log_info("%s : bufevent_write_failed", __func__); free(json_str); return ret; }
std::string Collector::generate_work(bool has_scene) { Json::Value root; /* No need to send scene if client already has it. */ if (has_scene == false) { root["scene"] = scene; std::cout << "client needs scene" << std::endl; } /* Return work from the queue, but don't remove it just yet. */ root["work"] = queue.get().to_json_value(); return json_to_string(root); }
static void check_txn(int error, struct jsonrpc_msg **reply_) { struct jsonrpc_msg *reply = *reply_; if (error) { ovs_fatal(error, "transaction failed"); } if (reply->error) { ovs_fatal(error, "transaction returned error: %s", json_to_string(reply->error, table_style.json_flags)); } }
int json_string_set_nocheck(json_t *json, const char *value) { char *dup; json_string_t *string; dup = strdup(value); if(!dup) return -1; string = json_to_string(json); free(string->value); string->value = dup; return 0; }
struct svalue * json_to_value(json_object *ob) { enum json_type type = json_object_get_type(ob); switch (type) { case json_type_boolean: return json_to_boolean(ob); case json_type_double: return json_to_float(ob); case json_type_int: return json_to_int(ob); case json_type_string: return json_to_string(ob); case json_type_object: return json_to_mapping(ob); case json_type_array: return json_to_array(ob); case json_type_null: return json_to_null(ob); } return NULL; }
static int send_reply(struct evhttp_request *req, struct json_object *obj) { char *result_reason = NULL; int result_status; if (get_status_and_reason(obj, &result_status, &result_reason) == -1) send_http_reply(req, HTTP_INTERNAL_ERROR_STATUS, HTTP_INTERNAL_ERROR_REASON, JSON_RPC_INTERNAL_ERROR); else { char *obj_str = json_to_string(obj); send_http_reply(req, result_status, result_reason, obj_str); free(obj_str); } free(result_reason); return 0; }
static void table_print_json__(const struct table *table, const struct table_style *style) { struct json *json, *headings, *data; size_t x, y; char *s; json = json_object_create(); if (table->caption) { json_object_put_string(json, "caption", table->caption); } if (table->timestamp) { char *s = table_format_timestamp__(); json_object_put_string(json, "time", s); free(s); } headings = json_array_create_empty(); for (x = 0; x < table->n_columns; x++) { const struct column *column = &table->columns[x]; json_array_add(headings, json_string_create(column->heading)); } json_object_put(json, "headings", headings); data = json_array_create_empty(); for (y = 0; y < table->n_rows; y++) { struct json *row = json_array_create_empty(); for (x = 0; x < table->n_columns; x++) { const struct cell *cell = table_cell__(table, y, x); if (cell->text) { json_array_add(row, json_string_create(cell->text)); } else if (cell->json) { json_array_add(row, json_clone(cell->json)); } else { json_array_add(row, json_null_create()); } } json_array_add(data, row); } json_object_put(json, "data", data); s = json_to_string(json, style->json_flags); json_destroy(json); puts(s); free(s); }
int main() { int i = 0; int rc; while (i++ < 1) { json_t *jso = json_new(); json_add_string(jso, "string1", "string1_val"); json_add_string(jso, "string2", "string2_val"); json_add_int(jso, "integer1", 12345); printf("%s\n", json_get_string(jso, "string1")); printf("%s\n%d\n", json_get_string(jso, "string2"), json_get_int(jso, "integer1")); json_padd_string(jso, "sub-object/sub-string", "sub-string-val"); json_padd_string(jso, "object1/sub-object/sub-string", "sub-string-val"); char *path = "object1/sub-object1/sub-sub-object1//sub-sub-string1"; json_padd_string(jso, path, "sub-sub-string1-val "); printf("sub-sub-string1=[%s]\n", json_pget_string(jso, path)); json_add_string(jso, "string3", "string3_val"); json_padd_string(jso, "string4", "string4_val"); // LOG("\n%s", json_to_string(jso)); array_t *arr = json_new_array( jso, "array0" ); rc = json_array_add_string( arr, 0, "array-item", "array-item0-val" ); rc = json_array_add_int( arr, 0, "array-item-int", 1234 ); rc = json_array_add_string( arr, 1, "array-item", "array-item1-val" ); rc = json_array_add_int( arr, 1, "array-item-int", 2234 ); rc = json_array_add_string( arr, 2, "array-item", "array-item2-val" ); rc = json_array_add_int( arr, 2, "array-item-int", 3234 ); /* LOG( "rc = [%d][%s][%s][%s][%d]", rc, json_array_get_string( arr, 0, "array-item"), json_array_get_string( arr, 1, "array-item" ), json_array_get_string( arr, 2, "array-item" ), json_array_get_int( arr, 2, "array-item-int") ); */ //printf( "====[%d]===[%s]-=-----\n", json_array_getlen(arr),json_array_get_string( arr, 0, "array-item" ) ); json_t *tmp1 = json_array_get_item( arr, 1 ); //printf( "-----------\n%s\n--------\n", json_to_string(tmp1) ); //printf("\n%s\n", json_to_string(jso)); LOG( "\n%s\n", json_to_string(jso) ); json_free(&jso); } //system("pause"); }
int json_string_set(const json_t *json, const char *value) { char *dup; json_string_t *string; if(!json_is_string(json) || !value || !utf8_check_string(value, -1)) return -1; dup = strdup(value); if(!dup) return -1; string = json_to_string(json); free(string->value); string->value = dup; return 0; }
struct ovsdb_error * ovsdb_syntax_error(const struct json *json, const char *tag, const char *details, ...) { struct ovsdb_error *error; va_list args; va_start(args, details); error = ovsdb_error_valist(tag ? tag : "syntax error", details, args); va_end(args); if (json) { /* XXX this is much too much information in some cases */ error->syntax = json_to_string(json, JSSF_SORT); } return error; }
void json_delete(json_t *json) { if(json_is_object(json)) json_delete_object(json_to_object(json)); else if(json_is_array(json)) json_delete_array(json_to_array(json)); else if(json_is_string(json)) json_delete_string(json_to_string(json)); else if(json_is_integer(json)) json_delete_integer(json_to_integer(json)); else if(json_is_real(json)) json_delete_real(json_to_real(json)); /* json_delete is not called for true, false or null */ }
/* Schedules 'msg' to be sent on 'rpc' and returns 'rpc''s status (as with * jsonrpc_get_status()). * * If 'msg' cannot be sent immediately, it is appended to a buffer. The caller * is responsible for ensuring that the amount of buffered data is somehow * limited. (jsonrpc_get_backlog() returns the amount of data currently * buffered in 'rpc'.) * * Always takes ownership of 'msg', regardless of success. */ int jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg) { struct ofpbuf *buf; struct json *json; size_t length; char *s; if (rpc->status) { jsonrpc_msg_destroy(msg); return rpc->status; } jsonrpc_log_msg(rpc, "send", msg); json = jsonrpc_msg_to_json(msg); s = json_to_string(json, 0); length = strlen(s); json_destroy(json); buf = xmalloc(sizeof *buf); ofpbuf_use(buf, s, length); buf->size = length; list_push_back(&rpc->output, &buf->list_node); rpc->output_count++; rpc->backlog += length; if (rpc->output_count >= 50) { VLOG_INFO_RL(&rl, "excessive sending backlog, jsonrpc: %s, num of" " msgs: %"PRIuSIZE", backlog: %"PRIuSIZE".", rpc->name, rpc->output_count, rpc->backlog); } if (rpc->backlog == length) { jsonrpc_run(rpc); } return rpc->status; }
int jrpc_client_call(struct jrpc_client *client, const char *method, struct json *params, struct json **response) { int fd, max_read_size; size_t bytes_read = 0; char *new_buffer, *end_ptr = NULL; struct jrpc_connection *conn; struct json *root, *request; request = json_create_object(); json_add_string_to_object(request, "method", method); json_add_item_to_object(request, "params", params); json_add_number_to_object(request, "id", client->id); send_request(&client->conn, json_to_string(request)); json_delete(request); // read conn = &client->conn; fd = conn->sock.fd; for (;;) { if (conn->pos == (conn->buffer_size - 1)) { conn->buffer_size *= 2; new_buffer = realloc(conn->buffer, conn->buffer_size); if (new_buffer == NULL) { perror("Memory error"); return -ENOMEM; } conn->buffer = new_buffer; memset(conn->buffer + conn->pos, 0, conn->buffer_size - conn->pos); } // can not fill the entire buffer, string must be NULL terminated max_read_size = conn->buffer_size - conn->pos - 1; if ((bytes_read = read(fd, conn->buffer + conn->pos, max_read_size)) == -1) { elog("read %d\n", strerror(errno)); return -EIO; } if (!bytes_read) { // client closed the sending half of the connection if (client->conn.debug_level) dlog("Client closed connection.\n"); return -EIO; } conn->pos += bytes_read; if ((root = json_parse_stream(conn->buffer, &end_ptr)) != NULL) { if (client->conn.debug_level > 1) { dlog("Valid JSON Received:\n%s\n", json_to_string(root)); } if (root->type == JSON_T_OBJECT) { struct json *id = json_get_object_item(root, "id"); if (id->type == JSON_T_STRING) { if (client->id != atoi(id->string)) goto out; } else if (id->type == JSON_T_NUMBER) { if (client->id != id->valueint) goto out; } client->id++; //shift processed request, discarding it memmove(conn->buffer, end_ptr, strlen(end_ptr) + 2); conn->pos = strlen(end_ptr); memset(conn->buffer + conn->pos, 0, conn->buffer_size - conn->pos - 1); *response = json_detach_item_from_object(root, "result"); if (*response == NULL) goto out; json_delete(root); return 0; } out: elog("INVALID JSON Received:\n---\n%s\n---\n", conn->buffer); json_delete(root); return -EINVAL; } else if (end_ptr != (conn->buffer + conn->pos)) { // did we parse the all buffer? If so, just wait for more. // else there was an error before the buffer's end if (client->conn.debug_level) { elog("INVALID JSON Received:\n---\n%s\n---\n", conn->buffer); } send_error(conn, JRPC_PARSE_ERROR, strdup("Parse error. Invalid JSON" " was received by the client."), NULL); return -EINVAL; } } }
static void connection_cb(struct uloop_fd *sock, unsigned int events) { struct json *root; int fd, max_read_size; struct jrpc_connection *conn; struct jrpc_server *server; ssize_t bytes_read = 0; char *new_buffer, *end_ptr = NULL; conn = container_of(sock, struct jrpc_connection, sock); fd = sock->fd; server = conn->server; if (conn->pos == (conn->buffer_size - 1)) { conn->buffer_size *= 2; new_buffer = realloc(conn->buffer, conn->buffer_size); if (new_buffer == NULL) { elog("Memory error %s\n", strerror(errno)); return close_connection(sock); } conn->buffer = new_buffer; memset(conn->buffer + conn->pos, 0, conn->buffer_size - conn->pos); } // can not fill the entire buffer, string must be NULL terminated max_read_size = conn->buffer_size - conn->pos - 1; do { bytes_read = read(fd, conn->buffer + conn->pos, max_read_size); if (bytes_read < 0) { if (errno == EINVAL) continue; if (errno == EAGAIN) break; // error return close_connection(sock); } if (bytes_read == 0) { // client closed the sending half of the connection if (server->debug_level) dlog("Client closed connection.\n"); return close_connection(sock); } break; } while (1); conn->pos += bytes_read; if ((root = json_parse_stream(conn->buffer, &end_ptr)) != NULL) { if (server->debug_level > 1) { dlog("Valid JSON Received:\n%s\n", json_to_string(root)); } if (root->type == JSON_T_OBJECT) { eval_request(server, conn, root); } //shift processed request, discarding it memmove(conn->buffer, end_ptr, strlen(end_ptr) + 2); conn->pos = strlen(end_ptr); memset(conn->buffer + conn->pos, 0, conn->buffer_size - conn->pos - 1); json_delete(root); } else { // did we parse the all buffer? If so, just wait for more. // else there was an error before the buffer's end if (end_ptr != (conn->buffer + conn->pos)) { if (server->debug_level) { dlog("INVALID JSON Received:\n---\n%s\n---\n", conn->buffer); } send_error(conn, JRPC_PARSE_ERROR, strdup("Parse error. Invalid JSON" " was received by the server."), NULL); return close_connection(sock); } } }
json_t *json_true(void) { static json_t the_true = { .type = JSON_TRUE, .refcount = (unsigned int)1 }; return &the_true; } json_t *json_false(void) { static json_t the_false = { .type = JSON_FALSE, .refcount = (unsigned int)1 }; return &the_false; } json_t *json_null(void) { static json_t the_null = { .type = JSON_NULL, .refcount = (unsigned int)1 }; return &the_null; } /*** deletion ***/ void json_delete(json_t *json) { if(json_is_object(json)) json_delete_object(json_to_object(json)); else if(json_is_array(json)) json_delete_array(json_to_array(json)); else if(json_is_string(json)) json_delete_string(json_to_string(json)); else if(json_is_integer(json)) json_delete_integer(json_to_integer(json)); else if(json_is_real(json)) json_delete_real(json_to_real(json)); /* json_delete is not called for true, false or null */ } /*** equality ***/ int json_equal(json_t *json1, json_t *json2) { if(!json1 || !json2) return 0; if(json_typeof(json1) != json_typeof(json2)) return 0; /* this covers true, false and null as they are singletons */ if(json1 == json2) return 1; if(json_is_object(json1)) return json_object_equal(json1, json2); if(json_is_array(json1)) return json_array_equal(json1, json2); if(json_is_string(json1)) return json_string_equal(json1, json2); if(json_is_integer(json1)) return json_integer_equal(json1, json2); if(json_is_real(json1)) return json_real_equal(json1, json2); return 0; } /*** copying ***/ json_t *json_copy(json_t *json) { if(!json) return NULL; if(json_is_object(json)) return json_object_copy(json); if(json_is_array(json)) return json_array_copy(json); if(json_is_string(json)) return json_string_copy(json); if(json_is_integer(json)) return json_integer_copy(json); if(json_is_real(json)) return json_real_copy(json); if(json_is_true(json) || json_is_false(json) || json_is_null(json)) return json; return NULL; } json_t *json_deep_copy(json_t *json) { if(!json) return NULL; if(json_is_object(json)) return json_object_deep_copy(json); if(json_is_array(json)) return json_array_deep_copy(json); /* for the rest of the types, deep copying doesn't differ from shallow copying */ if(json_is_string(json)) return json_string_copy(json); if(json_is_integer(json)) return json_integer_copy(json); if(json_is_real(json)) return json_real_copy(json); if(json_is_true(json) || json_is_false(json) || json_is_null(json)) return json; return NULL; }