-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonParser.c
101 lines (92 loc) · 2.87 KB
/
jsonParser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "jsmn.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsonParser.h"
#define MAX_TOKEN 10240 //10ko
TokenIterator* createTokenIterator(jsmn_parser *parser, jsmntok_t *tokens){
TokenIterator *iterator = malloc(sizeof (TokenIterator));
iterator->currentPosition = -1;
iterator->end = 0;
iterator->endPosition = parser->toknext;
iterator->tokens = tokens;
return iterator;
}
TokenIterator* copyTokenIterator(TokenIterator *it){
TokenIterator *iterator = malloc(sizeof (TokenIterator));
iterator->currentPosition = it->currentPosition;
iterator->end = it->end;
iterator->endPosition = it->endPosition;
iterator->tokens = it->tokens;
return iterator;
}
char* extractnToken(jsmntok_t token, char* jsonFile, int n){
int tokenStart = token.start;
int tokenEnd = token.end;
int tokenLenght = tokenEnd - tokenStart;
int stringLenght = tokenLenght > n ? n : tokenLenght;
char* extractedString = malloc(stringLenght+1);
extractedString[stringLenght] = '\0';
strncpy(extractedString, jsonFile + tokenStart, stringLenght);
return extractedString;
}
char* fileToString(FILE *file){
fseek(file, 0, SEEK_END);
int fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char *string = calloc(fileSize,1);
fread(string, 1, fileSize+1, file);
return string;
}
void getNextObject(TokenIterator *iterator){
iterator->currentPosition++;
while(!(iterator->currentPosition >= iterator->endPosition)){
if(iterator->tokens[iterator->currentPosition].type == JSMN_OBJECT){
return;
}else{
iterator->currentPosition++;
}
}
if(iterator->currentPosition >= iterator->end){
iterator->end = 1;
}
}
void printToken(char* jsonFile, jsmntok_t *token){
int stringStart = token->start;
int stringEnd = token->end;
for(int i=stringStart; i<stringEnd; i++){
printf("%c",jsonFile[i]);
}
printf("\n");
}
void getNextStringValue(TokenIterator *iterator, char* jsonFile, const char* key){
iterator->currentPosition++;
while(!(iterator->currentPosition >= iterator->endPosition)){
if(iterator->tokens[iterator->currentPosition].type == JSMN_STRING){
int objectPosition = iterator->currentPosition;
int stringStart = iterator->tokens[objectPosition].start;
int stringEnd = iterator->tokens[objectPosition].end;
int stringLenght = stringEnd - stringStart;
if(strncmp(jsonFile + stringStart, key, stringLenght) == 0){
iterator->currentPosition++;
return;
}
}
iterator->currentPosition++;
}
if(iterator->currentPosition >= iterator->end){
iterator->end = 1;
}
}
TokenIterator* parseJson(char *jsonFile){
jsmn_parser parser;
jsmntok_t *tokens = malloc(MAX_TOKEN * sizeof (jsmntok_t));
jsmn_init(&parser);
jsmnerr_t parsingError = jsmn_parse(&parser,jsonFile,tokens,MAX_TOKEN);
if(parsingError != JSMN_SUCCESS){
printf("parsingError: %d\n",parsingError);
exit(parsingError);
}
TokenIterator *it = createTokenIterator(&parser, tokens);
return it;
}