Skip to content

Commit 22e325b

Browse files
committed
Optimize copying JSON value.
1 parent 576dd85 commit 22e325b

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

json_parser.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -913,25 +913,26 @@ static int __copy_json_value(const json_value_t *src, json_value_t *dest);
913913

914914
static int __copy_json_members(const json_object_t *src, json_object_t *dest)
915915
{
916-
const char *name;
917-
const json_value_t *val;
916+
struct list_head *pos;
917+
json_member_t *entry;
918918
json_member_t *memb;
919919
int len;
920920

921-
json_object_for_each(name, val, src)
921+
list_for_each(pos, &src->head)
922922
{
923-
len = strlen(name);
923+
entry = list_entry(pos, json_member_t, list);
924+
len = strlen(entry->name);
924925
memb = (json_member_t *)malloc(offsetof(json_member_t, name) + len + 1);
925926
if (!memb)
926927
return -1;
927928

928-
if (__copy_json_value(val, &memb->value) < 0)
929+
if (__copy_json_value(&entry->value, &memb->value) < 0)
929930
{
930931
free(memb);
931932
return -1;
932933
}
933934

934-
memcpy(memb->name, name, len + 1);
935+
memcpy(memb->name, entry->name, len + 1);
935936
__insert_json_member(memb, dest->head.prev, dest);
936937
}
937938

@@ -940,16 +941,18 @@ static int __copy_json_members(const json_object_t *src, json_object_t *dest)
940941

941942
static int __copy_json_elements(const json_array_t *src, json_array_t *dest)
942943
{
943-
const json_value_t *val;
944+
struct list_head *pos;
945+
json_element_t *entry;
944946
json_element_t *elem;
945947

946-
json_array_for_each(val, src)
948+
list_for_each(pos, &src->head)
947949
{
948950
elem = (json_element_t *)malloc(sizeof (json_element_t));
949951
if (!elem)
950952
return -1;
951953

952-
if (__copy_json_value(val, &elem->value) < 0)
954+
entry = list_entry(pos, json_element_t, list);
955+
if (__copy_json_value(&entry->value, &elem->value) < 0)
953956
{
954957
free(elem);
955958
return -1;
@@ -1010,19 +1013,19 @@ static int __copy_json_value(const json_value_t *src, json_value_t *dest)
10101013

10111014
json_value_t *json_value_copy(const json_value_t *val)
10121015
{
1013-
json_value_t *dest;
1016+
json_value_t *copy;
10141017

1015-
dest = (json_value_t *)malloc(sizeof (json_value_t));
1016-
if (!dest)
1018+
copy = (json_value_t *)malloc(sizeof (json_value_t));
1019+
if (!copy)
10171020
return NULL;
10181021

1019-
if (__copy_json_value(val, dest) < 0)
1022+
if (__copy_json_value(val, copy) < 0)
10201023
{
1021-
free(dest);
1024+
free(copy);
10221025
return NULL;
10231026
}
10241027

1025-
return dest;
1028+
return copy;
10261029
}
10271030

10281031
void json_value_destroy(json_value_t *val)

0 commit comments

Comments
 (0)