@@ -21,10 +21,12 @@ char* bsp_generate_json(bsp_matrix_t matrix, cJSON* user_json) {
2121
2222 cJSON_AddItemToObject (j , "binsparse" , binsparse );
2323
24- cJSON * item ;
25- cJSON_ArrayForEach (item , user_json ) {
26- cJSON * item_copy = cJSON_Duplicate (item , 1 ); // 1 = deep copy
27- cJSON_AddItemToObject (j , item -> string , item_copy );
24+ if (user_json != NULL ) {
25+ cJSON * item ;
26+ cJSON_ArrayForEach (item , user_json ) {
27+ cJSON * item_copy = cJSON_Duplicate (item , 1 ); // 1 = deep copy
28+ cJSON_AddItemToObject (j , item -> string , item_copy );
29+ }
2830 }
2931
3032 cJSON_AddStringToObject (binsparse , "version" , BINSPARSE_VERSION );
@@ -136,7 +138,13 @@ bsp_error_t bsp_write_matrix_to_group_cjson(hid_t f, bsp_matrix_t matrix,
136138bsp_error_t bsp_write_matrix_to_group (hid_t f , bsp_matrix_t matrix ,
137139 const char * user_json ,
138140 int compression_level ) {
139- cJSON * user_json_cjson = cJSON_Parse (user_json );
141+ cJSON * user_json_cjson = NULL ;
142+ if (user_json != NULL ) {
143+ user_json_cjson = cJSON_Parse (user_json );
144+ }
145+ if (user_json_cjson == NULL ) {
146+ user_json_cjson = cJSON_CreateObject ();
147+ }
140148 bsp_error_t error = bsp_write_matrix_to_group_cjson (
141149 f , matrix , user_json_cjson , compression_level );
142150 cJSON_Delete (user_json_cjson );
@@ -181,7 +189,13 @@ bsp_error_t bsp_write_matrix_cjson(const char* fname, bsp_matrix_t matrix,
181189bsp_error_t bsp_write_matrix (const char * fname , bsp_matrix_t matrix ,
182190 const char * group , const char * user_json ,
183191 int compression_level ) {
184- cJSON * user_json_cjson = cJSON_Parse (user_json );
192+ cJSON * user_json_cjson = NULL ;
193+ if (user_json != NULL ) {
194+ user_json_cjson = cJSON_Parse (user_json );
195+ }
196+ if (user_json_cjson == NULL ) {
197+ user_json_cjson = cJSON_CreateObject ();
198+ }
185199
186200 bsp_error_t error = bsp_write_matrix_cjson (
187201 fname , matrix , group , user_json_cjson , compression_level );
0 commit comments