Skip to content

Commit

Permalink
xjson: More robust field parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Sep 29, 2024
1 parent 5a7ae6c commit 3f4bc28
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/xjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,18 @@ static XStructure *ParseObject(char **pos, int *lineNumber) {
XField *f;

next = SkipSpaces(next, lineNumber);

if(*next == '}') {
next++;
break;
}

if(*next == ',') {
Warning("[L.%d] Empty field.\n", *lineNumber);
next++;
continue;
}

f = calloc(1, sizeof(XField));
x_check_alloc(f);

Expand Down Expand Up @@ -466,11 +473,19 @@ static XStructure *ParseObject(char **pos, int *lineNumber) {
f->value = ParsePrimitive(&next, &f->type, lineNumber);
}

if(*next == ',')
next++;

f = xSetField(s, f);
if(f) xDestroyField(f); // If duplicate field, destroy the prior one.

// Spaces after field...
next = SkipSpaces(next, lineNumber);

// There should be either a comma or a closing bracket after the field...
if(*next == ',') next++;
else if(*next == '}') {
next++;
break;
}
else Warning("[L.%d] Missing comma or closing bracket after field.\n", *lineNumber);
}

*pos = next;
Expand Down

0 comments on commit 3f4bc28

Please sign in to comment.