Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSMN_TOKNEXT #242

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ simple_example: example/simple.c jsmn.h
jsondump: example/jsondump.c jsmn.h
$(CC) $(LDFLAGS) $< -o $@

tokendump: example/tokendump.c jsmn.h
$(CC) $(LDFLAGS) $< -o $@

fmt:
clang-format -i jsmn.h test/*.[ch] example/*.[ch]

Expand All @@ -31,6 +34,7 @@ clean:
rm -f *.o example/*.o
rm -f simple_example
rm -f jsondump
rm -f tokendump

.PHONY: clean test

78 changes: 78 additions & 0 deletions example/tokendump.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#define JSMN_TOKNEXT

#include "../jsmn.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void dump(char *js, jsmntok_t* tokens) {
puts("index type[size] start-end toknext value");
puts("-------------------------------------------");
int tokenCount = tokens[0].toknext;
for (int ti = 0; ti < tokenCount; ti++) {
jsmntok_t t = tokens[ti];
char *type;
switch (t.type) {
case JSMN_ARRAY:
type = "ARRAY";
break;
case JSMN_OBJECT:
type = "OBJECT";
break;
case JSMN_PRIMITIVE:
type = "PRIMITIVE";
break;
case JSMN_STRING:
type = "STRING";
break;
}
printf("%3d: %9s[%03d]: %4d-%4d: %3d", ti, type, t.size, t.start, t.end,
t.toknext);
switch (t.type) {
case JSMN_PRIMITIVE:
case JSMN_STRING:
printf(": ");
for (int i = t.start; i < t.end;)
putchar(js[i++]);
}
putchar('\n');
}
}

void help() {
puts("tokendump <json>");
exit(1);
}

int main(int argc, char *args[]) {
if (argc != 2)
help();
FILE *f = fopen(args[1], "r");
if (f == NULL)
help();

// load file to memory
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
char *js = malloc(size + 1);
for (int i = 0; i < size;)
js[i++] = fgetc(f);
js[size] = 0;
fclose(f);

// count tokens first
jsmn_parser parser;
jsmn_init(&parser);
int len = strlen(js);
int tokenCount = jsmn_parse(&parser, js, len, NULL, -1);
if (tokenCount > 0) {
// parse them
jsmntok_t *tokens = malloc(tokenCount * sizeof(jsmntok_t));
jsmn_init(&parser);
jsmn_parse(&parser, (const char *)js, len, tokens, tokenCount);
// and dump finally
dump(js, tokens);
}
}
12 changes: 11 additions & 1 deletion jsmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ enum jsmnerr {
typedef struct jsmntok {
jsmntype_t type;
int start;
#ifdef JSMN_TOKNEXT
union {
int end;
int toknext;
};
#else
int end;
#endif
int size;
#ifdef JSMN_PARENT_LINKS
int parent;
Expand Down Expand Up @@ -342,7 +349,10 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
}
parser->toksuper = -1;
token->end = parser->pos + 1;
break;
#ifdef JSMN_TOKNEXT
token->toknext = parser->toknext;
#endif
break;
}
}
/* Error if unmatched closing bracket */
Expand Down