diff --git a/Makefile b/Makefile index c6816e9..cda64f5 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,17 @@ -CFLAGS=-Wall -W -std=c89 +CFLAGS=-Wall -std=c89 + +OBJS=jsmn.o demo.o all: jsmn_demo -jsmn_demo: jsmn.o - gcc $(LDFLAGS) jsmn.o -o $@ +jsmn_demo: $(OBJS) + $(CC) $(LDFLAGS) $(OBJS) -o $@ -jsmn.o: jsmn.c jsmn.h - gcc $(CFLAGS) -c jsmn.c -o $@ +%.o: %.c jsmn.h + $(CC) -c $(CFLAGS) $< -o $@ clean: - rm -f jsmn.o + rm -f $(OBJS) rm -f jsmn_demo + +.PHONY: clean all diff --git a/demo.c b/demo.c new file mode 100644 index 0000000..e644dcb --- /dev/null +++ b/demo.c @@ -0,0 +1,66 @@ +/* This demo is not needed to be C89-compatible, so for now GCC extensions are + * used */ +#define _GNU_SOURCE + +#include +#include +#include + +#include "jsmn.h" + +#define NUM_TOKENS 20 + +static void json_dump_obj(jsontok_t *obj, const unsigned char *js) { + size_t len; + + printf("[%d,%d]", obj->start, obj->end); + len = (size_t) (obj->end - obj->start); + + char *type; + switch (obj->type) { + case JSON_OTHER: + type = "other"; + break; + case JSON_NUMBER: + type = "number"; + break; + case JSON_STRING: + type = "string"; + break; + case JSON_ARRAY: + type = "array"; + break; + case JSON_OBJECT: + type = "object"; + break; + } + + printf(" %s ", type); + + if (len > 0) { + char *s = strndup((const char *) &js[obj->start], len); + printf("%s", s); + free(s); + } + printf("\n"); +} + +int main(void) { + int i; + jsontok_t tokens[NUM_TOKENS]; + + const unsigned char *js = (unsigned char *) + "{" + "\"foo\": \"bar\"," + "\"bar\": [1,2, 3]," + "\"obj\": { \"true\": false}" + "}"; + + jsmn_parse(js, tokens, NUM_TOKENS, NULL); + + for (i = 0; i -#include #include "jsmn.h" @@ -53,7 +52,7 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in } int jsmn_token_end(jsontype_t type, int pos) { - unsigned int i; + int i; for (i = num_tokens - 1; i>= 0; i--) { if (tokens[i].type == type && tokens[i].start != -1 && tokens[i].end == -1) { tokens[i].end = pos; @@ -64,7 +63,6 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in } const unsigned char *p; - jsontok_t *cur_token; int obj_common[] = { JSON_SYM_ERROR(0 ... 255), @@ -96,9 +94,7 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in int *obj_state = obj_common; - cur_token = tokens; - - int i; + unsigned int i; for (i = 0; istart, obj->end); - len = obj->end - obj->start; - - char *type; - switch (obj->type) { - case JSON_OTHER: - type = "other"; - break; - case JSON_STRING: - type = "string"; - break; - case JSON_ARRAY: - type = "array"; - break; - case JSON_OBJECT: - type = "object"; - break; - } - - printf(" %s ", type); - - if (len > 0) { - char *s = strndup(&js[obj->start], len); - printf("%s", s); - free(s); - } - printf("\n"); -} - -int main(int argc, char *argv[]) { - int i; -#define NUM_TOKENS 20 - jsontok_t tokens[NUM_TOKENS]; - - const char *js = - "{" - "\"foo\": \"bar\"," - "\"bar\": [1,2, 3]," - "\"obj\": { \"true\": false}" - "}"; - - jsmn_parse(js, tokens, NUM_TOKENS, NULL); - - for (i = 0; i