132 lines
3.6 KiB
C
132 lines
3.6 KiB
C
//
|
|
// Created by cfif on 27.10.22.
|
|
//
|
|
#include <string.h>
|
|
#include "tiny-json.h"
|
|
#include "mtojson.h"
|
|
|
|
typedef struct {
|
|
float param1;
|
|
char param2[16];
|
|
uint32_t param3;
|
|
uint32_t param4;
|
|
uint32_t param5;
|
|
} com1;
|
|
|
|
typedef struct {
|
|
uint32_t param1;
|
|
uint32_t param2;
|
|
uint32_t param3;
|
|
uint32_t param4;
|
|
uint32_t param5;
|
|
} com2;
|
|
|
|
com1 dataCom1 = {1.123456, "qwerty", 3, 4, 5};
|
|
com2 dataCom2 = {6, 7,8, 9, 0};
|
|
|
|
const char *listComName[] = {"com1", "com2"};
|
|
const void *listComStruct[] = { &dataCom1, &dataCom2};
|
|
const int countListCom = sizeof(listComStruct) / 4;
|
|
|
|
const struct to_json jsonDataArray1[] = {
|
|
{.value = &dataCom1.param1, .vtype = t_to_float, .stype = t_to_array},
|
|
{.value = &dataCom1.param2, .vtype = t_to_string,},
|
|
{.value = &dataCom1.param3, .vtype = t_to_uint32_t,},
|
|
{.value = &dataCom1.param4, .vtype = t_to_uint32_t,},
|
|
{.value = &dataCom1.param5, .vtype = t_to_uint32_t,},
|
|
{NULL}
|
|
};
|
|
|
|
const struct to_json jsonDataArray2[] = {
|
|
{.value = &dataCom2.param1, .vtype = t_to_uint32_t, .stype = t_to_array},
|
|
{.value = &dataCom2.param2, .vtype = t_to_uint32_t,},
|
|
{.value = &dataCom2.param3, .vtype = t_to_uint32_t,},
|
|
{.value = &dataCom2.param4, .vtype = t_to_uint32_t,},
|
|
{.value = &dataCom2.param5, .vtype = t_to_uint32_t,},
|
|
{NULL}
|
|
|
|
};
|
|
|
|
const struct to_json jsonCom1 = {.name = "com1", .value = &jsonDataArray1, .vtype = t_to_array, .stype = t_to_object};
|
|
const struct to_json jsonCom2 = {.name = "com2", .value = &jsonDataArray2, .vtype = t_to_array, .stype = t_to_object};
|
|
|
|
enum { MAX_STRING_LEN = 1000 };
|
|
char json_text[MAX_STRING_LEN] = {0};
|
|
|
|
void testJson() {
|
|
|
|
const struct to_json jsonDataObject[] = {
|
|
jsonCom1,
|
|
jsonCom2,
|
|
{NULL}
|
|
};
|
|
|
|
size_t json_len = json_generate(&json_text[0], jsonDataObject, MAX_STRING_LEN);
|
|
|
|
|
|
dataCom1.param1 = 0;
|
|
strcpy(dataCom1.param2, "qaz");
|
|
dataCom1.param3 = 0;
|
|
dataCom1.param4 = 0;
|
|
dataCom1.param5 = 0;
|
|
|
|
dataCom2.param1 = 0;
|
|
dataCom2.param2 = 0;
|
|
dataCom2.param3 = 0;
|
|
dataCom2.param4 = 0;
|
|
dataCom2.param5 = 0;
|
|
|
|
|
|
json_t pool[1000];
|
|
unsigned const qty = sizeof pool / sizeof *pool;
|
|
char comName[20];
|
|
|
|
//char str[] = "{\"com1\":[ 11.12345, \"qwertyuiop\", 13, 14, 15 ], \"com2\":[ 16, 17, 18, 19, 20 ]}";
|
|
|
|
json_t const *json = json_create(json_text, pool, qty);
|
|
|
|
json_t const *elementRoot = json_getChild(json);
|
|
while (elementRoot != 0) {
|
|
|
|
strcpy(comName, json_getName(elementRoot));
|
|
|
|
void *data_p = NULL;
|
|
|
|
for (int i = 0; i < countListCom; ++i) {
|
|
if (strncmp(comName, listComName[i], strlen(listComName[i])) == 0) {
|
|
data_p = listComStruct[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (data_p == NULL)
|
|
continue;
|
|
|
|
json_t const *array = json_getProperty(json, comName);
|
|
json_t const *element = json_getChild(array);
|
|
while (element != 0) {
|
|
|
|
if (json_getType(element) == JSON_REAL) {
|
|
((float *) data_p)[0] = (float) json_getReal(element);
|
|
data_p += 4;
|
|
}
|
|
|
|
if (json_getType(element) == JSON_INTEGER) {
|
|
((uint32_t *) data_p)[0] = json_getInteger(element);
|
|
data_p += 4;
|
|
}
|
|
|
|
if (json_getType(element) == JSON_TEXT) {
|
|
strcpy(data_p, json_getValue(element));
|
|
data_p += 16;
|
|
}
|
|
|
|
|
|
element = json_getSibling(element);
|
|
}
|
|
|
|
elementRoot = json_getSibling(elementRoot);
|
|
}
|
|
|
|
dataCom2.param5 = 0;
|
|
} |