struct json * jsonrpc_msg_to_json(struct jsonrpc_msg *m) { struct json *json = json_object_create(); if (m->method) { json_object_put(json, "method", json_string_create_nocopy(m->method)); } if (m->params) { json_object_put(json, "params", m->params); } if (m->result) { json_object_put(json, "result", m->result); } else if (m->type == JSONRPC_ERROR) { json_object_put(json, "result", json_null_create()); } if (m->error) { json_object_put(json, "error", m->error); } else if (m->type == JSONRPC_REPLY) { json_object_put(json, "error", json_null_create()); } if (m->id) { json_object_put(json, "id", m->id); } else if (m->type == JSONRPC_NOTIFY) { json_object_put(json, "id", json_null_create()); } free(m); return json; }
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); }
static int handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done) { if (msg->type == JSONRPC_REQUEST) { struct jsonrpc_msg *reply = NULL; if (!strcmp(msg->method, "echo")) { reply = jsonrpc_create_reply(json_clone(msg->params), msg->id); } else { struct json *error = json_object_create(); json_object_put_string(error, "error", "unknown method"); reply = jsonrpc_create_error(error, msg->id); ovs_error(0, "unknown request %s", msg->method); } jsonrpc_send(rpc, reply); return 0; } else if (msg->type == JSONRPC_NOTIFY) { if (!strcmp(msg->method, "shutdown")) { *done = true; return 0; } else { ovs_error(0, "unknown notification %s", msg->method); return ENOTTY; } } else { ovs_error(0, "unsolicited JSON-RPC reply or error"); return EPROTO; } }
void osd_group_add_photostream(json_object_t g, int id) { json_object_t a = json_object_get(g, "photostreams"); json_object_t d = json_object_create(); json_object_setnum(d, "id", id); json_array_push(a, d); json_unref(d); }
json_object_t new_osd_group(const char* name, const char* description) { json_object_t object = json_object_create(); json_object_setstr(object, "name", name); json_object_setstr(object, "description", description); json_object_set(object, "datastreams", json_array_create()); json_object_set(object, "photostreams", json_array_create()); return object; }
json_object_t new_osd_photostream(const char* name, const char* description, double timezone, double latitude, double longitude) { json_object_t object = json_object_create(); json_object_setstr(object, "name", name); json_object_setstr(object, "description", description); json_object_setnum(object, "timezone", timezone); json_object_setnum(object, "latitude", latitude); json_object_setnum(object, "longitude", longitude); return object; }
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); }
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); }
struct json * ovsdb_type_to_json(const struct ovsdb_type *type) { if (ovsdb_type_is_scalar(type) && !ovsdb_base_type_has_constraints(&type->key)) { return ovsdb_base_type_to_json(&type->key); } else { struct json *json = json_object_create(); json_object_put(json, "key", ovsdb_base_type_to_json(&type->key)); if (type->value.type != OVSDB_TYPE_VOID) { json_object_put(json, "value", ovsdb_base_type_to_json(&type->value)); } if (type->n_min != 1) { json_object_put(json, "min", json_integer_create(type->n_min)); } if (type->n_max == UINT_MAX) { json_object_put_string(json, "max", "unlimited"); } else if (type->n_max != 1) { json_object_put(json, "max", json_integer_create(type->n_max)); } return json; } }
struct json * ovsdb_execute(struct ovsdb *db, const struct ovsdb_session *session, const struct json *params, long long int elapsed_msec, long long int *timeout_msec) { struct ovsdb_execution x; struct ovsdb_error *error; struct json *results; size_t n_operations; size_t i; if (params->type != JSON_ARRAY || !params->u.array.n || params->u.array.elems[0]->type != JSON_STRING || strcmp(params->u.array.elems[0]->u.string, db->schema->name)) { if (params->type != JSON_ARRAY) { error = ovsdb_syntax_error(params, NULL, "array expected"); } else { error = ovsdb_syntax_error(params, NULL, "database name expected " "as first parameter"); } results = ovsdb_error_to_json(error); ovsdb_error_destroy(error); return results; } x.db = db; x.session = session; x.txn = ovsdb_txn_create(db); x.symtab = ovsdb_symbol_table_create(); x.durable = false; x.elapsed_msec = elapsed_msec; x.timeout_msec = LLONG_MAX; results = NULL; results = json_array_create_empty(); n_operations = params->u.array.n - 1; error = NULL; for (i = 1; i <= n_operations; i++) { struct json *operation = params->u.array.elems[i]; struct ovsdb_error *parse_error; struct ovsdb_parser parser; struct json *result; const struct json *op; /* Parse and execute operation. */ ovsdb_parser_init(&parser, operation, "ovsdb operation %"PRIuSIZE" of %"PRIuSIZE, i, n_operations); op = ovsdb_parser_member(&parser, "op", OP_ID); result = json_object_create(); if (op) { const char *op_name = json_string(op); ovsdb_operation_executor *executor = lookup_executor(op_name); if (executor) { error = executor(&x, &parser, result); } else { ovsdb_parser_raise_error(&parser, "No operation \"%s\"", op_name); } } else { ovs_assert(ovsdb_parser_has_error(&parser)); } /* A parse error overrides any other error. * An error overrides any other result. */ parse_error = ovsdb_parser_finish(&parser); if (parse_error) { ovsdb_error_destroy(error); error = parse_error; } if (error) { json_destroy(result); result = ovsdb_error_to_json(error); } if (error && !strcmp(ovsdb_error_get_tag(error), "not supported") && timeout_msec) { ovsdb_txn_abort(x.txn); *timeout_msec = x.timeout_msec; json_destroy(result); json_destroy(results); results = NULL; goto exit; } /* Add result to array. */ json_array_add(results, result); if (error) { break; } } if (!error) { error = ovsdb_txn_commit(x.txn, x.durable); if (error) { json_array_add(results, ovsdb_error_to_json(error)); } } else { ovsdb_txn_abort(x.txn); } while (json_array(results)->n < n_operations) { json_array_add(results, json_null_create()); } exit: ovsdb_error_destroy(error); ovsdb_symbol_table_destroy(x.symtab); return results; }
struct json * ovsdb_base_type_to_json(const struct ovsdb_base_type *base) { struct json *json; if (!ovsdb_base_type_has_constraints(base)) { return json_string_create(ovsdb_atomic_type_to_string(base->type)); } json = json_object_create(); json_object_put_string(json, "type", ovsdb_atomic_type_to_string(base->type)); if (base->enum_) { const struct ovsdb_type *type; type = ovsdb_base_type_get_enum_type(base->type); json_object_put(json, "enum", ovsdb_datum_to_json(base->enum_, type)); } switch (base->type) { case OVSDB_TYPE_VOID: NOT_REACHED(); case OVSDB_TYPE_INTEGER: if (base->u.integer.min != INT64_MIN) { json_object_put(json, "minInteger", json_integer_create(base->u.integer.min)); } if (base->u.integer.max != INT64_MAX) { json_object_put(json, "maxInteger", json_integer_create(base->u.integer.max)); } break; case OVSDB_TYPE_REAL: if (base->u.real.min != -DBL_MAX) { json_object_put(json, "minReal", json_real_create(base->u.real.min)); } if (base->u.real.max != DBL_MAX) { json_object_put(json, "maxReal", json_real_create(base->u.real.max)); } break; case OVSDB_TYPE_BOOLEAN: break; case OVSDB_TYPE_STRING: if (base->u.string.minLen != 0) { json_object_put(json, "minLength", json_integer_create(base->u.string.minLen)); } if (base->u.string.maxLen != UINT_MAX) { json_object_put(json, "maxLength", json_integer_create(base->u.string.maxLen)); } break; case OVSDB_TYPE_UUID: if (base->u.uuid.refTableName) { json_object_put_string(json, "refTable", base->u.uuid.refTableName); if (base->u.uuid.refType == OVSDB_REF_WEAK) { json_object_put_string(json, "refType", "weak"); } } break; case OVSDB_N_TYPES: NOT_REACHED(); default: NOT_REACHED(); } return json; }