Allow empty string values. Disallow empty key strings
This commit is contained in:
parent
06cfc1d0b6
commit
e708952c05
@ -1,11 +1,14 @@
|
|||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: parse output
|
all: parse parse_file output
|
||||||
|
|
||||||
parse:
|
parse: parse.c
|
||||||
gcc -ggdb -I../../include -L../../build parse.c -Wall -Wextra -pedantic -o parse -lrjp
|
gcc -ggdb -I../../include -L../../build parse.c -Wall -Wextra -pedantic -o parse -lrjp
|
||||||
output:
|
output: output.c
|
||||||
gcc -ggdb -I../../include -L../../build output.c -Wall -Wextra -pedantic -o output -lrjp
|
gcc -ggdb -I../../include -L../../build output.c -Wall -Wextra -pedantic -o output -lrjp
|
||||||
|
parse_file: parse_file.c
|
||||||
|
gcc -ggdb -I../../include -L../../build parse_file.c -Wall -Wextra -pedantic -o parse_file -lrjp
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm parse
|
rm parse
|
||||||
|
rm parse_file
|
||||||
rm output
|
rm output
|
||||||
|
|||||||
37
doc/examples/parse_file.c
Normal file
37
doc/examples/parse_file.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <rjp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv){
|
||||||
|
if(argc != 2){
|
||||||
|
fprintf(stderr, "Requires exactly 1 argument\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE* fp = fopen(argv[1], "rb");
|
||||||
|
if(!fp){
|
||||||
|
fprintf(stderr, "Unable to open file\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fseek(fp, 0, SEEK_END);
|
||||||
|
int buflen = ftell(fp);
|
||||||
|
fseek(fp, 0, SEEK_SET);
|
||||||
|
char* buffer = malloc(buflen+1);
|
||||||
|
fread(buffer, 1, buflen, fp);
|
||||||
|
buffer[buflen] = 0;
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
//Read in json argument allowing all RJP extensions (comments, trailing comma)
|
||||||
|
RJP_value* root = rjp_parse(buffer, RJP_PARSE_ALL_EXT);
|
||||||
|
|
||||||
|
//returns NULL on error
|
||||||
|
if(!root){
|
||||||
|
fprintf(stderr, "Invalid JSON\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("Valid JSON\n");
|
||||||
|
|
||||||
|
//clean memory
|
||||||
|
free(buffer);
|
||||||
|
rjp_free_value(root);
|
||||||
|
}
|
||||||
@ -150,6 +150,10 @@ static RJP_value* irjp_add_value_to_array(RJP_lex_category cat, RJP_parse_state*
|
|||||||
static RJP_value* irjp_add_value_to_object(RJP_parse_state* state, const char* key, RJP_index keylen){
|
static RJP_value* irjp_add_value_to_object(RJP_parse_state* state, const char* key, RJP_index keylen){
|
||||||
RJP_index newlen;
|
RJP_index newlen;
|
||||||
char* newkey = irjp_convert_string(key, keylen, &newlen);
|
char* newkey = irjp_convert_string(key, keylen, &newlen);
|
||||||
|
if(!newlen){ //cannot have empty key
|
||||||
|
rjp_free(newkey);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return (state->lastadded = rjp_new_member_steal_key(state->curr, newkey, newlen));
|
return (state->lastadded = rjp_new_member_steal_key(state->curr, newkey, newlen));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -173,8 +173,6 @@ char* irjp_convert_string(const char* str, RJP_index length, RJP_index* newlen){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!newlength)
|
|
||||||
return NULL;
|
|
||||||
newstring = rjp_alloc(newlength + 1);
|
newstring = rjp_alloc(newlength + 1);
|
||||||
newstring[newlength] = 0;
|
newstring[newlength] = 0;
|
||||||
*newlen = newlength;
|
*newlen = newlength;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user