Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
fef1c45b
T
Third Party CJSON
项目概览
OpenHarmony
/
Third Party CJSON
大约 1 年 前同步成功
通知
6
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party CJSON
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fef1c45b
编写于
3月 14, 2017
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
parse_object: Use parse_buffer
上级
033a5425
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
54 addition
and
29 deletion
+54
-29
cJSON.c
cJSON.c
+35
-26
tests/parse_object.c
tests/parse_object.c
+12
-2
tests/print_object.c
tests/print_object.c
+7
-1
未找到文件。
cJSON.c
浏览文件 @
fef1c45b
...
...
@@ -900,7 +900,7 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * const
static
cJSON_bool
print_value
(
const
cJSON
*
const
item
,
const
size_t
depth
,
const
cJSON_bool
format
,
printbuffer
*
const
output_buffer
,
const
internal_hooks
*
const
hooks
);
static
const
unsigned
char
*
parse_array
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
ep
,
const
internal_hooks
*
const
hooks
);
static
cJSON_bool
print_array
(
const
cJSON
*
const
item
,
const
size_t
depth
,
const
cJSON_bool
format
,
printbuffer
*
const
output_buffer
,
const
internal_hooks
*
const
hooks
);
static
const
unsigned
char
*
parse_object
(
cJSON
*
const
item
,
const
unsigned
char
*
input
,
const
unsigned
char
**
const
ep
,
const
internal_hooks
*
const
hooks
);
static
const
unsigned
char
*
parse_object
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
ep
,
const
internal_hooks
*
const
hooks
);
static
cJSON_bool
print_object
(
const
cJSON
*
const
item
,
const
size_t
depth
,
const
cJSON_bool
format
,
printbuffer
*
const
output_buffer
,
const
internal_hooks
*
const
hooks
);
/* Utility to jump whitespace and cr/lf */
...
...
@@ -1168,7 +1168,7 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
/* object */
if
(
can_access_at_index
(
input_buffer
,
0
)
&&
(
buffer_at_offset
(
input_buffer
)[
0
]
==
'{'
))
{
content_pointer
=
parse_object
(
item
,
buffer_at_offset
(
input_buffer
)
,
error_pointer
,
hooks
);
content_pointer
=
parse_object
(
item
,
input_buffer
,
error_pointer
,
hooks
);
if
(
content_pointer
==
NULL
)
{
return
NULL
;
...
...
@@ -1423,26 +1423,35 @@ static cJSON_bool print_array(const cJSON * const item, const size_t depth, cons
}
/* Build an object from the text. */
static
const
unsigned
char
*
parse_object
(
cJSON
*
const
item
,
const
unsigned
char
*
input
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
static
const
unsigned
char
*
parse_object
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
{
cJSON
*
head
=
NULL
;
/* linked list head */
cJSON
*
current_item
=
NULL
;
parse_buffer
input_buffer
;
const
unsigned
char
*
content_pointer
=
NULL
;
if
(
*
input
!=
'{'
)
if
(
cannot_access_at_index
(
input_buffer
,
0
)
||
(
buffer_at_offset
(
input_buffer
)[
0
]
!=
'{'
)
)
{
*
error_pointer
=
input
;
*
error_pointer
=
buffer_at_offset
(
input_buffer
)
;
goto
fail
;
/* not an object */
}
input
=
skip_whitespace
(
input
+
1
);
if
(
*
input
==
'}'
)
input_buffer
->
offset
++
;
buffer_skip_whitespace
(
input_buffer
);
if
(
can_access_at_index
(
input_buffer
,
0
)
&&
(
buffer_at_offset
(
input_buffer
)[
0
]
==
'}'
))
{
goto
success
;
/* empty object */
}
/* check if we skipped to the end of the buffer */
if
(
cannot_access_at_index
(
input_buffer
,
0
))
{
input_buffer
->
offset
--
;
*
error_pointer
=
buffer_at_offset
(
input_buffer
);
goto
fail
;
}
/* step back to character in front of the first element */
input
--
;
input
_buffer
->
offset
--
;
/* loop through the comma separated array elements */
do
{
...
...
@@ -1468,41 +1477,40 @@ static const unsigned char *parse_object(cJSON * const item, const unsigned char
}
/* parse the name of the child */
input
=
skip_whitespace
(
input
+
1
)
;
input
=
parse_string
(
current_item
,
input
,
error_pointer
,
hooks
);
input
=
skip_whitespace
(
input
);
if
(
input
==
NULL
)
input
_buffer
->
offset
++
;
buffer_skip_whitespace
(
input_buffer
);
content_pointer
=
parse_string
(
current_item
,
buffer_at_offset
(
input_buffer
),
error_pointer
,
hooks
);
if
(
content_pointer
==
NULL
)
{
goto
fail
;
/* faile to parse name */
}
input_buffer
->
offset
=
(
size_t
)(
content_pointer
-
input_buffer
->
content
);
buffer_skip_whitespace
(
input_buffer
);
/* swap valuestring and string, because we parsed the name */
current_item
->
string
=
current_item
->
valuestring
;
current_item
->
valuestring
=
NULL
;
if
(
*
input
!=
':'
)
if
(
cannot_access_at_index
(
input_buffer
,
0
)
||
(
buffer_at_offset
(
input_buffer
)[
0
]
!=
':'
)
)
{
*
error_pointer
=
input
;
*
error_pointer
=
buffer_at_offset
(
input_buffer
)
;
goto
fail
;
/* invalid object */
}
/* parse the value */
input
=
skip_whitespace
(
input
+
1
);
input_buffer
.
content
=
(
const
unsigned
char
*
)
input
;
input_buffer
.
offset
=
0
;
input_buffer
.
length
=
strlen
((
const
char
*
)
input
)
+
sizeof
(
""
);
input
=
parse_value
(
current_item
,
&
input_buffer
,
error_pointer
,
hooks
);
input
=
skip_whitespace
(
input
);
if
(
input
==
NULL
)
input_buffer
->
offset
++
;
buffer_skip_whitespace
(
input_buffer
);
if
(
parse_value
(
current_item
,
input_buffer
,
error_pointer
,
hooks
)
==
NULL
)
{
goto
fail
;
/* failed to parse value */
}
buffer_skip_whitespace
(
input_buffer
);
}
while
(
*
input
==
','
);
while
(
can_access_at_index
(
input_buffer
,
0
)
&&
(
buffer_at_offset
(
input_buffer
)[
0
]
==
','
)
);
if
(
*
input
!=
'}'
)
if
(
cannot_access_at_index
(
input_buffer
,
0
)
||
(
buffer_at_offset
(
input_buffer
)[
0
]
!=
'}'
)
)
{
*
error_pointer
=
input
;
*
error_pointer
=
buffer_at_offset
(
input_buffer
)
;
goto
fail
;
/* expected end of object */
}
...
...
@@ -1510,7 +1518,8 @@ success:
item
->
type
=
cJSON_Object
;
item
->
child
=
head
;
return
input
+
1
;
input_buffer
->
offset
++
;
return
buffer_at_offset
(
input_buffer
);
fail:
if
(
head
!=
NULL
)
...
...
tests/parse_object.c
浏览文件 @
fef1c45b
...
...
@@ -54,14 +54,24 @@ static void assert_is_child(cJSON *child_item, const char *name, int type)
static
void
assert_not_object
(
const
char
*
json
)
{
TEST_ASSERT_NULL
(
parse_object
(
item
,
(
const
unsigned
char
*
)
json
,
&
error_pointer
,
&
global_hooks
));
parse_buffer
parsebuffer
;
parsebuffer
.
content
=
(
const
unsigned
char
*
)
json
;
parsebuffer
.
length
=
strlen
(
json
)
+
sizeof
(
""
);
parsebuffer
.
offset
=
0
;
TEST_ASSERT_NULL
(
parse_object
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
));
assert_is_invalid
(
item
);
reset
(
item
);
}
static
void
assert_parse_object
(
const
char
*
json
)
{
TEST_ASSERT_NOT_NULL
(
parse_object
(
item
,
(
const
unsigned
char
*
)
json
,
&
error_pointer
,
&
global_hooks
));
parse_buffer
parsebuffer
;
parsebuffer
.
content
=
(
const
unsigned
char
*
)
json
;
parsebuffer
.
length
=
strlen
(
json
)
+
sizeof
(
""
);
parsebuffer
.
offset
=
0
;
TEST_ASSERT_NOT_NULL
(
parse_object
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
));
assert_is_object
(
item
);
}
...
...
tests/print_object.c
浏览文件 @
fef1c45b
...
...
@@ -34,6 +34,12 @@ static void assert_print_object(const char * const expected, const char * const
printbuffer
formatted_buffer
;
printbuffer
unformatted_buffer
;
parse_buffer
parsebuffer
;
/* buffer for parsing */
parsebuffer
.
content
=
(
const
unsigned
char
*
)
input
;
parsebuffer
.
length
=
strlen
(
input
)
+
sizeof
(
""
);
parsebuffer
.
offset
=
0
;
/* buffer for formatted printing */
formatted_buffer
.
buffer
=
printed_formatted
;
...
...
@@ -48,7 +54,7 @@ static void assert_print_object(const char * const expected, const char * const
unformatted_buffer
.
noalloc
=
true
;
memset
(
item
,
0
,
sizeof
(
item
));
TEST_ASSERT_NOT_NULL_MESSAGE
(
parse_object
(
item
,
(
const
unsigned
char
*
)
input
,
&
error_pointer
,
&
global_hooks
),
"Failed to parse object."
);
TEST_ASSERT_NOT_NULL_MESSAGE
(
parse_object
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
),
"Failed to parse object."
);
TEST_ASSERT_TRUE_MESSAGE
(
print_object
(
item
,
0
,
false
,
&
unformatted_buffer
,
&
global_hooks
),
"Failed to print unformatted string."
);
TEST_ASSERT_EQUAL_STRING_MESSAGE
(
input
,
printed_unformatted
,
"Unformatted object is not correct."
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录