#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
JSON_NULL,
JSON_BOOL,
JSON_NUMBER,
JSON_STRING,
JSON_ARRAY,
JSON_OBJECT
} JsonType;
typedef struct JsonNode {
JsonType type;
char *key;
union {
unsigned char boolean;
double number;
char *string;
} value;
struct JsonNode *child;
struct JsonNode *sibling;
} JsonNode;
// 自定义字符串复制函数
static char *json_strdup(char *str) {
if (!str)
return NULL;
size_t len = strlen(str) + 1;
char *copy = (char *)malloc(len);
if (copy) {
memcpy(copy, str, len);
}
return copy;
}
// 保持您指定的节点创建函数
static JsonNode *json_new_node(JsonType type, ...) {
JsonNode *node = (JsonNode *)malloc(sizeof(JsonNode));
if (!node)
return NULL;
node->type = type;
node->key = NULL;
node->child = NULL;
node->sibling = NULL;
va_list args;
va_start(args, type);
switch (type) {
case JSON_BOOL:
node->value.boolean = va_arg(args, int);
break;
case JSON_NUMBER:
node->value.number = va_arg(args, double);
break;
case JSON_STRING: {
char *str = va_arg(args, char *);
if (str) {
node->value.string = json_strdup(str); // 使用自定义复制函数
if (!node->value.string) {
free(node);
node = NULL;
}
} else {
node->value.string = NULL;
}
break;
}
case JSON_NULL:
case JSON_ARRAY:
case JSON_OBJECT:
// 这些类型不需要额外参数
break;
}
va_end(args);
return node;
}
// 保持您指定的宏定义
#define json_new_null() json_new_node(JSON_NULL)
#define json_new_bool(value) json_new_node(JSON_BOOL, (int)(value))
#define json_new_number(value) json_new_node(JSON_NUMBER, (double)(value))
#define json_new_string(value) json_new_node(JSON_STRING, (char *)(value))
#define json_new_array() json_new_node(JSON_ARRAY)
#define json_new_object() json_new_node(JSON_OBJECT)
// 添加子节点
unsigned char json_add_child(JsonNode *parent, JsonNode *child) {
if (!parent || !child)
return 0x00;
if (parent->type != JSON_ARRAY && parent->type != JSON_OBJECT) {
return 0x00;
}
if (!parent->child) {
parent->child = child;
} else {
JsonNode *sibling = parent->child;
while (sibling->sibling) {
sibling = sibling->sibling;
}
sibling->sibling = child;
}
return 0x01;
}
// 添加键值对(修改为复制key字符串)
unsigned char json_add_pair(JsonNode *object, char *key,
JsonNode *value) {
if (!object || object->type != JSON_OBJECT || !key || !value) {
return 0x00;
}
value->key = json_strdup(key); // 使用自定义复制函数
if (!value->key)
return 0x00;
return json_add_child(object, value);
}
// 释放JSON树
void json_free(JsonNode *node) {
if (!node)
return;
if (node->key)
free(node->key);
if (node->type == JSON_STRING && node->value.string) {
free(node->value.string);
}
json_free(node->child);
json_free(node->sibling);
free(node);
}
// 辅助打印函数
static void json_print_indent(int level) {
for (int i = 0; i < level; i++) {
printf(" ");
}
}
// 格式化打印JSON
static void json_print_formatted(const JsonNode *node, int indent_level,
unsigned char is_array_element) {
if (!node) {
printf("null");
return;
}
switch (node->type) {
case JSON_NULL:
printf("null");
break;
case JSON_BOOL:
printf(node->value.boolean ? "true" : "false");
break;
case JSON_NUMBER:
printf("%g", node->value.number);
break;
case JSON_STRING:
printf("\"%s\"", node->value.string);
break;
case JSON_ARRAY: {
printf("[\n");
JsonNode *child = node->child;
unsigned char first = 0x01;
while (child) {
if (!first)
printf(",\n");
json_print_indent(indent_level + 1);
json_print_formatted(child, indent_level + 1, 0x01);
child = child->sibling;
first = 0x00;
}
printf("\n");
json_print_indent(indent_level);
printf("]");
break;
}
case JSON_OBJECT: {
if (!is_array_element) {
printf("\n");
json_print_indent(indent_level);
}
printf("{\n");
JsonNode *child = node->child;
unsigned char first = 0x01;
while (child) {
if (!first)
printf(",\n");
json_print_indent(indent_level + 1);
printf("\"%s\": ", child->key);
json_print_formatted(child, indent_level + 1, 0x00);
child = child->sibling;
first = 0x00;
}
printf("\n");
json_print_indent(indent_level);
printf("}");
break;
}
}
}
// 漂亮打印JSON
void json_print_pretty(JsonNode *node) {
json_print_formatted(node, 0, 0x00);
printf("\n");
}
// 测试函数
void test0() {
// 创建根对象
JsonNode *root = json_new_object();
// 添加基本类型
json_add_pair(root, "nullValue", json_new_null());
json_add_pair(root, "trueValue", json_new_bool(0x01));
json_add_pair(root, "falseValue", json_new_bool(0x00));
json_add_pair(root, "integer", json_new_number(42));
json_add_pair(root, "float", json_new_number(3.14159));
json_add_pair(root, "string", json_new_string("Hello, 世界!"));
// 创建空数组和空对象
json_add_pair(root, "emptyArray", json_new_array());
json_add_pair(root, "emptyObject", json_new_object());
// 创建复杂数组
JsonNode *complexArray = json_new_array();
json_add_child(complexArray, json_new_null());
json_add_child(complexArray, json_new_bool(0x01));
json_add_child(complexArray, json_new_number(123.456));
json_add_child(complexArray, json_new_string("数组中的字符串"));
// 数组中嵌套数组
JsonNode *nestedArray = json_new_array();
json_add_child(nestedArray, json_new_number(1));
json_add_child(nestedArray, json_new_number(2));
json_add_child(nestedArray, json_new_number(3));
json_add_child(complexArray, nestedArray);
// 数组中嵌套对象
JsonNode *arrayObj = json_new_object();
json_add_pair(arrayObj, "key1", json_new_string("value1"));
json_add_pair(arrayObj, "key2", json_new_string("value2"));
json_add_child(complexArray, arrayObj);
json_add_pair(root, "complexArray", complexArray);
// 创建复杂对象
JsonNode *complexObject = json_new_object();
// 对象中包含各种类型
json_add_pair(complexObject, "nullField", json_new_null());
json_add_pair(complexObject, "boolField", json_new_bool(0x00));
json_add_pair(complexObject, "numberField", json_new_number(987.654));
json_add_pair(complexObject, "stringField",
json_new_string("对象中的字符串"));
// 对象中嵌套数组
JsonNode *objArray = json_new_array();
json_add_child(objArray, json_new_string("第一项"));
json_add_child(objArray, json_new_string("第二项"));
json_add_child(objArray, json_new_string("第三项"));
json_add_pair(complexObject, "nestedArray", objArray);
// 对象中嵌套对象
JsonNode *nestedObj = json_new_object();
json_add_pair(nestedObj, "innerKey1", json_new_number(111));
json_add_pair(nestedObj, "innerKey2", json_new_number(222));
json_add_pair(nestedObj, "innerKey3", json_new_number(333));
json_add_pair(complexObject, "nestedObject", nestedObj);
// 多层嵌套
JsonNode *deepNested = json_new_object();
JsonNode *deepArray = json_new_array();
json_add_child(deepArray, json_new_string("深度1"));
JsonNode *deeperArray = json_new_array();
json_add_child(deeperArray, json_new_string("深度2"));
JsonNode *deepestObj = json_new_object();
json_add_pair(deepestObj, "finalKey", json_new_string("最终值"));
json_add_child(deeperArray, deepestObj);
json_add_child(deepArray, deeperArray);
json_add_pair(deepNested, "deepArray", deepArray);
json_add_pair(complexObject, "deepNested", deepNested);
json_add_pair(root, "complexObject", complexObject);
// 特殊字符测试
JsonNode *specialChars = json_new_object();
json_add_pair(specialChars, "quotes", json_new_string("\"双引号\""));
json_add_pair(specialChars, "backslash", json_new_string("反斜杠\\"));
json_add_pair(specialChars, "newline", json_new_string("换行\n符"));
json_add_pair(specialChars, "tab", json_new_string("制表\t符"));
json_add_pair(specialChars, "unicode", json_new_string("Unicode: \u03A9 Ω"));
json_add_pair(root, "specialCharacters", specialChars);
// 大型数组测试
JsonNode *largeArray = json_new_array();
for (int i = 0; i < 10; i++) {
JsonNode *item = json_new_object();
json_add_pair(item, "index", json_new_number(i));
json_add_pair(item, "square", json_new_number(i * i));
json_add_pair(item, "cube", json_new_number(i * i * i));
json_add_child(largeArray, item);
}
json_add_pair(root, "largeArray", largeArray);
JsonNode *matrix2d = json_new_array();
for (int i = 0; i < 3; i++) {
JsonNode *row = json_new_array();
for (int j = 0; j < 3; j++) {
json_add_child(row, json_new_number(i * 3 + j + 1)); // 填充1-9的数字
}
json_add_child(matrix2d, row);
}
json_add_pair(root, "matrix2d", matrix2d);
// 打印结果
printf("复杂JSON测试输出:\n");
json_print_pretty(root);
// 释放内存
json_free(root);
}
void test1() {
// 创建根对象 - 直接使用 json_new_node
JsonNode *root = json_new_node(JSON_OBJECT);
// 添加基本类型 - 直接使用 json_new_node
json_add_pair(root, "nullValue", json_new_node(JSON_NULL));
json_add_pair(root, "trueValue", json_new_node(JSON_BOOL, 1)); // 1 表示 true
json_add_pair(root, "falseValue",
json_new_node(JSON_BOOL, 0)); // 0 表示 false
json_add_pair(root, "integer", json_new_node(JSON_NUMBER, 42.0));
json_add_pair(root, "float", json_new_node(JSON_NUMBER, 3.14159));
json_add_pair(root, "string", json_new_node(JSON_STRING, "Hello, 世界!"));
// 创建空数组和空对象
json_add_pair(root, "emptyArray", json_new_node(JSON_ARRAY));
json_add_pair(root, "emptyObject", json_new_node(JSON_OBJECT));
// 创建复杂数组
JsonNode *complexArray = json_new_node(JSON_ARRAY);
json_add_child(complexArray, json_new_node(JSON_NULL));
json_add_child(complexArray, json_new_node(JSON_BOOL, 1));
json_add_child(complexArray, json_new_node(JSON_NUMBER, 123.456));
json_add_child(complexArray, json_new_node(JSON_STRING, "数组中的字符串"));
// 数组中嵌套数组
JsonNode *nestedArray = json_new_node(JSON_ARRAY);
json_add_child(nestedArray, json_new_node(JSON_NUMBER, 1.0));
json_add_child(nestedArray, json_new_node(JSON_NUMBER, 2.0));
json_add_child(nestedArray, json_new_node(JSON_NUMBER, 3.0));
json_add_child(complexArray, nestedArray);
// 数组中嵌套对象
JsonNode *arrayObj = json_new_node(JSON_OBJECT);
json_add_pair(arrayObj, "key1", json_new_node(JSON_STRING, "value1"));
json_add_pair(arrayObj, "key2", json_new_node(JSON_STRING, "value2"));
json_add_child(complexArray, arrayObj);
json_add_pair(root, "complexArray", complexArray);
// 创建复杂对象
JsonNode *complexObject = json_new_node(JSON_OBJECT);
// 对象中包含各种类型
json_add_pair(complexObject, "nullField", json_new_node(JSON_NULL));
json_add_pair(complexObject, "boolField", json_new_node(JSON_BOOL, 0));
json_add_pair(complexObject, "numberField",
json_new_node(JSON_NUMBER, 987.654));
json_add_pair(complexObject, "stringField",
json_new_node(JSON_STRING, "对象中的字符串"));
// 对象中嵌套数组
JsonNode *objArray = json_new_node(JSON_ARRAY);
json_add_child(objArray, json_new_node(JSON_STRING, "第一项"));
json_add_child(objArray, json_new_node(JSON_STRING, "第二项"));
json_add_child(objArray, json_new_node(JSON_STRING, "第三项"));
json_add_pair(complexObject, "nestedArray", objArray);
// 对象中嵌套对象
JsonNode *nestedObj = json_new_node(JSON_OBJECT);
json_add_pair(nestedObj, "innerKey1", json_new_node(JSON_NUMBER, 111.0));
json_add_pair(nestedObj, "innerKey2", json_new_node(JSON_NUMBER, 222.0));
json_add_pair(nestedObj, "innerKey3", json_new_node(JSON_NUMBER, 333.0));
json_add_pair(complexObject, "nestedObject", nestedObj);
// 多层嵌套
JsonNode *deepNested = json_new_node(JSON_OBJECT);
JsonNode *deepArray = json_new_node(JSON_ARRAY);
json_add_child(deepArray, json_new_node(JSON_STRING, "深度1"));
JsonNode *deeperArray = json_new_node(JSON_ARRAY);
json_add_child(deeperArray, json_new_node(JSON_STRING, "深度2"));
JsonNode *deepestObj = json_new_node(JSON_OBJECT);
json_add_pair(deepestObj, "finalKey", json_new_node(JSON_STRING, "最终值"));
json_add_child(deeperArray, deepestObj);
json_add_child(deepArray, deeperArray);
json_add_pair(deepNested, "deepArray", deepArray);
json_add_pair(complexObject, "deepNested", deepNested);
json_add_pair(root, "complexObject", complexObject);
// 特殊字符测试
JsonNode *specialChars = json_new_node(JSON_OBJECT);
json_add_pair(specialChars, "quotes",
json_new_node(JSON_STRING, "\"双引号\""));
json_add_pair(specialChars, "backslash",
json_new_node(JSON_STRING, "反斜杠\\"));
json_add_pair(specialChars, "newline",
json_new_node(JSON_STRING, "换行\n符"));
json_add_pair(specialChars, "tab", json_new_node(JSON_STRING, "制表\t符"));
json_add_pair(specialChars, "unicode",
json_new_node(JSON_STRING, "Unicode: \u03A9 Ω"));
json_add_pair(root, "specialCharacters", specialChars);
// 大型数组测试
JsonNode *largeArray = json_new_node(JSON_ARRAY);
for (int i = 0; i < 10; i++) {
JsonNode *item = json_new_node(JSON_OBJECT);
json_add_pair(item, "index", json_new_node(JSON_NUMBER, (double)i));
json_add_pair(item, "square", json_new_node(JSON_NUMBER, (double)(i * i)));
json_add_pair(item, "cube",
json_new_node(JSON_NUMBER, (double)(i * i * i)));
json_add_child(largeArray, item);
}
json_add_pair(root, "largeArray", largeArray);
JsonNode *matrix2d = json_new_node(JSON_ARRAY);
for (int i = 0; i < 3; i++) {
JsonNode *row = json_new_node(JSON_ARRAY);
for (int j = 0; j < 3; j++) {
json_add_child(row, json_new_node(JSON_NUMBER, (double)(i * 3 + j + 1)));
}
json_add_child(matrix2d, row);
}
json_add_pair(root, "matrix2d", matrix2d);
// 打印结果
printf("直接使用 json_new_node 创建的JSON测试输出:\n");
json_print_pretty(root);
// 释放内存
json_free(root);
}
int main(void) {
test0();
test1();
}
这个代码已经完成了json的构建和打印功能了,请帮我完成json的解析功能