Skip to content

Commit

Permalink
conf json support comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Dec 1, 2023
1 parent fa3310d commit fc7d41d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/conf_io/conf_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ static ret_t conf_json_skip_spaces(json_parser_t* parser) {
static ret_t conf_json_parse_object(json_parser_t* parser) {
char c = '\0';

return_value_if_fail(conf_json_skip_spaces(parser) == RET_OK, RET_BAD_PARAMS);
conf_json_skip_all_comments(parser);
parser->cursor++;
while (parser->cursor < parser->size) {
return_value_if_fail(conf_json_skip_spaces(parser) == RET_OK, RET_BAD_PARAMS);

conf_json_skip_all_comments(parser);
if (parser->data[parser->cursor] == '}') {
parser->cursor++;
break;
Expand Down Expand Up @@ -143,7 +143,7 @@ static ret_t conf_json_parse_array(json_parser_t* parser) {
conf_node_t* node = NULL;

parser->cursor++;
conf_json_skip_spaces(parser);
conf_json_skip_all_comments(parser);
c = parser->data[parser->cursor];
if (c == ']') {
parser->cursor++;
Expand Down
18 changes: 18 additions & 0 deletions tests/conf_json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,21 @@ TEST(ConfJson, save_json_ex) {
conf_doc_destroy(doc);
str_reset(&str);
}

TEST(ConfJson, comment) {
const char* data = "\
{\
\"log_message\": {\
\"enable\": true, /*是否启用告警信息*/\
\"fields_count\": 4, /*告警信息中带的字段个数: 级别 | 时间 | 设备 | 信息*/\
\"fields_seperator\": \"|\", /*字段之间的分隔符*/\
\"max_rows\": 1000 /*告警信息最大行数*/\
}\
}";
conf_doc_t* doc = conf_doc_load_json(data, -1);
ASSERT_EQ(conf_doc_get_int(doc, "log_message.fields_count", 0), 4);
ASSERT_EQ(conf_doc_get_int(doc, "log_message.max_rows", 0), 1000);

conf_doc_destroy(doc);
}

0 comments on commit fc7d41d

Please sign in to comment.