add json parsing of booleans and numbers

This commit is contained in:
Felix Van der Jeugt 2022-11-03 08:23:02 +01:00
parent a13c6ecb3a
commit e8897609b7
No known key found for this signature in database
GPG Key ID: 58B209295023754D

68
json.c
View File

@ -6,9 +6,63 @@
#include "sortedmap.h"
#include "arraylist.h"
/* TODO */
Json * parse_map(FILE *stream) { return NULL; /* TODO */ }
Json * parse_list(FILE *stream) { return NULL; /* TODO */ }
Json * parse_true(FILE *stream) {
Json * json;
if (getc(stream) == 'r' && getc(stream) == 'u' && getc(stream) == 'e') {
json = malloc(sizeof(Json));
json->type = BOOLEAN;
json->value.boolean = 1;
return json;
}
return NULL;
}
Json * parse_false(FILE *stream) {
Json * json;
if (getc(stream) == 'a' && getc(stream) == 'l' && getc(stream) == 's' && getc(stream) == 'e') {
json = malloc(sizeof(Json));
json->type = BOOLEAN;
json->value.boolean = 0;
return json;
}
return NULL;
}
Json * parse_number(FILE *stream) {
Json * json;
long num = 0, den = 0;
char c;
while (1) {
c = getc(stream);
switch(c) {
case '0':
num = num * 10;
den *= 10;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
num = num * 10 + (c - '1' + 1);
den *= 10;
break;
case '.':
den = 1;
break;
default:
if (c != EOF) ungetc(c, stream);
json = malloc(sizeof(Json));
json->type = NUMBER;
json->value.number = num * 1.0 / den;
return json;
}
}
}
Json * parse_string(FILE *stream) {
int i = 0, cap = 10;
@ -42,9 +96,8 @@ Json * parse_string(FILE *stream) {
return NULL;
}
Json * parse_true(FILE *stream) { return NULL; /* TODO */ }
Json * parse_false(FILE *stream) { return NULL; /* TODO */ }
Json * parse_number(FILE *stream, char first) { return NULL; /* TODO */ }
Json * parse_list(FILE *stream) { return NULL; /* TODO */ }
Json * parse_map(FILE *stream) { return NULL; /* TODO */ }
Json * json_parse(FILE *stream) {
char c;
@ -71,7 +124,8 @@ Json * json_parse(FILE *stream) {
case '7':
case '8':
case '9':
return parse_number(stream, c);
ungetc(c, stream);
return parse_number(stream);
case ' ':
case '\n':
case '\t':