summaryrefslogtreecommitdiff
path: root/json.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2009-08-28 10:24:13 +0000
committerEric S. Raymond <esr@thyrsus.com>2009-08-28 10:24:13 +0000
commit4769babfa5a2cad6666eb5d44e8ad109a5528d25 (patch)
tree9f6665a61592f84f5b8f68603ded5486f6833720 /json.c
parent511cfba4940624602c4ea5fbb622c7989d8acc2e (diff)
downloadgpsd-4769babfa5a2cad6666eb5d44e8ad109a5528d25.tar.gz
Allow mapping of strings to enumerated values in JSON.
Diffstat (limited to 'json.c')
-rw-r--r--json.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/json.c b/json.c
index adc4d744..cfbed639 100644
--- a/json.c
+++ b/json.c
@@ -63,6 +63,7 @@ static int json_internal_read_object(const char *cp, const struct json_attr_t *a
char valbuf[JSON_VAL_MAX+1], *pval = NULL;
const struct json_attr_t *cursor;
int substatus, maxlen;
+ const struct json_enum_t *mp;
/* stuff fields with defaults in case they're omitted in the JSON input */
for (cursor = attrs; cursor->attribute != NULL; cursor++)
@@ -228,6 +229,8 @@ static int json_internal_read_object(const char *cp, const struct json_attr_t *a
maxlen = cursor->len - 1;
else if (cursor->type == check)
maxlen = strlen(cursor->dflt.check);
+ else if (cursor->map != NULL)
+ maxlen = sizeof(valbuf)-1;
if (*cp == '"') {
*pval = '\0';
#ifdef JSONDEBUG
@@ -260,6 +263,15 @@ static int json_internal_read_object(const char *cp, const struct json_attr_t *a
*pval++ = *cp;
break;
case post_val:
+ if (cursor->map != 0) {
+ for (mp = cursor->map; mp->name != NULL; mp++)
+ if (strcmp(mp->name, valbuf) == 0) {
+ goto foundit;
+ }
+ return JSON_ERR_BADENUM;
+ foundit:
+ (void)snprintf(valbuf, sizeof(valbuf), "%d", mp->value);
+ }
if (parent == NULL || parent->element_type != structobject) {
/* ordinary case - use the address in the cursor structure */
switch(cursor->type)