Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.so.*
*.o
jsonlint
run-unit-tests
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
AR = ar
CC = gcc
CXX = g++
CFLAGS ?= -Wall -Os -fPIC
CFLAGS_TEST ?= -I. -I./unit-tests/catch
LDFLAGS = -L.
SHLIB_CFLAGS = -shared

Expand All @@ -21,6 +23,8 @@ SO_LINKS = lib$(NAME).so lib$(NAME).so.$(MAJOR) lib$(NAME).so.$(MAJOR).$(MINOR)
SO_FILE = lib$(NAME).so.$(MAJOR).$(MINOR).$(MICRO)
HEADERS = $(NAME).h

TEST_SRC=$(wildcard unit-tests/*.cpp)

PREFIX ?= /usr
DESTDIR ?=
INSTALLDIR ?= $(DESTDIR)$(PREFIX)
Expand Down Expand Up @@ -62,10 +66,14 @@ $(NAME)lint: $(NAME)lint.o $(NAME).o
lib$(NAME).pc: lib$(NAME).pc.in
sed -e 's;@PREFIX@;$(PREFIX);' -e 's;@LIBJSON_VER_MAJOR@;$(MAJOR);' -e 's;@LIBJSON_VER_MINOR@;$(MINOR);' < $< > $@

.PHONY: tests clean install install-bin install-lib
.PHONY: tests clean install install-bin install-lib unit-tests
tests: $(NAME)lint
(cd tests; ./runtest)

unit-tests: $(TEST_SRC) $(NAME).o
$(CXX) $(CFLAGS) $(CFLAGS_TEST) $(LDFLAGS) -o run-unit-tests $+
./run-unit-tests -d yes

install-lib: $(SO_TARGETS) $(A_TARGETS) $(PC_TARGET)
mkdir -p $(INSTALLDIR)/lib/pkgconfig
$(INSTALL_DATA) -t $(INSTALLDIR)/lib/pkgconfig $(PC_TARGET)
Expand Down
423 changes: 300 additions & 123 deletions json.c

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions json.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef unsigned __int32 uint32_t;

typedef enum
{
JSON_NONE,
JSON_NONE = 1,
JSON_ARRAY_BEGIN,
JSON_OBJECT_BEGIN,
JSON_ARRAY_END,
Expand All @@ -48,6 +48,9 @@ typedef enum
JSON_FALSE,
JSON_NULL,
JSON_BSTRING,
JSON_PARTIAL_KEY,
JSON_PARTIAL_VALUE,
JSON_PARTIAL_STRING,
} json_type;

typedef enum
Expand Down Expand Up @@ -79,10 +82,23 @@ typedef enum
JSON_ERROR_CALLBACK,
/* utf8 stream is invalid */
JSON_ERROR_UTF8,
/* internal error */
JSON_ERROR,
} json_error;

typedef enum
{
DEFAULT,
/* Enable buffer flushing at the end of provided JSON chunk.
* A JSON_PARTIAL_DATA callback will be triggered. */
PARTIAL_DATA_CALLBACKS,
/* Enable inplace parsing */
IN_PLACE
} json_parsing_mode;

#define LIBJSON_DEFAULT_STACK_SIZE 256
#define LIBJSON_DEFAULT_BUFFER_SIZE 4096
#define ESCAPE_BUFFER_SIZE 6

typedef int (*json_parser_callback)(void *userdata, int type, const char *data, uint32_t length);
typedef int (*json_printer_callback)(void *userdata, const char *s, uint32_t length);
Expand All @@ -93,6 +109,7 @@ typedef struct {
uint32_t max_data;
int allow_c_comments;
int allow_yaml_comments;
json_parsing_mode mode;
void * (*user_calloc)(size_t nmemb, size_t size);
void * (*user_realloc)(void *ptr, size_t size);
} json_config;
Expand All @@ -117,10 +134,12 @@ typedef struct json_parser {
uint32_t stack_offset;
uint32_t stack_size;

/* parse buffer */
/* parse buffers */
char *buffer;
uint32_t buffer_size;
uint32_t buffer_offset;
char escape_buffer[ESCAPE_BUFFER_SIZE];
uint32_t escape_buffer_offset;
} json_parser;

typedef struct json_printer {
Expand Down
2 changes: 2 additions & 0 deletions unit-tests/CatchTestRunner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include <catch.hpp>
Loading