Line data Source code
1 : /** 2 : * @file json.c 3 : * @brief Wrapper around cJSON library with helpers 4 : * 5 : * This library is only a simple wrapper around the cJSON library, providing 6 : * helper functions. 7 : * 8 : * @author François Cerbelle (Fanfan), francois@cerbelle.net 9 : * 10 : * @internal 11 : * Created: 29/10/2024 12 : * Revision: none 13 : * Last modified: 2024-11-05 19:02 14 : * Compiler: gcc 15 : * Organization: Cerbelle.net 16 : * Copyright: Copyright (c) 2024, François Cerbelle 17 : * 18 : * This source code is released for free distribution under the terms of the 19 : * GNU General Public License as published by the Free Software Foundation. 20 : */ 21 : 22 : #ifdef HAVE_CONFIG_H 23 : #include "config.h" 24 : #endif 25 : 26 : #include "json.h" 27 : #include <stdio.h> 28 : #include <stdlib.h> 29 : #include <string.h> 30 : 31 168 : char* json2text(cJSON* value_json) { 32 : char* retval; 33 168 : if (cJSON_IsNull(value_json)) 34 3 : retval = strdup("null"); 35 165 : else if (cJSON_IsString(value_json)) 36 138 : retval = strdup(value_json->valuestring); 37 27 : else if (cJSON_IsNumber(value_json)) { 38 3 : retval = (char*)malloc(20); 39 3 : if (retval) 40 3 : snprintf(retval,20,"%d",value_json->valueint); 41 24 : } else if (cJSON_IsBool(value_json)) { 42 6 : if (cJSON_IsTrue(value_json)) 43 3 : retval = strdup("true"); 44 : else 45 3 : retval = strdup("false"); 46 18 : } else if (cJSON_IsArray(value_json)) { 47 3 : retval = cJSON_PrintUnformatted(value_json); 48 15 : } else if (cJSON_IsObject(value_json)) { 49 6 : retval = cJSON_PrintUnformatted(value_json); 50 : } else 51 9 : retval = strdup("Invalid data"); 52 168 : return retval; 53 : } 54 : 55 : /* vim: set tw=80: */