Commits

Serge A. Zaitsev  committed 03af3a5

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

  • Participants
  • Parent commits be55d4c

Comments (0)

Files changed (4)

-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
+/* 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;
+}
 #include <stdlib.h>
-#include <stdio.h>
 
 #include "jsmn.h"
 
 	}
 
 	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;
 	}
 	
 	const unsigned char *p;
-	jsontok_t *cur_token;
 
 	int obj_common[] = {
 		JSON_SYM_ERROR(0 ... 255),
 
 	int *obj_state = obj_common;
 
-	cur_token = tokens;
-
-	int i;
+	unsigned int i;
 	for (i = 0; i<num_tokens; i++) {
 		tokens[i].start = tokens[i].end = -1;
 		tokens[i].type = JSON_OTHER;
 	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;
-}
 #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_ */