Skip to content

Commit 21b2644

Browse files
committed
Use 'size_t' for JSON object/array's size.
1 parent 758a8fb commit 21b2644

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ json_array_t *json_value_array(const json_value_t *val);
6868
~~~c
6969
/* Get the size of the JSON object.
7070
@obj: JSON object */
71-
int json_object_size(const json_object_t *obj);
71+
size_t json_object_size(const json_object_t *obj);
7272
7373
/* Find the JSON value under the key @name. Returns NULL if @name
7474
can not be found. The time complexity of this function is
@@ -91,7 +91,7 @@ json_object_for_each_prev(name, val, obj)
9191
~~~c
9292
/* Get the size of the JSON array.
9393
@arr:JSON array */
94-
int json_array_size(const json_array_t *arr);
94+
size_t json_array_size(const json_array_t *arr);
9595

9696
/* Traversing the JSON array forward or backward
9797
@val: Temporary (const json_value_t *) pointer for each JSON value

README_cn.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ json_array_t *json_value_array(const json_value_t *val);
6060
~~~c
6161
/* 返回object的大小。即object包含的name,value对数量
6262
@obj:JSON object对象 */
63-
int json_object_size(const json_object_t *obj);
63+
size_t json_object_size(const json_object_t *obj);
6464
6565
/* 查找并返回name下的value。返回NULL代表找不到这个name。函数时间复杂度为O(log(size))
6666
@name:要查找的名字
@@ -80,7 +80,7 @@ json_object_for_each_prev(name, val, obj)
8080
~~~c
8181
/* 返回JSON array的大小,即元素个数
8282
@arr:JSON array对象 */
83-
int json_array_size(const json_array_t *arr);
83+
size_t json_array_size(const json_array_t *arr);
8484

8585
/* 向前或向后遍历JSON array
8686
@val:临时的const json_value_t *类型的JSON value对象

json_parser.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
struct __json_object
1313
{
1414
struct list_head head;
15-
int size;
15+
size_t size;
1616
};
1717

1818
struct __json_array
1919
{
2020
struct list_head head;
21-
int size;
21+
size_t size;
2222
};
2323

2424
struct __json_value
@@ -481,7 +481,6 @@ static int __parse_json_members(const char *cursor, const char **end,
481481
int depth, json_object_t *obj)
482482
{
483483
json_member_t *memb;
484-
int cnt = 0;
485484
size_t len;
486485
int ret;
487486

@@ -516,7 +515,7 @@ static int __parse_json_members(const char *cursor, const char **end,
516515
}
517516

518517
list_add_tail(&memb->list, &obj->head);
519-
cnt++;
518+
obj->size++;
520519

521520
while (isspace(*cursor))
522521
cursor++;
@@ -534,7 +533,7 @@ static int __parse_json_members(const char *cursor, const char **end,
534533
}
535534

536535
*end = cursor + 1;
537-
return cnt;
536+
return 0;
538537
}
539538

540539
static void __destroy_json_members(json_object_t *obj)
@@ -559,22 +558,21 @@ static int __parse_json_object(const char *cursor, const char **end,
559558
return -3;
560559

561560
INIT_LIST_HEAD(&obj->head);
561+
obj->size = 0;
562562
ret = __parse_json_members(cursor, end, depth + 1, obj);
563563
if (ret < 0)
564564
{
565565
__destroy_json_members(obj);
566566
return ret;
567567
}
568568

569-
obj->size = ret;
570569
return 0;
571570
}
572571

573572
static int __parse_json_elements(const char *cursor, const char **end,
574573
int depth, json_array_t *arr)
575574
{
576575
json_element_t *elem;
577-
int cnt = 0;
578576
int ret;
579577

580578
while (isspace(*cursor))
@@ -600,7 +598,7 @@ static int __parse_json_elements(const char *cursor, const char **end,
600598
}
601599

602600
list_add_tail(&elem->list, &arr->head);
603-
cnt++;
601+
arr->size++;
604602

605603
while (isspace(*cursor))
606604
cursor++;
@@ -618,7 +616,7 @@ static int __parse_json_elements(const char *cursor, const char **end,
618616
}
619617

620618
*end = cursor + 1;
621-
return cnt;
619+
return 0;
622620
}
623621

624622
static void __destroy_json_elements(json_array_t *arr)
@@ -643,14 +641,14 @@ static int __parse_json_array(const char *cursor, const char **end,
643641
return -3;
644642

645643
INIT_LIST_HEAD(&arr->head);
644+
arr->size = 0;
646645
ret = __parse_json_elements(cursor, end, depth + 1, arr);
647646
if (ret < 0)
648647
{
649648
__destroy_json_elements(arr);
650649
return ret;
651650
}
652651

653-
arr->size = ret;
654652
return 0;
655653
}
656654

@@ -913,9 +911,10 @@ static int __copy_json_members(const json_object_t *src, json_object_t *dest)
913911

914912
memcpy(memb->name, entry->name, len + 1);
915913
list_add_tail(&memb->list, &dest->head);
914+
dest->size++;
916915
}
917916

918-
return src->size;
917+
return 0;
919918
}
920919

921920
static int __copy_json_elements(const json_array_t *src, json_array_t *dest)
@@ -940,9 +939,10 @@ static int __copy_json_elements(const json_array_t *src, json_array_t *dest)
940939
}
941940

942941
list_add_tail(&elem->list, &dest->head);
942+
dest->size++;
943943
}
944944

945-
return src->size;
945+
return 0;
946946
}
947947

948948
static int __copy_json_value(const json_value_t *src, json_value_t *dest)
@@ -967,26 +967,26 @@ static int __copy_json_value(const json_value_t *src, json_value_t *dest)
967967

968968
case JSON_VALUE_OBJECT:
969969
INIT_LIST_HEAD(&dest->value.object.head);
970+
dest->value.object.size = 0;
970971
ret = __copy_json_members(&src->value.object, &dest->value.object);
971972
if (ret < 0)
972973
{
973974
__destroy_json_members(&dest->value.object);
974975
return ret;
975976
}
976977

977-
dest->value.object.size = ret;
978978
break;
979979

980980
case JSON_VALUE_ARRAY:
981981
INIT_LIST_HEAD(&dest->value.array.head);
982+
dest->value.array.size = 0;
982983
ret = __copy_json_elements(&src->value.array, &dest->value.array);
983984
if (ret < 0)
984985
{
985986
__destroy_json_elements(&dest->value.array);
986987
return ret;
987988
}
988989

989-
dest->value.array.size = ret;
990990
break;
991991
}
992992

@@ -1002,13 +1002,11 @@ json_value_t *json_value_copy(const json_value_t *val)
10021002
if (!copy)
10031003
return NULL;
10041004

1005-
if (__copy_json_value(val, copy) < 0)
1006-
{
1007-
free(copy);
1008-
return NULL;
1009-
}
1005+
if (__copy_json_value(val, copy) >= 0)
1006+
return copy;
10101007

1011-
return copy;
1008+
free(copy);
1009+
return NULL;
10121010
}
10131011

10141012
void json_value_destroy(json_value_t *val)
@@ -1070,7 +1068,7 @@ const json_value_t *json_object_find(const char *name,
10701068
return NULL;
10711069
}
10721070

1073-
int json_object_size(const json_object_t *obj)
1071+
size_t json_object_size(const json_object_t *obj)
10741072
{
10751073
return obj->size;
10761074
}
@@ -1238,7 +1236,7 @@ json_value_t *json_object_remove(const json_value_t *val,
12381236
return (json_value_t *)val;
12391237
}
12401238

1241-
int json_array_size(const json_array_t *arr)
1239+
size_t json_array_size(const json_array_t *arr)
12421240
{
12431241
return arr->size;
12441242
}

json_parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ json_array_t *json_value_array(const json_value_t *val);
3333

3434
const json_value_t *json_object_find(const char *name,
3535
const json_object_t *obj);
36-
int json_object_size(const json_object_t *obj);
36+
size_t json_object_size(const json_object_t *obj);
3737
const char *json_object_next_name(const char *name,
3838
const json_object_t *obj);
3939
const json_value_t *json_object_next_value(const json_value_t *val,
@@ -58,7 +58,7 @@ const json_value_t *json_object_insert_before(const json_value_t *val,
5858
json_value_t *json_object_remove(const json_value_t *val,
5959
json_object_t *obj);
6060

61-
int json_array_size(const json_array_t *arr);
61+
size_t json_array_size(const json_array_t *arr);
6262
const json_value_t *json_array_next_value(const json_value_t *val,
6363
const json_array_t *arr);
6464
const json_value_t *json_array_prev_value(const json_value_t *val,

0 commit comments

Comments
 (0)