Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
87a204ed
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,发现更多精彩内容 >>
提交
87a204ed
编写于
3月 14, 2017
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
parse_functions: Return booleans instead of pointers
上级
c9739c59
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
41 addition
and
48 deletion
+41
-48
cJSON.c
cJSON.c
+30
-37
tests/parse_array.c
tests/parse_array.c
+2
-2
tests/parse_number.c
tests/parse_number.c
+1
-1
tests/parse_object.c
tests/parse_object.c
+2
-2
tests/parse_string.c
tests/parse_string.c
+2
-2
tests/parse_value.c
tests/parse_value.c
+1
-1
tests/print_array.c
tests/print_array.c
+1
-1
tests/print_object.c
tests/print_object.c
+1
-1
tests/print_value.c
tests/print_value.c
+1
-1
未找到文件。
cJSON.c
浏览文件 @
87a204ed
...
...
@@ -202,7 +202,7 @@ typedef struct
#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
/* Parse the input text to generate a number, and populate the result into item. */
static
c
onst
unsigned
char
*
parse_number
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
)
static
c
JSON_bool
parse_number
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
)
{
double
number
=
0
;
unsigned
char
*
after_end
=
NULL
;
...
...
@@ -212,7 +212,7 @@ static const unsigned char *parse_number(cJSON * const item, parse_buffer * cons
if
((
input_buffer
==
NULL
)
||
(
input_buffer
->
content
==
NULL
))
{
return
NULL
;
return
false
;
}
/* copy the number into a temporary buffer and replace '.' with the decimal point
...
...
@@ -253,7 +253,7 @@ loop_end:
number
=
strtod
((
const
char
*
)
number_c_string
,
(
char
**
)
&
after_end
);
if
(
number_c_string
==
after_end
)
{
return
NULL
;
/* parse_error */
return
false
;
/* parse_error */
}
item
->
valuedouble
=
number
;
...
...
@@ -275,7 +275,7 @@ loop_end:
item
->
type
=
cJSON_Number
;
input_buffer
->
offset
+=
(
size_t
)(
after_end
-
number_c_string
);
return
buffer_at_offset
(
input_buffer
)
;
return
true
;
}
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
...
...
@@ -662,7 +662,7 @@ fail:
}
/* Parse the input text into an unescaped cinput, and populate item. */
static
c
onst
unsigned
char
*
parse_string
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
static
c
JSON_bool
parse_string
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
{
const
unsigned
char
*
input_pointer
=
buffer_at_offset
(
input_buffer
)
+
1
;
const
unsigned
char
*
input_end
=
buffer_at_offset
(
input_buffer
)
+
1
;
...
...
@@ -776,7 +776,7 @@ static const unsigned char *parse_string(cJSON * const item, parse_buffer * cons
input_buffer
->
offset
=
(
size_t
)
(
input_end
-
input_buffer
->
content
);
input_buffer
->
offset
++
;
return
buffer_at_offset
(
input_buffer
)
;
return
true
;
fail:
if
(
output
!=
NULL
)
...
...
@@ -784,7 +784,7 @@ fail:
hooks
->
deallocate
(
output
);
}
return
NULL
;
return
false
;
}
/* Render the cstring provided to an escaped version that can be printed. */
...
...
@@ -916,11 +916,11 @@ static cJSON_bool print_string(const cJSON * const item, printbuffer * const p,
}
/* Predeclare these prototypes. */
static
c
onst
unsigned
char
*
parse_value
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
ep
,
const
internal_hooks
*
const
hooks
);
static
c
JSON_bool
parse_value
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
ep
,
const
internal_hooks
*
const
hooks
);
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
c
onst
unsigned
char
*
parse_array
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
ep
,
const
internal_hooks
*
const
hooks
);
static
c
JSON_bool
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
c
onst
unsigned
char
*
parse_object
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
ep
,
const
internal_hooks
*
const
hooks
);
static
c
JSON_bool
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 */
...
...
@@ -980,13 +980,14 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return
buffer
.
length
=
strlen
((
const
char
*
)
value
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
end
=
parse_value
(
item
,
buffer_skip_whitespace
(
&
buffer
),
error_pointer
,
&
global_hooks
);
if
(
end
==
NULL
)
if
(
!
parse_value
(
item
,
buffer_skip_whitespace
(
&
buffer
),
error_pointer
,
&
global_hooks
))
{
/* parse failure. ep is set. */
goto
fail
;
}
end
=
buffer_at_offset
(
&
buffer
);
/* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
if
(
require_null_terminated
)
{
...
...
@@ -1125,12 +1126,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const i
}
/* Parser core - when encountering text, process appropriately. */
static
c
onst
unsigned
char
*
parse_value
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
static
c
JSON_bool
parse_value
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
{
const
unsigned
char
*
content_pointer
=
NULL
;
if
((
input_buffer
==
NULL
)
||
(
input_buffer
->
content
==
NULL
))
{
return
NULL
;
/* no input */
return
false
;
/* no input */
}
/* parse the different types of values */
...
...
@@ -1139,14 +1139,14 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
{
item
->
type
=
cJSON_NULL
;
input_buffer
->
offset
+=
4
;
return
buffer_at_offset
(
input_buffer
)
;
return
true
;
}
/* false */
if
(
can_read
(
input_buffer
,
5
)
&&
(
strncmp
((
const
char
*
)
buffer_at_offset
(
input_buffer
),
"false"
,
5
)
==
0
))
{
item
->
type
=
cJSON_False
;
input_buffer
->
offset
+=
5
;
return
buffer_at_offset
(
input_buffer
)
;
return
true
;
}
/* true */
if
(
can_read
(
input_buffer
,
4
)
&&
(
strncmp
((
const
char
*
)
buffer_at_offset
(
input_buffer
),
"true"
,
4
)
==
0
))
...
...
@@ -1154,12 +1154,12 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
item
->
type
=
cJSON_True
;
item
->
valueint
=
1
;
input_buffer
->
offset
+=
4
;
return
buffer_at_offset
(
input_buffer
)
;
return
true
;
}
/* string */
if
(
can_access_at_index
(
input_buffer
,
0
)
&&
(
buffer_at_offset
(
input_buffer
)[
0
]
==
'\"'
))
{
return
content_pointer
=
parse_string
(
item
,
input_buffer
,
error_pointer
,
hooks
);
return
parse_string
(
item
,
input_buffer
,
error_pointer
,
hooks
);
}
/* number */
if
(
can_access_at_index
(
input_buffer
,
0
)
&&
((
buffer_at_offset
(
input_buffer
)[
0
]
==
'-'
)
||
((
buffer_at_offset
(
input_buffer
)[
0
]
>=
'0'
)
&&
(
buffer_at_offset
(
input_buffer
)[
0
]
<=
'9'
))))
...
...
@@ -1174,14 +1174,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
,
input_buffer
,
error_pointer
,
hooks
);
if
(
content_pointer
==
NULL
)
{
return
NULL
;
}
input_buffer
->
offset
=
(
size_t
)(
content_pointer
-
input_buffer
->
content
);
return
buffer_at_offset
(
input_buffer
);
return
parse_object
(
item
,
input_buffer
,
error_pointer
,
hooks
);
}
/* failure. */
...
...
@@ -1198,7 +1191,7 @@ static const unsigned char *parse_value(cJSON * const item, parse_buffer * cons
*
error_pointer
=
input_buffer
->
content
;
}
return
NULL
;
return
false
;
}
/* Render a value to text. */
...
...
@@ -1280,7 +1273,7 @@ static cJSON_bool print_value(const cJSON * const item, const size_t depth, cons
}
/* Build an array from input text. */
static
c
onst
unsigned
char
*
parse_array
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
static
c
JSON_bool
parse_array
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
{
cJSON
*
head
=
NULL
;
/* head of the linked list */
cJSON
*
current_item
=
NULL
;
...
...
@@ -1337,7 +1330,7 @@ static const unsigned char *parse_array(cJSON * const item, parse_buffer * const
/* parse next value */
input_buffer
->
offset
++
;
buffer_skip_whitespace
(
input_buffer
);
if
(
parse_value
(
current_item
,
input_buffer
,
error_pointer
,
hooks
)
==
NULL
)
if
(
!
parse_value
(
current_item
,
input_buffer
,
error_pointer
,
hooks
)
)
{
goto
fail
;
/* failed to parse value */
}
...
...
@@ -1357,7 +1350,7 @@ success:
input_buffer
->
offset
++
;
return
buffer_at_offset
(
input_buffer
)
;
return
true
;
fail:
if
(
head
!=
NULL
)
...
...
@@ -1365,7 +1358,7 @@ fail:
cJSON_Delete
(
head
);
}
return
NULL
;
return
false
;
}
/* Render an array to text */
...
...
@@ -1429,7 +1422,7 @@ static cJSON_bool print_array(const cJSON * const item, const size_t depth, cons
}
/* Build an object from the text. */
static
c
onst
unsigned
char
*
parse_object
(
cJSON
*
const
item
,
parse_buffer
*
const
input_buffer
,
const
unsigned
char
**
const
error_pointer
,
const
internal_hooks
*
const
hooks
)
static
c
JSON_bool
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
;
...
...
@@ -1484,7 +1477,7 @@ static const unsigned char *parse_object(cJSON * const item, parse_buffer * cons
/* parse the name of the child */
input_buffer
->
offset
++
;
buffer_skip_whitespace
(
input_buffer
);
if
(
parse_string
(
current_item
,
input_buffer
,
error_pointer
,
hooks
)
==
NULL
)
if
(
!
parse_string
(
current_item
,
input_buffer
,
error_pointer
,
hooks
)
)
{
goto
fail
;
/* faile to parse name */
}
...
...
@@ -1503,7 +1496,7 @@ static const unsigned char *parse_object(cJSON * const item, parse_buffer * cons
/* parse the value */
input_buffer
->
offset
++
;
buffer_skip_whitespace
(
input_buffer
);
if
(
parse_value
(
current_item
,
input_buffer
,
error_pointer
,
hooks
)
==
NULL
)
if
(
!
parse_value
(
current_item
,
input_buffer
,
error_pointer
,
hooks
)
)
{
goto
fail
;
/* failed to parse value */
}
...
...
@@ -1522,7 +1515,7 @@ success:
item
->
child
=
head
;
input_buffer
->
offset
++
;
return
buffer_at_offset
(
input_buffer
)
;
return
true
;
fail:
if
(
head
!=
NULL
)
...
...
@@ -1530,7 +1523,7 @@ fail:
cJSON_Delete
(
head
);
}
return
NULL
;
return
false
;
}
/* Render an object to text. */
...
...
tests/parse_array.c
浏览文件 @
87a204ed
...
...
@@ -51,7 +51,7 @@ static void assert_not_array(const char *json)
buffer
.
length
=
strlen
(
json
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
TEST_ASSERT_
NULL
(
parse_array
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
));
TEST_ASSERT_
FALSE
(
parse_array
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
));
assert_is_invalid
(
item
);
}
...
...
@@ -62,7 +62,7 @@ static void assert_parse_array(const char *json)
buffer
.
length
=
strlen
(
json
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
TEST_ASSERT_
NOT_NULL
(
parse_array
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
));
TEST_ASSERT_
TRUE
(
parse_array
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
));
assert_is_array
(
item
);
}
...
...
tests/parse_number.c
浏览文件 @
87a204ed
...
...
@@ -50,7 +50,7 @@ static void assert_parse_number(const char *string, int integer, double real)
buffer
.
length
=
strlen
(
string
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
TEST_ASSERT_
NOT_NULL
(
parse_number
(
item
,
&
buffer
));
TEST_ASSERT_
TRUE
(
parse_number
(
item
,
&
buffer
));
assert_is_number
(
item
);
TEST_ASSERT_EQUAL_INT
(
integer
,
item
->
valueint
);
TEST_ASSERT_EQUAL_DOUBLE
(
real
,
item
->
valuedouble
);
...
...
tests/parse_object.c
浏览文件 @
87a204ed
...
...
@@ -59,7 +59,7 @@ static void assert_not_object(const char *json)
parsebuffer
.
length
=
strlen
(
json
)
+
sizeof
(
""
);
parsebuffer
.
offset
=
0
;
TEST_ASSERT_
NULL
(
parse_object
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
));
TEST_ASSERT_
FALSE
(
parse_object
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
));
assert_is_invalid
(
item
);
reset
(
item
);
}
...
...
@@ -71,7 +71,7 @@ static void assert_parse_object(const char *json)
parsebuffer
.
length
=
strlen
(
json
)
+
sizeof
(
""
);
parsebuffer
.
offset
=
0
;
TEST_ASSERT_
NOT_NULL
(
parse_object
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
));
TEST_ASSERT_
TRUE
(
parse_object
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
));
assert_is_object
(
item
);
}
...
...
tests/parse_string.c
浏览文件 @
87a204ed
...
...
@@ -52,7 +52,7 @@ static void assert_parse_string(const char *string, const char *expected)
buffer
.
length
=
strlen
(
string
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
TEST_ASSERT_
NOT_NULL
_MESSAGE
(
parse_string
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
),
"Couldn't parse string."
);
TEST_ASSERT_
TRUE
_MESSAGE
(
parse_string
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
),
"Couldn't parse string."
);
assert_is_string
(
item
);
TEST_ASSERT_EQUAL_STRING_MESSAGE
(
expected
,
item
->
valuestring
,
"The parsed result isn't as expected."
);
global_hooks
.
deallocate
(
item
->
valuestring
);
...
...
@@ -66,7 +66,7 @@ static void assert_not_parse_string(const char * const string)
buffer
.
length
=
strlen
(
string
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
TEST_ASSERT_
NULL
_MESSAGE
(
parse_string
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
),
"Malformed string should not be accepted."
);
TEST_ASSERT_
FALSE
_MESSAGE
(
parse_string
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
),
"Malformed string should not be accepted."
);
assert_is_invalid
(
item
);
}
...
...
tests/parse_value.c
浏览文件 @
87a204ed
...
...
@@ -48,7 +48,7 @@ static void assert_parse_value(const char *string, int type)
buffer
.
content
=
(
const
unsigned
char
*
)
string
;
buffer
.
length
=
strlen
(
string
)
+
sizeof
(
""
);
buffer
.
offset
=
0
;
TEST_ASSERT_
NOT_NULL
(
parse_value
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
));
TEST_ASSERT_
TRUE
(
parse_value
(
item
,
&
buffer
,
&
error_pointer
,
&
global_hooks
));
assert_is_value
(
item
,
type
);
}
...
...
tests/print_array.c
浏览文件 @
87a204ed
...
...
@@ -53,7 +53,7 @@ static void assert_print_array(const char * const expected, const char * const i
unformatted_buffer
.
noalloc
=
true
;
memset
(
item
,
0
,
sizeof
(
item
));
TEST_ASSERT_
NOT_NULL
_MESSAGE
(
parse_array
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
),
"Failed to parse array."
);
TEST_ASSERT_
TRUE
_MESSAGE
(
parse_array
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
),
"Failed to parse array."
);
TEST_ASSERT_TRUE_MESSAGE
(
print_array
(
item
,
0
,
false
,
&
unformatted_buffer
,
&
global_hooks
),
"Failed to print unformatted string."
);
TEST_ASSERT_EQUAL_STRING_MESSAGE
(
input
,
printed_unformatted
,
"Unformatted array is not correct."
);
...
...
tests/print_object.c
浏览文件 @
87a204ed
...
...
@@ -54,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
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
),
"Failed to parse object."
);
TEST_ASSERT_
TRUE
_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."
);
...
...
tests/print_value.c
浏览文件 @
87a204ed
...
...
@@ -46,7 +46,7 @@ static void assert_print_value(const char *input)
memset
(
item
,
0
,
sizeof
(
item
));
TEST_ASSERT_
NOT_NULL
_MESSAGE
(
parse_value
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
),
"Failed to parse value."
);
TEST_ASSERT_
TRUE
_MESSAGE
(
parse_value
(
item
,
&
parsebuffer
,
&
error_pointer
,
&
global_hooks
),
"Failed to parse value."
);
TEST_ASSERT_TRUE_MESSAGE
(
print_value
(
item
,
0
,
false
,
&
buffer
,
&
global_hooks
),
"Failed to print value."
);
TEST_ASSERT_EQUAL_STRING_MESSAGE
(
input
,
buffer
.
buffer
,
"Printed value is not as expected."
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录