summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--json.c9
-rw-r--r--test_json.c39
2 files changed, 31 insertions, 17 deletions
diff --git a/json.c b/json.c
index 2a6a4a6c..2f5b6c37 100644
--- a/json.c
+++ b/json.c
@@ -6,8 +6,6 @@
#include "json.h"
-#define JSON_DEBUG
-
int parse_json(const char *cp, const struct json_attr_t *attrs)
{
enum {init, await_attr, in_attr, await_value,
@@ -92,10 +90,10 @@ int parse_json(const char *cp, const struct json_attr_t *attrs)
break;
case in_val_string:
if (*cp == '"') {
+ *pval = '\0';
#ifdef JSONDEBUG
(void) printf("Collected string value %s\n", valbuf);
#endif /* JSONDEBUG */
- *pval = '\0';
state = post_val;
} else if (pval > valbuf + JSON_VAL_MAX - 1)
return -5; /* value too long */
@@ -104,10 +102,10 @@ int parse_json(const char *cp, const struct json_attr_t *attrs)
break;
case in_val_token:
if (isspace(*cp) || *cp == ',' || *cp == '}') {
+ *pval = '\0';
#ifdef JSONDEBUG
(void) printf("Collected token value %s\n", valbuf);
#endif /* JSONDEBUG */
- *pval = '\0';
state = post_val;
if (*cp == '}' || *cp == ',')
--cp;
@@ -129,6 +127,9 @@ int parse_json(const char *cp, const struct json_attr_t *attrs)
(void)strcpy(cursor->addr.string, valbuf);
break;
case boolean:
+#ifdef JSONDEBUG
+ (void) printf("Boolean value '%s' processed to %d\n", valbuf, !strcmp(valbuf, "true"));
+#endif /* JSONDEBUG */
*(cursor->addr.boolean) = (bool)!strcmp(valbuf, "true");
break;
}
diff --git a/test_json.c b/test_json.c
index 9441cc5f..16508130 100644
--- a/test_json.c
+++ b/test_json.c
@@ -14,17 +14,24 @@
exit(1); \
}
-#define ASSERT_STRING(fld, val) \
+#define ASSERT_STRING(attr, fld, val) \
if (strcmp(fld, val)) \
{\
- (void)fprintf(stderr, "string attribute eval failed, value = %s.\n", fld);\
+ (void)fprintf(stderr, "'%s' attribute eval failed, value = %s.\n", attr, fld); \
exit(1);\
}
-#define ASSERT_INTEGER(fld, val) \
+#define ASSERT_INTEGER(attr, fld, val) \
if (fld != val)\
{\
- (void)fprintf(stderr, "integer attribute eval failed, value = %d.\n", fld);\
+ (void)fprintf(stderr, "'%s' attribute eval failed, value = %d.\n", attr, fld); \
+ exit(1);\
+ }
+
+#define ASSERT_BOOLEAN(attr, fld, val) \
+ if (fld != val)\
+ {\
+ (void)fprintf(stderr, "'%s' attribute eval failed, value = %s.\n", attr, fld ? "true" : "false"); \
exit(1);\
}
@@ -32,20 +39,22 @@
* Floating point comparisons are iffy, but at least if any of these fail
* the output will make it clear whether it was a precision issue
*/
-#define ASSERT_REAL(fld, val) \
+#define ASSERT_REAL(attr, fld, val) \
if (fld != val)\
{\
- (void)fprintf(stderr, "real attribute eval failed, value = %f.\n", fld);\
+ (void)fprintf(stderr, "'%s' attribute eval failed, value = %f.\n", attr, fld); \
exit(1);\
}
const char *json_str1 = "{\"device\":\"GPS#1\",\"tag\":\"MID2\",\
\"time\":1119197561.890,\"lon\":46.498203637,\"lat\":7.568074350,\
- \"alt\":1327.780,\"eph\":21.000,\"epv\":124.484,\"mode\":3}";
+ \"alt\":1327.780,\"eph\":21.000,\"epv\":124.484,\"mode\":3,\
+ \"flag1\":true,\"flag2\":false}";
static char buf1[JSON_VAL_MAX+1];
static char buf2[JSON_VAL_MAX+1];
static struct gps_fix_t fix;
+bool flag1, flag2;
const struct json_attr_t json_attrs_1[] = {
{"device", string, .addr.string = buf1},
@@ -57,6 +66,8 @@ const struct json_attr_t json_attrs_1[] = {
{"eph", real, .addr.real = &fix.eph, .dflt.real = 0},
{"epv", real, .addr.real = &fix.epv, .dflt.real = 0},
{"mode", integer, .addr.integer = &fix.mode, .dflt.integer = -1},
+ {"flag1", boolean, .addr.boolean = &flag1,},
+ {"flag2", boolean, .addr.boolean = &flag2,},
{NULL},
};
@@ -68,12 +79,14 @@ int main(int argc, char *argv[])
status = parse_json(json_str1, json_attrs_1);
ASSERT_CASE(1, status);
- ASSERT_STRING(buf1, "GPS#1");
- ASSERT_STRING(buf2, "MID2");
- ASSERT_INTEGER(fix.mode, 3);
- ASSERT_REAL(fix.time, 1119197561.890);
- ASSERT_REAL(fix.longitude, 46.498203637);
- ASSERT_REAL(fix.latitude, 7.568074350);
+ ASSERT_STRING("device", buf1, "GPS#1");
+ ASSERT_STRING("tag", buf2, "MID2");
+ ASSERT_INTEGER("mode", fix.mode, 3);
+ ASSERT_REAL("time", fix.time, 1119197561.890);
+ ASSERT_REAL("lon", fix.longitude, 46.498203637);
+ ASSERT_REAL("lat", fix.latitude, 7.568074350);
+ ASSERT_BOOLEAN("flag1", flag1, true);
+ ASSERT_BOOLEAN("flag2", flag2, false);
(void)fprintf(stderr, "succeeded.\n");
exit(0);