Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
8879ed5d
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看板
You need to sign in or sign up before continuing.
提交
8879ed5d
编写于
10月 17, 2016
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
reformatting: cJSONUtils_CompareToPatch
上级
52e53acf
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
89 addition
and
53 deletion
+89
-53
cJSON_Utils.c
cJSON_Utils.c
+89
-53
未找到文件。
cJSON_Utils.c
浏览文件 @
8879ed5d
...
@@ -509,64 +509,100 @@ void cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path,
...
@@ -509,64 +509,100 @@ void cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path,
cJSONUtils_GeneratePatch
(
array
,
op
,
path
,
0
,
val
);
cJSONUtils_GeneratePatch
(
array
,
op
,
path
,
0
,
val
);
}
}
static
void
cJSONUtils_CompareToPatch
(
cJSON
*
patches
,
const
char
*
path
,
cJSON
*
from
,
cJSON
*
to
)
static
void
cJSONUtils_CompareToPatch
(
cJSON
*
patches
,
const
char
*
path
,
cJSON
*
from
,
cJSON
*
to
)
{
{
if
(
from
->
type
!=
to
->
type
)
{
cJSONUtils_GeneratePatch
(
patches
,
"replace"
,
path
,
0
,
to
);
return
;
}
if
(
from
->
type
!=
to
->
type
)
{
cJSONUtils_GeneratePatch
(
patches
,
"replace"
,
path
,
0
,
to
);
return
;
}
switch
(
from
->
type
)
switch
(
from
->
type
)
{
{
case
cJSON_Number
:
case
cJSON_Number
:
if
(
from
->
valueint
!=
to
->
valueint
||
from
->
valuedouble
!=
to
->
valuedouble
)
if
((
from
->
valueint
!=
to
->
valueint
)
||
(
from
->
valuedouble
!=
to
->
valuedouble
))
cJSONUtils_GeneratePatch
(
patches
,
"replace"
,
path
,
0
,
to
);
{
cJSONUtils_GeneratePatch
(
patches
,
"replace"
,
path
,
0
,
to
);
}
return
;
return
;
case
cJSON_String
:
case
cJSON_String
:
if
(
strcmp
(
from
->
valuestring
,
to
->
valuestring
)
!=
0
)
if
(
strcmp
(
from
->
valuestring
,
to
->
valuestring
)
!=
0
)
cJSONUtils_GeneratePatch
(
patches
,
"replace"
,
path
,
0
,
to
);
{
cJSONUtils_GeneratePatch
(
patches
,
"replace"
,
path
,
0
,
to
);
}
return
;
return
;
case
cJSON_Array
:
case
cJSON_Array
:
{
{
int
c
;
char
*
newpath
=
(
char
*
)
malloc
(
strlen
(
path
)
+
23
);
/* Allow space for 64bit int. */
int
c
;
for
(
c
=
0
,
from
=
from
->
child
,
to
=
to
->
child
;
from
&&
to
;
from
=
from
->
next
,
to
=
to
->
next
,
c
++
){
char
*
newpath
=
(
char
*
)
malloc
(
strlen
(
path
)
+
23
);
/* Allow space for 64bit int. */
sprintf
(
newpath
,
"%s/%d"
,
path
,
c
);
cJSONUtils_CompareToPatch
(
patches
,
newpath
,
from
,
to
);
/* generate patches for all array elements that exist in "from" and "to" */
for
(
c
=
0
,
from
=
from
->
child
,
to
=
to
->
child
;
from
&&
to
;
from
=
from
->
next
,
to
=
to
->
next
,
c
++
)
{
sprintf
(
newpath
,
"%s/%d"
,
path
,
c
);
/* path of the current array element */
cJSONUtils_CompareToPatch
(
patches
,
newpath
,
from
,
to
);
}
/* remove leftover elements from 'from' that are not in 'to' */
for
(;
from
;
from
=
from
->
next
,
c
++
)
{
sprintf
(
newpath
,
"%d"
,
c
);
cJSONUtils_GeneratePatch
(
patches
,
"remove"
,
path
,
newpath
,
0
);
}
/* add new elements in 'to' that were not in 'from' */
for
(;
to
;
to
=
to
->
next
,
c
++
)
{
cJSONUtils_GeneratePatch
(
patches
,
"add"
,
path
,
"-"
,
to
);
}
}
for
(;
from
;
from
=
from
->
next
,
c
++
)
{
sprintf
(
newpath
,
"%d"
,
c
);
cJSONUtils_GeneratePatch
(
patches
,
"remove"
,
path
,
newpath
,
0
);
}
for
(;
to
;
to
=
to
->
next
,
c
++
)
cJSONUtils_GeneratePatch
(
patches
,
"add"
,
path
,
"-"
,
to
);
free
(
newpath
);
free
(
newpath
);
return
;
return
;
}
}
case
cJSON_Object
:
case
cJSON_Object
:
{
{
cJSON
*
a
,
*
b
;
cJSON
*
a
;
cJSON
*
b
;
cJSONUtils_SortObject
(
from
);
cJSONUtils_SortObject
(
from
);
cJSONUtils_SortObject
(
to
);
cJSONUtils_SortObject
(
to
);
a
=
from
->
child
,
b
=
to
->
child
;
a
=
from
->
child
;
b
=
to
->
child
;
/* for all object values in the object with more of them */
while
(
a
||
b
)
while
(
a
||
b
)
{
{
int
diff
=
(
!
a
)
?
1
:
(
!
b
)
?-
1
:
cJSONUtils_strcasecmp
(
a
->
string
,
b
->
string
);
int
diff
=
(
!
a
)
?
1
:
((
!
b
)
?
-
1
:
cJSONUtils_strcasecmp
(
a
->
string
,
b
->
string
)
);
if
(
!
diff
)
if
(
!
diff
)
{
{
char
*
newpath
=
(
char
*
)
malloc
(
strlen
(
path
)
+
cJSONUtils_PointerEncodedstrlen
(
a
->
string
)
+
2
);
/* both object keys are the same */
cJSONUtils_PointerEncodedstrcpy
(
newpath
+
sprintf
(
newpath
,
"%s/"
,
path
),
a
->
string
);
char
*
newpath
=
(
char
*
)
malloc
(
strlen
(
path
)
+
cJSONUtils_PointerEncodedstrlen
(
a
->
string
)
+
2
);
cJSONUtils_CompareToPatch
(
patches
,
newpath
,
a
,
b
);
cJSONUtils_PointerEncodedstrcpy
(
newpath
+
sprintf
(
newpath
,
"%s/"
,
path
),
a
->
string
);
/* create a patch for the element */
cJSONUtils_CompareToPatch
(
patches
,
newpath
,
a
,
b
);
free
(
newpath
);
free
(
newpath
);
a
=
a
->
next
;
a
=
a
->
next
;
b
=
b
->
next
;
b
=
b
->
next
;
}
else
if
(
diff
<
0
)
{
/* object element doesn't exist in 'to' --> remove it */
cJSONUtils_GeneratePatch
(
patches
,
"remove"
,
path
,
a
->
string
,
0
);
a
=
a
->
next
;
}
else
{
/* object element doesn't exist in 'from' --> add it */
cJSONUtils_GeneratePatch
(
patches
,
"add"
,
path
,
b
->
string
,
b
);
b
=
b
->
next
;
}
}
else
if
(
diff
<
0
)
{
cJSONUtils_GeneratePatch
(
patches
,
"remove"
,
path
,
a
->
string
,
0
);
a
=
a
->
next
;}
else
{
cJSONUtils_GeneratePatch
(
patches
,
"add"
,
path
,
b
->
string
,
b
);
b
=
b
->
next
;}
}
}
return
;
return
;
}
}
default:
break
;
default:
break
;
}
}
}
}
cJSON
*
cJSONUtils_GeneratePatches
(
cJSON
*
from
,
cJSON
*
to
)
cJSON
*
cJSONUtils_GeneratePatches
(
cJSON
*
from
,
cJSON
*
to
)
{
{
cJSON
*
patches
=
cJSON_CreateArray
();
cJSON
*
patches
=
cJSON_CreateArray
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录