static void run_tests() { struct my_sink s; json_t *json; const char str[] = "[\"A\", {\"B\": \"C\", \"e\": false}, 1, null, \"foo\"]"; char *dumped_to_string; json = json_loads(str, 0, NULL); if(!json) { fail("json_loads failed"); } dumped_to_string = json_dumps(json, 0); if (!dumped_to_string) { json_decref(json); fail("json_dumps failed"); } s.off = 0; s.cap = strlen(dumped_to_string); s.buf = malloc(s.cap); if (!s.buf) { json_decref(json); free(dumped_to_string); fail("malloc failed"); } if (json_dump_callback(json, my_writer, &s, 0) == -1) { json_decref(json); free(dumped_to_string); free(s.buf); fail("json_dump_callback failed on an exact-length sink buffer"); } if (strncmp(dumped_to_string, s.buf, s.off) != 0) { json_decref(json); free(dumped_to_string); free(s.buf); fail("json_dump_callback and json_dumps did not produce identical output"); } s.off = 1; if (json_dump_callback(json, my_writer, &s, 0) != -1) { json_decref(json); free(dumped_to_string); free(s.buf); fail("json_dump_callback succeeded on a short buffer when it should have failed"); } json_decref(json); free(dumped_to_string); free(s.buf); }
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer) { if (file_ctx->sensor_name) { json_object_set_new(js, "host", json_string(file_ctx->sensor_name)); } if (file_ctx->prefix) { MemBufferWriteRaw((*buffer), file_ctx->prefix, file_ctx->prefix_len); } OutputJSONMemBufferWrapper wrapper = { .buffer = buffer, .expand_by = OUTPUT_BUFFER_SIZE }; int r = json_dump_callback(js, OutputJSONMemBufferCallback, &wrapper, JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII| JSON_ESCAPE_SLASH); if (r != 0) return TM_ECODE_OK; LogFileWrite(file_ctx, *buffer); return 0; }
int cometd_dump_message(void * output, cometd_message * message, json_dump_callback_t callback) { json_t * root = json_object(); json_object_set_new(root, CHANNEL_FIELD, json_string_nocheck(message->channel)); if (message->id) json_object_set_new(root, ID_FIELD, json_string_nocheck(message->id)); if (message->clientId) json_object_set_new(root, CLIENT_ID_FIELD, json_string_nocheck(message->clientId)); json_object_set_new(root, SUCCESSFUL_FIELD, message->successful ? json_true() : json_false()); json_object_set_new(root, VERSION_FIELD, json_string_nocheck(message->version)); if (message->supportedConnectionTypes) json_object_set(root, SUPPORTED_CONNECTION_TYPES_FIELD, message->supportedConnectionTypes); if (message->connectionType) json_object_set_new(root, CONNECTION_TYPE_FIELD, json_string_nocheck(message->connectionType)); if (message->data) json_object_set(root, DATA_FIELD, message->data); if (message->ext) json_object_set(root, EXT_FIELD, message->ext); if (message->advice.present) { if (message->advice.sourceAdvice) json_object_set_new(root, ADVICE_FIELD, message->advice.sourceAdvice); else { json_t * advice = json_object(); if (message->advice.reconnect) json_object_set_new(advice, RECONNECT_FIELD, json_string_nocheck(message->advice.reconnect)); json_object_set_new(advice, INTERVAL_FIELD, json_integer(message->advice.interval)); json_object_set(root, DATA_FIELD, message->data); json_object_set_new(root, ADVICE_FIELD, advice); } } int result = json_dump_callback(root, callback, output, 0); json_decref(root); return result; }
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer) { if (file_ctx->sensor_name) { json_object_set_new(js, "host", json_string(file_ctx->sensor_name)); } if (file_ctx->is_pcap_offline) { json_object_set_new(js, "pcap_filename", json_string(PcapFileGetFilename())); } if (file_ctx->prefix) { MemBufferWriteRaw((*buffer), file_ctx->prefix, file_ctx->prefix_len); } OutputJSONMemBufferWrapper wrapper = { .buffer = buffer, .expand_by = OUTPUT_BUFFER_SIZE }; int r = json_dump_callback(js, OutputJSONMemBufferCallback, &wrapper, file_ctx->json_flags); if (r != 0) return TM_ECODE_OK; LogFileWrite(file_ctx, *buffer); return 0; }
static int send_metric(struct mg_connection *conn) { static const char *metric_types[] = { "gauge", "meter", "counter", "histogram", "timer", "internal" }; static const char *expire_status[] = { "disabled", "inactive", "active" }; struct brubeck_server *server = conn->server_param; struct brubeck_metric *metric = safe_lookup_metric( server, conn->uri + strlen("/metric/")); if (metric) { json_t *mj = json_pack("{s:s, s:s, s:i, s:s}", "key", metric->key, "type", metric_types[metric->type], #if METRIC_SHARD_SPECIFIER "shard", (int)metric->shard, #else "shard", 0, #endif "expire", expire_status[metric->expire] ); send_headers(conn); json_dump_callback(mj, &dump_json, (void *)conn, JSON_INDENT(4) | JSON_PRESERVE_ORDER); json_decref(mj); return MG_TRUE; } return MG_FALSE; }
int ast_json_dump_str_format(struct ast_json *root, struct ast_str **dst, enum ast_json_encoding_format format) { /* Jansson's json_dump*, even though it's a read operation, isn't * thread safe for concurrent reads. Locking is necessary. * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */ SCOPED_JSON_LOCK(root); return json_dump_callback((json_t *)root, write_to_ast_str, dst, dump_flags(format)); }
bool JSON::serializeToBuffer(utByteArray* bytes) { if (!_json || !bytes) { return false; } if (json_dump_callback(_json, json_dump_write_callback, bytes, JSON_SORT_KEYS) == -1) return false; return true; }
static void cache_program_binary(GLuint program, const SCP_string& hash) { if (!do_shader_caching()) { return; } GR_DEBUG_SCOPE("Saving shader binary"); GLint size; glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &size); if (size <= 0) { // No binary available (I'm looking at you Mesa...) return; } SCP_vector<uint8_t> binary; binary.resize((size_t) size); GLenum binary_fmt; GLsizei length; glGetProgramBinary(program, (GLsizei) binary.size(), &length, &binary_fmt, binary.data()); if (length == 0) { return; } auto base_filename = SCP_string("ogl_shader-") + hash; auto metadata_name = base_filename + ".json"; auto binary_name = base_filename + ".bin"; auto metadata_fp = cfopen(metadata_name.c_str(), "wb", CFILE_NORMAL, CF_TYPE_CACHE, false, CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT); if (!metadata_fp) { mprintf(("Could not open shader cache metadata file!\n")); return; } auto metadata = json_pack("{sI}", "format", (json_int_t)binary_fmt); if (json_dump_callback(metadata, json_write_callback, metadata_fp, 0) != 0) { mprintf(("Failed to write shader cache metadata file!\n")); cfclose(metadata_fp); return; } cfclose(metadata_fp); json_decref(metadata); auto binary_fp = cfopen(binary_name.c_str(), "wb", CFILE_NORMAL, CF_TYPE_CACHE, false, CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT); if (!binary_fp) { mprintf(("Could not open shader cache binary file!\n")); return; } cfwrite(binary.data(), 1, (int) binary.size(), binary_fp); cfclose(binary_fp); }
static void encode_null() { if(json_dumps(NULL, JSON_ENCODE_ANY) != NULL) fail("json_dumps didn't fail for NULL"); if(json_dumpf(NULL, stderr, JSON_ENCODE_ANY) != -1) fail("json_dumpf didn't fail for NULL"); /* Don't test json_dump_file to avoid creating a file */ if(json_dump_callback(NULL, encode_null_callback, NULL, JSON_ENCODE_ANY) != -1) fail("json_dump_callback didn't fail for NULL"); }
void node_send_json(json_t *array) { strbuffer_t strbuff; int rv = strbuffer_init(&strbuff); assert(rv == 0); if (0 == json_dump_callback(array, dump_to_strbuffer, (void *)&strbuff, 0)) { strbuffer_append(&strbuff, "\n"); node_send_raw(strbuffer_value(&strbuff)); } strbuffer_close(&strbuff); json_decref(array); }
bool w_json_buffer_write(w_jbuffer_t *jr, w_stm_t stm, json_t *json, int flags) { struct jbuffer_write_data data = { stm, jr }; int res; res = json_dump_callback(json, jbuffer_write, &data, flags); if (res != 0) { return false; } if (jbuffer_write("\n", 1, &data) != 0) { return false; } return jbuffer_flush(&data); }
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer *buffer) { if (file_ctx->sensor_name) { json_object_set_new(js, "host", json_string(file_ctx->sensor_name)); } if (file_ctx->prefix) MemBufferWriteRaw(buffer, file_ctx->prefix, file_ctx->prefix_len); int r = json_dump_callback(js, MemBufferCallback, buffer, JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII| #ifdef JSON_ESCAPE_SLASH JSON_ESCAPE_SLASH #else 0 #endif ); if (r != 0) return TM_ECODE_OK; LogFileWrite(file_ctx, buffer); return 0; }
void pilot::JSONFileHandler::flush() { Assertion(_elementStack.size() == 1, "Not all sections or arrays have been ended!"); json_dump_callback(_rootObj, json_write_callback, _cfp, JSON_INDENT(4)); }
void RulesDumpMatchArray(const DetectEngineThreadCtx *det_ctx, const Packet *p) { json_t *js = CreateJSONHeader(p, 0, "inspectedrules"); if (js == NULL) return; json_t *ir = json_object(); if (ir == NULL) return; json_object_set_new(ir, "rule_group_id", json_integer(det_ctx->sgh->id)); json_object_set_new(ir, "rule_cnt", json_integer(det_ctx->match_array_cnt)); json_t *js_array = json_array(); uint32_t x; for (x = 0; x < det_ctx->match_array_cnt; x++) { const Signature *s = det_ctx->match_array[x]; if (s == NULL) continue; json_t *js_sig = json_object(); if (unlikely(js == NULL)) continue; json_object_set_new(js_sig, "sig_id", json_integer(s->id)); json_object_set_new(js_sig, "mpm", (s->mpm_sm != NULL) ? json_true() : json_false()); if (s->mpm_sm != NULL) { char orig[256] = ""; char chop[256] = ""; DumpFp(s->mpm_sm, orig, sizeof(orig), chop, sizeof(chop)); json_object_set_new(js_sig, "mpm_buffer", json_string(DetectListToHumanString(SigMatchListSMBelongsTo(s, s->mpm_sm)))); json_object_set_new(js_sig, "mpm_pattern", json_string(orig)); if (strlen(chop) > 0) { json_object_set_new(js_sig, "mpm_pattern_chop", json_string(chop)); } } json_array_append_new(js_array, js_sig); } json_object_set_new(ir, "rules", js_array); json_object_set_new(js, "inspectedrules", ir); const char *filename = "packet_inspected_rules.json"; const char *log_dir = ConfigGetLogDirectory(); char log_path[PATH_MAX] = ""; snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename); MemBuffer *mbuf = NULL; mbuf = MemBufferCreateNew(4096); BUG_ON(mbuf == NULL); OutputJSONMemBufferWrapper wrapper = { .buffer = &mbuf, .expand_by = 4096, }; int r = json_dump_callback(js, OutputJSONMemBufferCallback, &wrapper, JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII| JSON_ESCAPE_SLASH); if (r != 0) { SCLogWarning(SC_ERR_SOCKET, "unable to serialize JSON object"); } else { MemBufferWriteString(mbuf, "\n"); SCMutexLock(&g_rule_dump_write_m); FILE *fp = fopen(log_path, "a"); if (fp != NULL) { MemBufferPrintToFPAsString(mbuf, fp); fclose(fp); SCMutexUnlock(&g_rule_dump_write_m); } } MemBufferFree(mbuf); json_object_clear(js); json_decref(js); }
static int send_stats(struct mg_connection *conn) { struct brubeck_server *brubeck = conn->server_param; json_t *stats, *secure, *backends, *samplers; int i; backends = json_array(); for (i = 0; i < brubeck->active_backends; ++i) { struct brubeck_backend *backend = brubeck->backends[i]; if (backend->type == BRUBECK_BACKEND_CARBON) { struct brubeck_carbon *carbon = (struct brubeck_carbon *)backend; struct sockaddr_in *address = &carbon->out_sockaddr; char addr[INET_ADDRSTRLEN]; json_array_append_new(backends, json_pack("{s:s, s:i, s:b, s:s, s:i, s:I}", "type", "carbon", "sample_freq", (int)carbon->backend.sample_freq, "connected", (carbon->out_sock >= 0), "address", inet_ntop(AF_INET, &address->sin_addr.s_addr, addr, INET_ADDRSTRLEN), "port", (int)ntohs(address->sin_port), "sent", (json_int_t)carbon->sent )); } } samplers = json_array(); for (i = 0; i < brubeck->active_samplers; ++i) { struct brubeck_sampler *sampler = brubeck->samplers[i]; struct sockaddr_in *address = &sampler->addr; char addr[INET_ADDRSTRLEN]; const char *sampler_name = NULL; switch (sampler->type) { case BRUBECK_SAMPLER_STATSD: sampler_name = "statsd"; break; case BRUBECK_SAMPLER_STATSD_SECURE: sampler_name = "statsd_secure"; break; default: assert(0); } json_array_append_new(samplers, json_pack("{s:s, s:f, s:s, s:i}", "type", sampler_name, "sample_freq", (double)sampler->current_flow, "address", inet_ntop(AF_INET, &address->sin_addr.s_addr, addr, INET_ADDRSTRLEN), "port", (int)ntohs(address->sin_port) )); } secure = json_pack("{s:i, s:i, s:i, s:i}", "failed", brubeck->stats.secure.failed, "from_future", brubeck->stats.secure.from_future, "delayed", brubeck->stats.secure.delayed, "replayed", brubeck->stats.secure.replayed ); stats = json_pack("{s:s, s:i, s:i, s:i, s:i, s:o, s:o, s:o}", "version", "brubeck " GIT_SHA, "metrics", brubeck->stats.metrics, "errors", brubeck->stats.errors, "unique_keys", brubeck->stats.unique_keys, "memory", brubeck->stats.memory, "secure", secure, "backends", backends, "samplers", samplers); json_dump_callback(stats, &dump_json, (void *)conn, JSON_INDENT(4) | JSON_PRESERVE_ORDER); json_decref(stats); return MG_TRUE; }
int la_codec_dump_callback(const la_codec_value_t *json, la_codec_dump_callback_t callback, void *data, size_t flags) { return json_dump_callback((const json_t *) json, callback, data, flags); }
static void writeJSONResponse(json_t *j, size_t flags) { FCGI_fputs("Content-Type: application/json\r\n\r\n", FCGI_stdout); json_dump_callback(j, fcgi_out_jansson, static_cast<void *>(FCGI_stdout), flags); }