| 802 | } |
| 803 | |
| 804 | char *cbm_layout_to_json(const cbm_layout_result_t *r) { |
| 805 | if (!r) |
| 806 | return NULL; |
| 807 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 808 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 809 | yyjson_mut_doc_set_root(doc, root); |
| 810 | |
| 811 | yyjson_mut_val *na = yyjson_mut_arr(doc); |
| 812 | for (int i = 0; i < r->node_count; i++) { |
| 813 | yyjson_mut_val *nd = yyjson_mut_obj(doc); |
| 814 | yyjson_mut_obj_add_int(doc, nd, "id", r->nodes[i].id); |
| 815 | double nx = isfinite(r->nodes[i].x) ? (double)r->nodes[i].x : 0.0; |
| 816 | double ny = isfinite(r->nodes[i].y) ? (double)r->nodes[i].y : 0.0; |
| 817 | double nz = isfinite(r->nodes[i].z) ? (double)r->nodes[i].z : 0.0; |
| 818 | yyjson_mut_obj_add_real(doc, nd, "x", nx); |
| 819 | yyjson_mut_obj_add_real(doc, nd, "y", ny); |
| 820 | yyjson_mut_obj_add_real(doc, nd, "z", nz); |
| 821 | if (r->nodes[i].label) |
| 822 | yyjson_mut_obj_add_str(doc, nd, "label", r->nodes[i].label); |
| 823 | if (r->nodes[i].name) |
| 824 | yyjson_mut_obj_add_str(doc, nd, "name", r->nodes[i].name); |
| 825 | if (r->nodes[i].file_path) |
| 826 | yyjson_mut_obj_add_str(doc, nd, "file_path", r->nodes[i].file_path); |
| 827 | if (r->nodes[i].qualified_name) |
| 828 | yyjson_mut_obj_add_str(doc, nd, "qualified_name", r->nodes[i].qualified_name); |
| 829 | if (r->nodes[i].start_line > 0) |
| 830 | yyjson_mut_obj_add_int(doc, nd, "start_line", r->nodes[i].start_line); |
| 831 | if (r->nodes[i].end_line > 0) |
| 832 | yyjson_mut_obj_add_int(doc, nd, "end_line", r->nodes[i].end_line); |
| 833 | double nsz = isfinite(r->nodes[i].size) ? (double)r->nodes[i].size : 1.0; |
| 834 | yyjson_mut_obj_add_real(doc, nd, "size", nsz); |
| 835 | char hex[CBM_SZ_8]; |
| 836 | snprintf(hex, sizeof(hex), "#%06x", r->nodes[i].color); |
| 837 | yyjson_mut_obj_add_strcpy(doc, nd, "color", hex); |
| 838 | yyjson_mut_obj_add_int(doc, nd, "in_calls", r->nodes[i].in_calls); |
| 839 | if (r->nodes[i].status) |
| 840 | yyjson_mut_obj_add_str(doc, nd, "status", r->nodes[i].status); |
| 841 | yyjson_mut_arr_append(na, nd); |
| 842 | } |
| 843 | yyjson_mut_obj_add_val(doc, root, "nodes", na); |
| 844 | |
| 845 | yyjson_mut_val *ea = yyjson_mut_arr(doc); |
| 846 | for (int i = 0; i < r->edge_count; i++) { |
| 847 | yyjson_mut_val *ed = yyjson_mut_obj(doc); |
| 848 | yyjson_mut_obj_add_int(doc, ed, "source", r->edges[i].source); |
| 849 | yyjson_mut_obj_add_int(doc, ed, "target", r->edges[i].target); |
| 850 | if (r->edges[i].type) |
| 851 | yyjson_mut_obj_add_str(doc, ed, "type", r->edges[i].type); |
| 852 | yyjson_mut_arr_append(ea, ed); |
| 853 | } |
| 854 | yyjson_mut_obj_add_val(doc, root, "edges", ea); |
| 855 | yyjson_mut_obj_add_int(doc, root, "total_nodes", r->total_nodes); |
| 856 | |
| 857 | size_t len = 0; |
| 858 | yyjson_write_err write_err = {0}; |
| 859 | char *json = |
| 860 | yyjson_mut_write_opts(doc, YYJSON_WRITE_ALLOW_INVALID_UNICODE, NULL, &len, &write_err); |
| 861 | yyjson_mut_doc_free(doc); |
no outgoing calls
no test coverage detected