Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
48c97985
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,发现更多精彩内容 >>
提交
48c97985
编写于
4月 30, 2017
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor cJSONUtils_ApplyPatch
上级
63db67bf
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
143 addition
and
110 deletion
+143
-110
cJSON_Utils.c
cJSON_Utils.c
+143
-110
未找到文件。
cJSON_Utils.c
浏览文件 @
48c97985
...
...
@@ -533,54 +533,100 @@ static cJSON_bool insert_item_in_array(cJSON *array, size_t which, cJSON *newite
enum
patch_operation
{
INVALID
,
ADD
,
REMOVE
,
REPLACE
,
MOVE
,
COPY
,
TEST
};
static
int
cJSONUtils_ApplyPatch
(
cJSON
*
object
,
cJSON
*
patch
)
static
enum
patch_operation
decode_patch_operation
(
const
cJSON
*
const
patch
)
{
cJSON
*
op
=
NULL
;
cJSON
*
path
=
NULL
;
cJSON
*
value
=
NULL
;
cJSON
*
parent
=
NULL
;
enum
patch_operation
opcode
=
INVALID
;
unsigned
char
*
parentptr
=
NULL
;
unsigned
char
*
childptr
=
NULL
;
cJSON
*
operation
=
cJSON_GetObjectItem
(
patch
,
"op"
);
if
(
!
cJSON_IsString
(
operation
))
{
return
INVALID
;
}
op
=
cJSON_GetObjectItem
(
patch
,
"op"
);
path
=
cJSON_GetObjectItem
(
patch
,
"path"
);
if
(
!
cJSON_IsString
(
op
)
||
!
cJSON_IsString
(
path
))
if
(
strcmp
(
operation
->
valuestring
,
"add"
)
==
0
)
{
/* malformed patch. */
return
2
;
return
ADD
;
}
/* decode operation */
if
(
!
strcmp
(
op
->
valuestring
,
"add"
))
if
(
strcmp
(
operation
->
valuestring
,
"remove"
)
==
0
)
{
opcode
=
ADD
;
return
REMOVE
;
}
else
if
(
!
strcmp
(
op
->
valuestring
,
"remove"
))
if
(
strcmp
(
operation
->
valuestring
,
"replace"
)
==
0
)
{
opcode
=
REMOV
E
;
return
REPLAC
E
;
}
else
if
(
!
strcmp
(
op
->
valuestring
,
"replace"
))
if
(
strcmp
(
operation
->
valuestring
,
"move"
)
==
0
)
{
opcode
=
REPLAC
E
;
return
MOV
E
;
}
else
if
(
!
strcmp
(
op
->
valuestring
,
"move"
))
if
(
strcmp
(
operation
->
valuestring
,
"copy"
)
==
0
)
{
opcode
=
MOVE
;
return
COPY
;
}
else
if
(
!
strcmp
(
op
->
valuestring
,
"copy"
))
if
(
strcmp
(
operation
->
valuestring
,
"test"
)
==
0
)
{
opcode
=
COPY
;
return
TEST
;
}
else
if
(
!
strcmp
(
op
->
valuestring
,
"test"
))
return
INVALID
;
}
/* overwrite and existing item with another one and free resources on the way */
static
void
overwrite_item
(
cJSON
*
const
root
,
const
cJSON
replacement
)
{
if
(
root
==
NULL
)
{
/* compare value: {...} with the given path */
return
cJSONUtils_Compare
(
cJSONUtils_GetPointer
(
object
,
path
->
valuestring
),
cJSON_GetObjectItem
(
patch
,
"value"
));
return
;
}
else
if
(
root
->
string
!=
NULL
)
{
cJSON_free
(
root
->
string
);
}
if
(
root
->
valuestring
!=
NULL
)
{
cJSON_free
(
root
->
valuestring
);
}
if
(
root
->
child
!=
NULL
)
{
cJSON_Delete
(
root
->
child
);
}
memcpy
(
root
,
&
replacement
,
sizeof
(
cJSON
));
}
static
int
cJSONUtils_ApplyPatch
(
cJSON
*
object
,
const
cJSON
*
patch
)
{
cJSON
*
path
=
NULL
;
cJSON
*
value
=
NULL
;
cJSON
*
parent
=
NULL
;
enum
patch_operation
opcode
=
INVALID
;
unsigned
char
*
parent_pointer
=
NULL
;
unsigned
char
*
child_pointer
=
NULL
;
int
status
=
0
;
path
=
cJSON_GetObjectItem
(
patch
,
"path"
);
if
(
!
cJSON_IsString
(
path
))
{
/* unknown opcode. */
return
3
;
/* malformed patch. */
status
=
2
;
goto
cleanup
;
}
opcode
=
decode_patch_operation
(
patch
);
if
(
opcode
==
INVALID
)
{
status
=
3
;
goto
cleanup
;
}
else
if
(
opcode
==
TEST
)
{
/* compare value: {...} with the given path */
status
=
cJSONUtils_Compare
(
cJSONUtils_GetPointer
(
object
,
path
->
valuestring
),
cJSON_GetObjectItem
(
patch
,
"value"
));
goto
cleanup
;
}
/* special case for replacing the root */
...
...
@@ -588,73 +634,47 @@ static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
{
if
(
opcode
==
REMOVE
)
{
/* remove possible children */
if
(
object
->
child
!=
NULL
)
{
cJSON_Delete
(
object
->
child
);
}
static
const
cJSON
invalid
=
{
NULL
,
NULL
,
NULL
,
cJSON_Invalid
,
NULL
,
0
,
0
,
NULL
};
/* remove other allocated resources */
if
(
object
->
string
!=
NULL
)
{
cJSON_free
(
object
->
string
);
}
if
(
object
->
valuestring
!=
NULL
)
{
cJSON_free
(
object
->
valuestring
);
}
/* make it invalid */
memset
(
object
,
'\0'
,
sizeof
(
cJSON
));
overwrite_item
(
object
,
invalid
);
return
0
;
status
=
0
;
goto
cleanup
;
}
if
((
opcode
==
REPLACE
)
||
(
opcode
==
ADD
))
{
/* remove possible children */
if
(
object
->
child
!=
NULL
)
{
cJSON_Delete
(
object
->
child
);
}
/* remove other allocated resources */
if
(
object
->
string
!=
NULL
)
{
cJSON_free
(
object
->
string
);
}
if
(
object
->
valuestring
!=
NULL
)
{
cJSON_free
(
object
->
valuestring
);
}
value
=
cJSON_GetObjectItem
(
patch
,
"value"
);
if
(
value
==
NULL
)
{
/* missing "value" for add/replace. */
return
7
;
status
=
7
;
goto
cleanup
;
}
value
=
cJSON_Duplicate
(
value
,
1
);
if
(
value
==
NULL
)
{
/* out of memory for add/replace. */
return
8
;
}
/* the string "value" isn't needed */
if
(
value
->
string
!=
NULL
)
{
cJSON_free
(
value
->
string
);
value
->
string
=
NULL
;
status
=
8
;
goto
cleanup
;
}
/* copy over the value object */
memcpy
(
object
,
value
,
sizeof
(
cJSON
));
overwrite_item
(
object
,
*
value
);
/* delete the duplicated value */
cJSON_free
(
value
);
value
=
NULL
;
return
0
;
/* the string "value" isn't needed */
if
(
object
->
string
!=
NULL
)
{
cJSON_free
(
object
->
string
);
object
->
string
=
NULL
;
}
status
=
0
;
goto
cleanup
;
}
}
...
...
@@ -664,13 +684,15 @@ static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
cJSON
*
old_item
=
cJSONUtils_PatchDetach
(
object
,
(
unsigned
char
*
)
path
->
valuestring
);
if
(
old_item
==
NULL
)
{
return
13
;
status
=
13
;
goto
cleanup
;
}
cJSON_Delete
(
old_item
);
if
(
opcode
==
REMOVE
)
{
/* For Remove, this job is done. */
return
0
;
status
=
0
;
goto
cleanup
;
}
}
...
...
@@ -678,10 +700,11 @@ static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
if
((
opcode
==
MOVE
)
||
(
opcode
==
COPY
))
{
cJSON
*
from
=
cJSON_GetObjectItem
(
patch
,
"from"
);
if
(
!
from
)
if
(
from
==
NULL
)
{
/* missing "from" for copy/move. */
return
4
;
status
=
4
;
goto
cleanup
;
}
if
(
opcode
==
MOVE
)
...
...
@@ -692,93 +715,103 @@ static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
{
value
=
cJSONUtils_GetPointer
(
object
,
from
->
valuestring
);
}
if
(
!
value
)
if
(
value
==
NULL
)
{
/* missing "from" for copy/move. */
return
5
;
status
=
5
;
goto
cleanup
;
}
if
(
opcode
==
COPY
)
{
value
=
cJSON_Duplicate
(
value
,
1
);
}
if
(
!
value
)
if
(
value
==
NULL
)
{
/* out of memory for copy/move. */
return
6
;
status
=
6
;
goto
cleanup
;
}
}
else
/* Add/Replace uses "value". */
{
value
=
cJSON_GetObjectItem
(
patch
,
"value"
);
if
(
!
value
)
if
(
value
==
NULL
)
{
/* missing "value" for add/replace. */
return
7
;
status
=
7
;
goto
cleanup
;
}
value
=
cJSON_Duplicate
(
value
,
1
);
if
(
!
value
)
if
(
value
==
NULL
)
{
/* out of memory for add/replace. */
return
8
;
status
=
8
;
goto
cleanup
;
}
}
/* Now, just add "value" to "path". */
/* split pointer in parent and child */
parent
pt
r
=
cJSONUtils_strdup
((
unsigned
char
*
)
path
->
valuestring
);
child
ptr
=
(
unsigned
char
*
)
strrchr
((
char
*
)
parentpt
r
,
'/'
);
if
(
child
ptr
)
parent
_pointe
r
=
cJSONUtils_strdup
((
unsigned
char
*
)
path
->
valuestring
);
child
_pointer
=
(
unsigned
char
*
)
strrchr
((
char
*
)
parent_pointe
r
,
'/'
);
if
(
child
_pointer
!=
NULL
)
{
*
childptr
++
=
'\0'
;
child_pointer
[
0
]
=
'\0'
;
child_pointer
++
;
}
parent
=
cJSONUtils_GetPointer
(
object
,
(
char
*
)
parent
pt
r
);
cJSONUtils_InplaceDecodePointerString
(
child
pt
r
);
parent
=
cJSONUtils_GetPointer
(
object
,
(
char
*
)
parent
_pointe
r
);
cJSONUtils_InplaceDecodePointerString
(
child
_pointe
r
);
/* add, remove, replace, move, copy, test. */
if
((
parent
==
NULL
)
||
(
child
pt
r
==
NULL
))
if
((
parent
==
NULL
)
||
(
child
_pointe
r
==
NULL
))
{
/* Couldn't find object to add to. */
free
(
parentptr
);
cJSON_Delete
(
value
);
return
9
;
status
=
9
;
goto
cleanup
;
}
else
if
(
cJSON_IsArray
(
parent
))
{
if
(
!
strcmp
((
char
*
)
childptr
,
"-"
)
)
if
(
strcmp
((
char
*
)
child_pointer
,
"-"
)
==
0
)
{
cJSON_AddItemToArray
(
parent
,
value
);
value
=
NULL
;
}
else
{
size_t
index
=
0
;
if
(
!
decode_array_index_from_pointer
(
child
pt
r
,
&
index
))
if
(
!
decode_array_index_from_pointer
(
child
_pointe
r
,
&
index
))
{
free
(
parentptr
);
cJSON_Delete
(
value
);
return
11
;
status
=
11
;
goto
cleanup
;
}
if
(
!
insert_item_in_array
(
parent
,
index
,
value
))
{
free
(
parentptr
);
cJSON_Delete
(
value
);
return
10
;
status
=
10
;
goto
cleanup
;
}
value
=
NULL
;
}
}
else
if
(
cJSON_IsObject
(
parent
))
{
cJSON_DeleteItemFromObject
(
parent
,
(
char
*
)
childptr
);
cJSON_AddItemToObject
(
parent
,
(
char
*
)
childptr
,
value
);
cJSON_DeleteItemFromObject
(
parent
,
(
char
*
)
child_pointer
);
cJSON_AddItemToObject
(
parent
,
(
char
*
)
child_pointer
,
value
);
value
=
NULL
;
}
else
cleanup:
if
(
value
!=
NULL
)
{
cJSON_Delete
(
value
);
}
free
(
parentptr
);
if
(
parent_pointer
!=
NULL
)
{
cJSON_free
(
parent_pointer
);
}
return
0
;
return
status
;
}
CJSON_PUBLIC
(
int
)
cJSONUtils_ApplyPatches
(
cJSON
*
object
,
cJSON
*
patches
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录