Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ indent_size = 4
[list.h]
indent_style = tab
indent_size = 8
[rbtree.*]
indent_style = tab
indent_size = 8
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ LD = gcc

all: test_speed parse_json

json_parser.o: json_parser.c json_parser.h list.h rbtree.h
json_parser.o: json_parser.c json_parser.h list.h

test_speed: json_parser.o rbtree.o test_speed.o
test_speed: json_parser.o test_speed.o
$(LD) -o test_speed $^

parse_json: json_parser.o rbtree.o test.o
parse_json: json_parser.o test.o
$(LD) -o parse_json $^

clean:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int json_object_size(const json_object_t *obj);

/* Find the JSON value under the key @name. Returns NULL if @name
can not be found. The time complexity of this function is
O(log(n)), where n is the size of the JSON object.
O(n), where n is the size of the JSON object.
@name: The key to find
@obj: JSON object
Note: The returned pointer to JSON value is const. */
Expand Down
50 changes: 7 additions & 43 deletions json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
#include <ctype.h>
#include <math.h>
#include "list.h"
#include "rbtree.h"
#include "json_parser.h"

#define JSON_DEPTH_LIMIT 1024

struct __json_object
{
struct list_head head;
struct rb_root root;
int size;
};

Expand All @@ -38,7 +36,6 @@ struct __json_value
struct __json_member
{
struct list_head list;
struct rb_node rb;
json_value_t value;
char name[1];
};
Expand Down Expand Up @@ -448,28 +445,6 @@ static int __parse_json_number(const char *cursor, const char **end,
return 0;
}

static void __insert_json_member(json_member_t *memb, struct list_head *pos,
json_object_t *obj)
{
struct rb_node **p = &obj->root.rb_node;
struct rb_node *parent = NULL;
json_member_t *entry;

while (*p)
{
parent = *p;
entry = rb_entry(*p, json_member_t, rb);
if (strcmp(memb->name, entry->name) < 0)
p = &(*p)->rb_left;
else
p = &(*p)->rb_right;
}

rb_link_node(&memb->rb, parent, p);
rb_insert_color(&memb->rb, &obj->root);
list_add(&memb->list, pos);
}

static int __parse_json_value(const char *cursor, const char **end,
int depth, json_value_t *val);

Expand Down Expand Up @@ -540,7 +515,7 @@ static int __parse_json_members(const char *cursor, const char **end,
return ret;
}

__insert_json_member(memb, obj->head.prev, obj);
list_add_tail(&memb->list, &obj->head);
cnt++;

while (isspace(*cursor))
Expand Down Expand Up @@ -584,7 +559,6 @@ static int __parse_json_object(const char *cursor, const char **end,
return -3;

INIT_LIST_HEAD(&obj->head);
obj->root.rb_node = NULL;
ret = __parse_json_members(cursor, end, depth + 1, obj);
if (ret < 0)
{
Expand Down Expand Up @@ -834,7 +808,6 @@ static void __move_json_value(json_value_t *src, json_value_t *dest)
case JSON_VALUE_OBJECT:
INIT_LIST_HEAD(&dest->value.object.head);
list_splice(&src->value.object.head, &dest->value.object.head);
dest->value.object.root.rb_node = src->value.object.root.rb_node;
dest->value.object.size = src->value.object.size;
break;

Expand Down Expand Up @@ -878,7 +851,6 @@ static int __set_json_value(int type, va_list ap, json_value_t *val)

case JSON_VALUE_OBJECT:
INIT_LIST_HEAD(&val->value.object.head);
val->value.object.root.rb_node = NULL;
val->value.object.size = 0;
break;

Expand Down Expand Up @@ -940,7 +912,7 @@ static int __copy_json_members(const json_object_t *src, json_object_t *dest)
}

memcpy(memb->name, entry->name, len + 1);
__insert_json_member(memb, dest->head.prev, dest);
list_add_tail(&memb->list, &dest->head);
}

return src->size;
Expand Down Expand Up @@ -995,7 +967,6 @@ static int __copy_json_value(const json_value_t *src, json_value_t *dest)

case JSON_VALUE_OBJECT:
INIT_LIST_HEAD(&dest->value.object.head);
dest->value.object.root.rb_node = NULL;
ret = __copy_json_members(&src->value.object, &dest->value.object);
if (ret < 0)
{
Expand Down Expand Up @@ -1086,19 +1057,13 @@ json_array_t *json_value_array(const json_value_t *val)
const json_value_t *json_object_find(const char *name,
const json_object_t *obj)
{
struct rb_node *p = obj->root.rb_node;
struct list_head *pos;
json_member_t *memb;
int n;

while (p)
list_for_each(pos, &obj->head)
{
memb = rb_entry(p, json_member_t, rb);
n = strcmp(name, memb->name);
if (n < 0)
p = p->rb_left;
else if (n > 0)
p = p->rb_right;
else
memb = list_entry(pos, json_member_t, list);
if (strcmp(name, memb->name) == 0)
return &memb->value;
}

Expand Down Expand Up @@ -1200,7 +1165,7 @@ static const json_value_t *__json_object_insert(const char *name,
return NULL;
}

__insert_json_member(memb, pos, obj);
list_add(&memb->list, pos);
obj->size++;
return &memb->value;
}
Expand Down Expand Up @@ -1266,7 +1231,6 @@ json_value_t *json_object_remove(const json_value_t *val,
return NULL;

list_del(&memb->list);
rb_erase(&memb->rb, &obj->root);
obj->size--;

__move_json_value(&memb->value, (json_value_t *)val);
Expand Down
Loading