Design: primitive type implemented as a replacement to boolean/number/null. String tokens point to the unquoted string

master
Serge A. Zaitsev 14 years ago
parent 60509e2850
commit d0d52f6847

@ -1,4 +1,4 @@
CFLAGS=-Wall -std=c89 -g CFLAGS=-Wall -std=c89 -g -O2
OBJS=jsmn.o demo.o OBJS=jsmn.o demo.o

@ -19,18 +19,15 @@ static void jsmn_dump_obj(jsontok_t *obj, const char *js) {
len = obj->end - obj->start; len = obj->end - obj->start;
printf("[%d,%d]\t", obj->start, obj->end); printf("[%3d,%3d]\t", obj->start, obj->end);
char *type; char *type;
switch (obj->type) { switch (obj->type) {
case JSON_OTHER: case JSON_PRIMITIVE:
type = "(?)"; type = "(.)";
break;
case JSON_NUMBER:
type = "(N)";
break; break;
case JSON_STRING: case JSON_STRING:
type = "(S)"; type = "(s)";
break; break;
case JSON_ARRAY: case JSON_ARRAY:
type = "(A)"; type = "(A)";
@ -91,7 +88,6 @@ int main(int argc, char *argv[]) {
r = jsmn_parse(&parser); r = jsmn_parse(&parser);
if (r < 0) { if (r < 0) {
printf("error %d at pos %d: %s\n", r, parser.pos, &js[parser.pos]); printf("error %d at pos %d: %s\n", r, parser.pos, &js[parser.pos]);
exit(EXIT_FAILURE);
} }
for (i = 0; i<NUM_TOKENS; i++) { for (i = 0; i<NUM_TOKENS; i++) {

@ -14,7 +14,7 @@ void jsmn_init_parser(jsmn_parser *parser, const char *js,
for (i = 0; i < parser->num_tokens; i++) { for (i = 0; i < parser->num_tokens; i++) {
parser->tokens[i].start = -1; parser->tokens[i].start = -1;
parser->tokens[i].end = -1; parser->tokens[i].end = -1;
parser->tokens[i].type = JSON_OTHER; parser->tokens[i].type = JSON_PRIMITIVE;
} }
} }
@ -49,7 +49,7 @@ static int jsmn_parse_primitive(jsmn_parser *parser) {
js = parser->js; js = parser->js;
token = jsmn_start_token(parser, JSON_NUMBER); token = jsmn_start_token(parser, JSON_PRIMITIVE);
for (; js[parser->pos] != '\0'; parser->pos++) { for (; js[parser->pos] != '\0'; parser->pos++) {
switch (js[parser->pos]) { switch (js[parser->pos]) {
@ -66,7 +66,6 @@ static int jsmn_parse_primitive(jsmn_parser *parser) {
return JSMN_ERROR_PART; return JSMN_ERROR_PART;
} }
static int jsmn_parse_string(jsmn_parser *parser) { static int jsmn_parse_string(jsmn_parser *parser) {
const char *js; const char *js;
jsontok_t *token; jsontok_t *token;
@ -112,7 +111,6 @@ static int jsmn_parse_string(jsmn_parser *parser) {
return JSMN_ERROR_PART; return JSMN_ERROR_PART;
} }
jsmnerr_t jsmn_parse(jsmn_parser *parser) { jsmnerr_t jsmn_parse(jsmn_parser *parser) {
const char *js; const char *js;
jsontype_t type; jsontype_t type;
@ -131,6 +129,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser) {
case '}': case ']': case '}': case ']':
type = (c == '}' ? JSON_OBJECT : JSON_ARRAY); type = (c == '}' ? JSON_OBJECT : JSON_ARRAY);
token = jsmn_end_token(parser, type); token = jsmn_end_token(parser, type);
token->end++;
break; break;
case '-': case '0': case '1' : case '2': case '3' : case '4': case '-': case '0': case '1' : case '2': case '3' : case '4':
case '5': case '6': case '7' : case '8': case '9': case '5': case '6': case '7' : case '8': case '9':

@ -10,11 +10,10 @@
* o Other primitive: boolean (true/false) or null * o Other primitive: boolean (true/false) or null
*/ */
typedef enum { typedef enum {
JSON_OTHER = 0, JSON_PRIMITIVE = 0,
JSON_OBJECT = 1, JSON_OBJECT = 1,
JSON_ARRAY = 2, JSON_ARRAY = 2,
JSON_STRING = 3, JSON_STRING = 3
JSON_NUMBER = 4
} jsontype_t; } jsontype_t;
typedef enum { typedef enum {

Loading…
Cancel
Save