/
superterminator
/
JsonParse
Обзор
Документация
Войти
/
superterminator
/
JsonParse
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/test.c
164 строки
5 KB
superterminator
initial commit
15 сен 2024, 11:40
15 сен 2024, 11:40
f4b359b
Код
Авторство
О чём код?
#include "include/mapper.h" #include "include/json_parse.h" #include <stdio.h> #include <string.h> #include <stdlib.h> void print_subnodes(const struct JsonNode* nodes, int level) { const struct JsonNode* cur_node = nodes; while (cur_node) { char text[256] = { 0 }; const char* type = ""; switch (cur_node->type) { case JsonNodeArray: type = "array"; break; case JsonNodeArrayValue: type = "array value"; break; case JsonNodeCommaArrays: type = "comma arrays"; break; case JsonNodeCommaFields: type = "comma fields"; break; case JsonNodeCommaNamed: type = "comma named"; break; case JsonNodeCommaObjects: type = "comma objects"; break; case JsonNodeField: type = "field"; break; case JsonNodeKeyword: type = "keyword"; break; case JsonNodeObject: type = "object"; break; case JsonNodeObjectValue: type = "object value"; break; case JsonNodeString: type = "string"; break; case JsonNodeUnknown: type = "unknown"; break; case JsonNodeValue: type = "value"; break; } char token_text[256] = { 0 }; if(cur_node->token) memcpy(token_text, cur_node->token->text, cur_node->token->len); printf("%*s type : %s, token: %s\n", level * 4, "", type, token_text); if (cur_node->subnodes) print_subnodes(cur_node->subnodes, level + 1); cur_node = cur_node->next; } } void check_subnode(const struct JsonNode* parent_node, const char* name) { const struct JsonNode* subnode = 0; int err = get_subnode_named(name, parent_node, &subnode); printf("subnode %s found = %d, err = %d\n", name, subnode != 0, err); } void test_json_parser() { char* json_text = \ "{\ \"field1_bool\" : True,\ \"field2_bool\" : false,\ \"field3_int\" : 1232,\ \"field4_float\" : 323.54,\ \"field5_string\" : \"hello string1\",\ \"field6_array\" : [\"53.342\", \"array string 1\", \"array string 2\"],\ \"field7_array\" : [{\"val1\" : \"val1_string\"}, {\"val2\" : \"val2_string\"}],\ \"field8_object\" : {\"object_field1\":\"53.342\", \"object_field2\":\"object field 2\",\"object_array1\":[234,543, 132]},\ \"field9_object\" : {\"object_field1\":\"object2 field1\", \"object_field2\":\"object2 field 2\",\"object_array1\":[234, 543, 132]}\ }"; struct JsonDocument* doc = 0; int err = init_json_document(&doc, json_text); if (err || doc == 0) { printf("Cannot parse json %d\n", err); return; } //print tokens list struct JsonTokenList* cur_token = doc->tokens; while (cur_token) { char text[256] = { 0 }; memcpy(text, cur_token->token.text, cur_token->token.len); printf("token : %s\n", text); cur_token = cur_token->next; } print_subnodes(doc->nodes, 0); check_subnode(doc->nodes, "field1_bool"); check_subnode(doc->nodes, "field2_bool"); check_subnode(doc->nodes, "field3_int"); check_subnode(doc->nodes, "field4_float"); check_subnode(doc->nodes, "field5_string"); check_subnode(doc->nodes, "field6_array"); check_subnode(doc->nodes, "field7_array"); check_subnode(doc->nodes, "field8_object"); check_subnode(doc->nodes, "field9_object"); check_subnode(doc->nodes, "field_no_exists"); deinit_json_document(&doc); } void test_json_values() { int out_int = 0; float out_float = 0; int err = get_str_int_value("54", 2, &out_int); printf("54 value %d, err = %d\n", out_int, err); out_int = 0; err = get_str_int_value("4.2", 3, &out_int); printf("4.2 value %d, err = %d\n", out_int, err); out_int = 0; err = get_str_int_value("True", 4, &out_int); printf("True value %d, err = %d\n", out_int, err); out_int = 1; err = get_str_int_value("false", 5, &out_int); printf("false value %d, err = %d\n", out_int, err); err = get_str_float_value("6.32", 4, &out_float); printf("6.32 value %f, err = %d\n", out_float, err); out_float = 0; err = get_str_float_value("129", 3, &out_float); printf("129 value %f, err = %d\n", out_float, err); out_float = 0; err = get_str_float_value("78.0", 4, &out_float); printf("78.0 value %f, err = %d\n", out_float, err); } int main() { //int val = 0; //testChainMapper1(&val); test_json_parser(); test_json_values(); return 0; }