optimize json module

This commit is contained in:
shuxin   zheng 2021-03-28 00:02:43 +08:00
parent c4993fd694
commit 9b835c8e8a
9 changed files with 150 additions and 7 deletions

View File

@ -287,6 +287,22 @@ void acl_json_building(ACL_JSON *json, size_t length,
ACL_ITER iter; ACL_ITER iter;
ACL_JSON_NODE *node, *prev; ACL_JSON_NODE *node, *prev;
ACL_VSTRING *buf = acl_vstring_alloc(256); ACL_VSTRING *buf = acl_vstring_alloc(256);
ACL_RING *ring_ptr = acl_ring_succ(&json->root->children);
/* 为了兼容历史的BUG所以此处只能如此处理了--zsx, 2021.3.27 */
if (ring_ptr == &json->root->children) {
if (json->root->left_ch == 0) {
json->root->left_ch = '{';
json->root->right_ch = '}';
}
} else {
node = acl_ring_to_appl(ring_ptr, ACL_JSON_NODE, node);
if (node->left_ch == 0 && json->root->left_ch == 0) {
json->root->left_ch = '{';
json->root->right_ch = '}';
}
}
if (json->root->left_ch > 0) if (json->root->left_ch > 0)
ACL_VSTRING_ADDCH(buf, json->root->left_ch); ACL_VSTRING_ADDCH(buf, json->root->left_ch);
@ -438,14 +454,9 @@ ACL_VSTRING *acl_json_build(ACL_JSON *json, ACL_VSTRING *buf)
json->root->left_ch = '{'; json->root->left_ch = '{';
json->root->right_ch = '}'; json->root->right_ch = '}';
} }
} else if (json->root->type != ACL_JSON_T_ARRAY) { } else {
node = acl_ring_to_appl(ring_ptr, ACL_JSON_NODE, node); node = acl_ring_to_appl(ring_ptr, ACL_JSON_NODE, node);
if (node != NULL && node->type == ACL_JSON_T_ARRAY if (node->left_ch == 0 && json->root->left_ch == 0) {
&& acl_ring_size(&json->root->children) == 1) {
json->root->left_ch = 0;
json->root->right_ch = 0;
} else {
json->root->left_ch = '{'; json->root->left_ch = '{';
json->root->right_ch = '}'; json->root->right_ch = '}';
} }

View File

@ -0,0 +1,3 @@
base_path = ../../..
PROG = json
include ../../Makefile.in

View File

@ -0,0 +1,74 @@
#include "stdafx.h"
#include <getopt.h>
static void usage(const char* procname) {
printf("usage: %s -h [help[ -f json_file\r\n", procname);
}
int main(int argc, char* argv[]) {
acl::string file;
int ch;
while ((ch = getopt(argc, argv, "hf:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'f':
file = optarg;
break;
default:
break;
}
}
if (file.empty()) {
usage(argv[0]);
return 1;
}
acl::string buf;
if (!acl::ifstream::load(file, &buf)) {
printf("load %s error %s\r\n", file.c_str(), acl::last_serror());
return 1;
}
acl::json json(buf);
if (!json.finish()) {
printf("invalid json: %s\r\n", buf.c_str());
return 1;
}
printf("parse json ok: %s\r\n", json.to_string().c_str());
json.reset();
json.get_root().add_array(true)
.add_array_text("name1")
.add_array_text("name2")
.add_array_text("name3");
printf("build json: %s\r\n", json.to_string().c_str());
json.reset();
json.get_root().add_text("name1", "value1")
.add_text("name2", "value2")
.add_text("name3", "value3")
.add_number("name4", 100);
printf("build json: %s\r\n", json.to_string().c_str());
json.reset();
json.get_root().add_text("name1", "value1");
printf("build json: %s\r\n", json.to_string().c_str());
json.reset();
json.get_root().add_child(false, true)
.add_text("name1", "value1")
.add_text("name2", "value2")
.add_text("name3", "value3");
printf("build json: %s\r\n", json.to_string().c_str());
json.reset();
json.get_root().add_child(false, true)
.add_text("name1", "value1");
printf("build json: %s\r\n", json.to_string().c_str());
return 0;
}

View File

@ -0,0 +1 @@
[ "name1", "name2", "name3" ]

View File

@ -0,0 +1 @@
[ { "name1": "value1" }, { "name2": "value2"}, {"name3": "value3"} ]

View File

@ -0,0 +1 @@
[ 1, 2, 3, 4, 5, 6 ]

View File

@ -0,0 +1,8 @@
// stdafx.cpp : 只包括标准包含文件的源文件
// json.pch 将成为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: 在 STDAFX.H 中
//引用任何所需的附加头文件,而不是在此文件中引用

View File

@ -0,0 +1,41 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是常用但不常更改的项目特定的包含文件
//
#pragma once
//#include <iostream>
//#include <tchar.h>
// TODO: 在此处引用程序要求的附加头文件
#include "acl_cpp/lib_acl.hpp"
#include "lib_acl.h"
#ifdef WIN32
#include <io.h>
#define snprintf _snprintf
#endif
#ifdef DEBUG
// 以下宏定义用来帮助检查变参中的参数类型是否合法
#undef logger
#define logger printf
#undef logger_error
#define logger_error printf
#undef logger_warn
#define logger_warn printf
#undef logger_fatal
#define logger_fatal printf
#undef logger_panic
#define logger_panic printf
extern void __attribute__((format(printf,3,4))) \
dummy_debug(int, int, const char*, ...);
#undef logger_debug
#define logger_debug dummy_debug
#endif

View File

@ -0,0 +1,3 @@
#!/bin/sh
valgrind --tool=memcheck --leak-check=yes -v ./json