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
|
||||
|
||||
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
|
||||
|
@ -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,17 +1,38 @@
|
||||
#ifndef __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 {
|
||||
JSON_OTHER = 0,
|
||||
JSON_OBJECT,
|
||||
JSON_ARRAY,
|
||||
JSON_STRING,
|
||||
JSON_OTHER
|
||||
JSON_NUMBER
|
||||
} 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 {
|
||||
jsontype_t type;
|
||||
int start;
|
||||
int end;
|
||||
} 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_ */
|
||||
|
Loading…
Reference in new issue