Hello, i have problem with jansson pack. when i run the example code in simulator its output is correct as expected but when i change the device to for example stm32f103 the output is empty curly brackets {}; the example code is :
void add_2array_to_json( json_t* obj, const char* name, const int* marr, size_t dim1, size_t dim2 ) { size_t i, j; json_t* jarr1 = json_array(); for( i=0; i<dim1; ++i ) { json_t* jarr2 = json_array(); for( j=0; j<dim2; ++j ) { int val = marr[ i*dim2 + j ]; json_t* jval = json_integer( val ); json_array_append_new( jarr2, jval ); } json_array_append_new( jarr1, jarr2 ); } json_object_set_new( obj, name, jarr1 ); return; } void SystemClock_Config(void); static json_t* jdata; int main() { char* s; int arr1[2][3] = { {1,2,3}, {4,5,6} }; int arr2[4][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; char ss[128]; /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_USART1_UART_Init(); json_object_seed(1); jdata = json_object(); add_2array_to_json( jdata, "arr1", &arr1[0][0], 2, 3 ); add_2array_to_json( jdata, "arr2", &arr2[0][0], 4, 4 ); s = json_dumps( jdata, 0 ); HAL_UART_Transmit(&huart1,(uint8_t*)s,strlen(s),1000); // puts( s ); free( s ); json_decref( jdata ); while(1){ HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13); HAL_Delay(200); }; }
i used jansson v2.7 and also the latest v2.9. both the same am i wrong?
Have you given your program a properly sized heap so Jansson can allocate the required memory?
Next thing: Jansson can report errors. Have you checked?
Thanks alot for you reply i checked for errors. json_array_append_new and json_array_append return -1 . how can i set heap size?
the error -1 is because :
json_t* jval = json_integer( val );
jval is null
Of course jval is null if you don't configure a heap. The only way you can run Jansson without a heap is if you replace the Jansson allocation functions with your own.
Note that Jansson can often supply better error messages if you are making good use of the error message functionality.
Setting a heap? I think you should spend some time with a couple of sample programs but also spend some time learning what settings you can tweak during start-up.
yes thanks! it was heap size problem. i changer heap size in startup .s from 200 to 1000 Bytes and now i have all of outputs. i faced another problem: how can i get utf-8 output ? for example € sign:
jdata= json_pack("{sisi}", "cur", "€", "price", 7);
my output when i change the editor to utf-8 without signature is: {"cur": 134240804, "price": 7}
sorry my code was wrong the correct is: jdata= json_pack("{sssi}", L"cur", "€", "price", 7); and the output in ascii is: {"cur": "€", "price": 7} and "€" is equal to "0xE2 0x82 0xAC" the euro sign 3 byte of utf-8