Demo separated from the jsmn code. Makefile changed. Some comments added

master
Serge A. Zaitsev 14 years ago
parent 6b6b3ba5c1
commit 0568be6e0b

@ -1,13 +1,17 @@
CFLAGS=-Wall -W -std=c89 CFLAGS=-Wall -std=c89
OBJS=jsmn.o demo.o
all: jsmn_demo all: jsmn_demo
jsmn_demo: jsmn.o jsmn_demo: $(OBJS)
gcc $(LDFLAGS) jsmn.o -o $@ $(CC) $(LDFLAGS) $(OBJS) -o $@
jsmn.o: jsmn.c jsmn.h %.o: %.c jsmn.h
gcc $(CFLAGS) -c jsmn.c -o $@ $(CC) -c $(CFLAGS) $< -o $@
clean: clean:
rm -f jsmn.o rm -f $(OBJS)
rm -f jsmn_demo rm -f jsmn_demo
.PHONY: clean all

@ -0,0 +1,66 @@
/* This demo is not needed to be C89-compatible, so for now GCC extensions are
* used */
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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<NUM_TOKENS; i++) {
json_dump_obj(&tokens[i], js);
}
return 0;
}

@ -1,5 +1,4 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include "jsmn.h" #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) { int jsmn_token_end(jsontype_t type, int pos) {
unsigned int i; int i;
for (i = num_tokens - 1; i>= 0; i--) { for (i = num_tokens - 1; i>= 0; i--) {
if (tokens[i].type == type && tokens[i].start != -1 && tokens[i].end == -1) { if (tokens[i].type == type && tokens[i].start != -1 && tokens[i].end == -1) {
tokens[i].end = pos; 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; const unsigned char *p;
jsontok_t *cur_token;
int obj_common[] = { int obj_common[] = {
JSON_SYM_ERROR(0 ... 255), 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; int *obj_state = obj_common;
cur_token = tokens; unsigned int i;
int i;
for (i = 0; i<num_tokens; i++) { for (i = 0; i<num_tokens; i++) {
tokens[i].start = tokens[i].end = -1; tokens[i].start = tokens[i].end = -1;
tokens[i].type = JSON_OTHER; tokens[i].type = JSON_OTHER;
@ -144,55 +140,3 @@ int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, in
return 0; return 0;
} }
void json_dump_obj(jsontok_t *obj, const char *js) {
int len;
printf("[%d,%d]", obj->start, 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<NUM_TOKENS; i++) {
json_dump_obj(&tokens[i], js);
}
return 0;
}

@ -1,17 +1,38 @@
#ifndef __JSMN_H_ #ifndef __JSMN_H_
#define __JSMN_H_ #define __JSMN_H_
/**
* JSON type identifier. Basic types are:
* o Object
* o Array
* o String
* o Number
* o Primitive: boolean (true/false) or null
*/
typedef enum { typedef enum {
JSON_OTHER = 0,
JSON_OBJECT, JSON_OBJECT,
JSON_ARRAY, JSON_ARRAY,
JSON_STRING, JSON_STRING,
JSON_OTHER JSON_NUMBER
} jsontype_t; } jsontype_t;
/**
* JSON token description.
* @param type type (object, array, string etc.)
* @param start start position in JSON data string
* @param end end position in JSON data string
*/
typedef struct { typedef struct {
jsontype_t type; jsontype_t type;
int start; int start;
int end; int end;
} jsontok_t; } jsontok_t;
/**
* Run JSON parser. It parses a JSON data string into and array of tokens, each describing
* a single JSON object.
*/
int jsmn_parse(const unsigned char *js, jsontok_t *tokens, size_t num_tokens, int **errpos);
#endif /* __JSMN_H_ */ #endif /* __JSMN_H_ */

Loading…
Cancel
Save