diff --git a/examples/cjson_demo/.gitignore b/examples/cjson_demo/.gitignore new file mode 100644 index 0000000..76b743d --- /dev/null +++ b/examples/cjson_demo/.gitignore @@ -0,0 +1,6 @@ + +dist +build +.config.mk +.flash.conf.json + diff --git a/examples/cjson_demo/SConstruct b/examples/cjson_demo/SConstruct new file mode 100644 index 0000000..076d65c --- /dev/null +++ b/examples/cjson_demo/SConstruct @@ -0,0 +1,4 @@ +from pathlib import Path +import os +with open(str(Path(os.getcwd())/'..'/'..'/'tools'/'scons'/'project.py')) as f: + exec(f.read()) diff --git a/examples/cjson_demo/config_defaults.mk b/examples/cjson_demo/config_defaults.mk new file mode 100644 index 0000000..0d94aed --- /dev/null +++ b/examples/cjson_demo/config_defaults.mk @@ -0,0 +1,8 @@ +# unix +# CONFIG_TOOLCHAIN_PATH="/opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin" +# win +# CONFIG_TOOLCHAIN_PATH="..\\gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf\\bin" + +# CONFIG_TOOLCHAIN_PREFIX="arm-linux-gnueabihf-" + +CONFIG_CJSON_ENABLED=y \ No newline at end of file diff --git a/examples/cjson_demo/main/Kconfig b/examples/cjson_demo/main/Kconfig new file mode 100644 index 0000000..e69de29 diff --git a/examples/cjson_demo/main/SConstruct b/examples/cjson_demo/main/SConstruct new file mode 100644 index 0000000..9708864 --- /dev/null +++ b/examples/cjson_demo/main/SConstruct @@ -0,0 +1,33 @@ +# project_root/src/SConscript +import os +# Import the environment from the SConstruct file +Import('env') +with open(env['PROJECT_TOOL_S']) as f: + exec(f.read()) + + +SRCS = Glob('src/*.c*') +INCLUDE = [ADir('include'), ADir('.')] +PRIVATE_INCLUDE = [] +REQUIREMENTS = ['pthread', 'cjson'] +STATIC_LIB = [] +DYNAMIC_LIB = [] +DEFINITIONS = [] +DEFINITIONS_PRIVATE = [] +LDFLAGS = [] +LINK_SEARCH_PATH = [] + + +env['COMPONENTS'].append({'target':env['PROJECT_NAME'], + 'SRCS':SRCS, + 'INCLUDE':INCLUDE, + 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, + 'REQUIREMENTS':REQUIREMENTS, + 'STATIC_LIB':STATIC_LIB, + 'DYNAMIC_LIB':DYNAMIC_LIB, + 'DEFINITIONS':DEFINITIONS, + 'DEFINITIONS_PRIVATE':DEFINITIONS_PRIVATE, + 'LDFLAGS':LDFLAGS, + 'LINK_SEARCH_PATH':LINK_SEARCH_PATH, + 'REGISTER':'project' + }) \ No newline at end of file diff --git a/examples/cjson_demo/main/include/main.h b/examples/cjson_demo/main/include/main.h new file mode 100644 index 0000000..45dcbb0 --- /dev/null +++ b/examples/cjson_demo/main/include/main.h @@ -0,0 +1,3 @@ +#pragma once + + diff --git a/examples/cjson_demo/main/src/main.c b/examples/cjson_demo/main/src/main.c new file mode 100644 index 0000000..228cad5 --- /dev/null +++ b/examples/cjson_demo/main/src/main.c @@ -0,0 +1,59 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +#include +#include +#include "cJSON.h" + +int main() { + // Create a JSON object + cJSON *root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "name", "John Doe"); + cJSON_AddNumberToObject(root, "age", 30); + cJSON_AddBoolToObject(root, "employed", 1); + + // Create an array and add it to the JSON object + cJSON *skills = cJSON_CreateArray(); + cJSON_AddItemToArray(skills, cJSON_CreateString("C")); + cJSON_AddItemToArray(skills, cJSON_CreateString("C++")); + cJSON_AddItemToArray(skills, cJSON_CreateString("Python")); + cJSON_AddItemToObject(root, "skills", skills); + + // Convert the JSON object to a string and print it + char *json_string = cJSON_Print(root); + printf("Generated JSON:\n%s\n", json_string); + + // Parse the JSON string + cJSON *parsed_root = cJSON_Parse(json_string); + if (parsed_root == NULL) { + fprintf(stderr, "Error parsing JSON string.\n"); + } else { + // Get data from the parsed JSON object + cJSON *name = cJSON_GetObjectItem(parsed_root, "name"); + cJSON *age = cJSON_GetObjectItem(parsed_root, "age"); + cJSON *employed = cJSON_GetObjectItem(parsed_root, "employed"); + cJSON *parsed_skills = cJSON_GetObjectItem(parsed_root, "skills"); + + // Print the parsed data + printf("Parsed JSON:\n"); + printf("Name: %s\n", name->valuestring); + printf("Age: %d\n", age->valueint); + printf("Employed: %s\n", employed->valueint ? "true" : "false"); + + printf("Skills:\n"); + int skill_count = cJSON_GetArraySize(parsed_skills); + for (int i = 0; i < skill_count; i++) { + cJSON *skill = cJSON_GetArrayItem(parsed_skills, i); + printf(" - %s\n", skill->valuestring); + } + } + + // Free memory + cJSON_Delete(root); + cJSON_Delete(parsed_root); + free(json_string); + + return 0; +}