/* Helper routine for building cJSON objects in a printf-like manner. ** ** Sample call: ** j = iperf_json_printf("foo: %b bar: %d bletch: %f eep: %s", b, i, f, s); ** ** The four formatting characters and the types they expect are: ** %b boolean int ** %d integer int64_t ** %f floating point double ** %s string char * ** If the values you're passing in are not these exact types, you must ** cast them, there is no automatic type coercion/widening here. ** ** The colons mark the end of field names, and blanks are ignored. ** ** This routine is not particularly robust, but it's not part of the API, ** it's just for internal iperf3 use. */ cJSON* iperf_json_printf(const char *format, ...) { printf("\n printa \n"); cJSON* o; va_list argp; const char *cp; char name[100]; char* np; cJSON* j; o = cJSON_CreateObject(); if (o == NULL) return NULL; va_start(argp, format); np = name; for (cp = format; *cp != '\0'; ++cp) { switch (*cp) { case ' ': break; case ':': *np = '\0'; break; case '%': ++cp; switch (*cp) { case 'b': j = cJSON_CreateBool(va_arg(argp, int)); break; case 'd': j = cJSON_CreateInt(va_arg(argp, int64_t)); break; case 'f': j = cJSON_CreateFloat(va_arg(argp, double)); break; case 's': j = cJSON_CreateString(va_arg(argp, char *)); break; default: return NULL; } if (j == NULL) return NULL; cJSON_AddItemToObject(o, name, j); np = name; break; default: *np++ = *cp; break; } } va_end(argp); return o; }
static void _gpath_save_contig_hist2json(cJSON *json_hists, const size_t *arr_counts, size_t arr_len) { cJSON *lens = cJSON_CreateArray(); cJSON *cnts = cJSON_CreateArray(); size_t i; for(i = 1; i < arr_len; i++) { if(arr_counts[i]) { cJSON_AddItemToArray(lens, cJSON_CreateInt(i)); cJSON_AddItemToArray(cnts, cJSON_CreateInt(arr_counts[i])); } } cJSON *hist = cJSON_CreateObject(); cJSON_AddItemToObject(hist, "lengths", lens); cJSON_AddItemToObject(hist, "counts", cnts); cJSON_AddItemToArray(json_hists, hist); }
// Print JSON header to gzout static void bubble_caller_print_header(gzFile gzout, const char* out_path, BubbleCallingPrefs prefs, cJSON **hdrs, size_t nhdrs, const dBGraph *db_graph) { size_t i; // Construct cJSON cJSON *json = cJSON_CreateObject(); cJSON_AddStringToObject(json, "file_format", "CtxBubbles"); cJSON_AddNumberToObject(json, "format_version", BUBBLE_FORMAT_VERSION); // Add standard cortex headers json_hdr_make_std(json, out_path, hdrs, nhdrs, db_graph); // Add parameters used in bubble calling to the header json_hdr_augment_cmd(json, "bubbles", "max_flank_kmers", cJSON_CreateInt(prefs.max_flank_len)); json_hdr_augment_cmd(json, "bubbles", "max_allele_kmers", cJSON_CreateInt(prefs.max_allele_len)); cJSON *haploids = cJSON_CreateArray(); for(i = 0; i < prefs.num_haploid; i++) cJSON_AddItemToArray(haploids, cJSON_CreateInt(prefs.haploid_cols[i])); json_hdr_augment_cmd(json, "bubbles", "haploid_colours", haploids); // Write header to file json_hdr_gzprint(json, gzout); // Print comments about the format gzputs(gzout, "\n"); gzputs(gzout, "# This file was generated with McCortex\n"); gzputs(gzout, "# written by Isaac Turner <*****@*****.**>\n"); gzputs(gzout, "# url: "CORTEX_URL"\n"); gzputs(gzout, "# \n"); gzputs(gzout, "# Comment lines begin with a # and are ignored, but must come after the header\n"); gzputs(gzout, "\n"); cJSON_Delete(json); }
cJSON *cJSON_CreateIntArray( int64_t *numbers, int count ) { int i; cJSON *n = 0, *p = 0, *a = cJSON_CreateArray(); for ( i = 0; a && i < count; ++i ) { n = cJSON_CreateInt( numbers[i] ); if ( ! i ) a->child = n; else suffix_object( p, n ); p = n; } return a; }