diff options
-rw-r--r-- | json.c | 43 | ||||
-rw-r--r-- | json.h | 13 | ||||
-rw-r--r-- | libgps_json.c | 6 | ||||
-rw-r--r-- | test_json.c | 9 |
4 files changed, 65 insertions, 6 deletions
@@ -128,6 +128,12 @@ static char *json_target_address(const struct json_attr_t *cursor, case t_uinteger: targetaddr = (char *)&cursor->addr.uinteger[offset]; break; + case t_short: + targetaddr = (char *)&cursor->addr.shortint[offset]; + break; + case t_ushort: + targetaddr = (char *)&cursor->addr.ushortint[offset]; + break; case t_time: case t_real: targetaddr = (char *)&cursor->addr.real[offset]; @@ -197,6 +203,13 @@ static int json_internal_read_object(const char *cp, case t_uinteger: memcpy(lptr, &cursor->dflt.uinteger, sizeof(unsigned int)); break; + case t_short: + memcpy(lptr, &cursor->dflt.shortint, sizeof(short)); + break; + case t_ushort: + memcpy(lptr, &cursor->dflt.ushortint, + sizeof(unsigned short)); + break; case t_time: case t_real: memcpy(lptr, &cursor->dflt.real, sizeof(double)); @@ -478,6 +491,18 @@ static int json_internal_read_object(const char *cp, memcpy(lptr, &tmp, sizeof(unsigned int)); } break; + case t_short: + { + short tmp = atoi(valbuf); + memcpy(lptr, &tmp, sizeof(short)); + } + break; + case t_ushort: + { + unsigned short tmp = (unsigned int)atoi(valbuf); + memcpy(lptr, &tmp, sizeof(unsigned short)); + } + break; case t_time: { double tmp = iso8601_to_unix(valbuf); @@ -645,6 +670,24 @@ int json_read_array(const char *cp, const struct json_array_t *arr, cp = ep; break; #endif /* JSON_MINIMAL */ + case t_short: +#ifndef JSON_MINIMAL + arr->arr.shorts.store[offset] = (short)strtol(cp, &ep, 0); + if (ep == cp) + return JSON_ERR_BADNUM; + else + cp = ep; + break; +#endif /* JSON_MINIMAL */ + case t_ushort: +#ifndef JSON_MINIMAL + arr->arr.ushorts.store[offset] = (unsigned short)strtoul(cp, &ep, 0); + if (ep == cp) + return JSON_ERR_BADNUM; + else + cp = ep; + break; +#endif /* JSON_MINIMAL */ case t_time: #ifndef JSON_MINIMAL if (*cp != '"') @@ -11,7 +11,8 @@ typedef enum {t_integer, t_uinteger, t_real, t_string, t_boolean, t_character, t_time, t_object, t_structobject, t_array, - t_check, t_ignore} + t_check, t_ignore, + t_short, t_ushort} json_type; struct json_enum_t { @@ -39,6 +40,12 @@ struct json_array_t { unsigned int *store; } uintegers; struct { + short *store; + } shorts; + struct { + unsigned short *store; + } ushorts; + struct { double *store; } reals; struct { @@ -54,6 +61,8 @@ struct json_attr_t { union { int *integer; unsigned int *uinteger; + short *shortint; + unsigned short *ushortint; double *real; char *string; bool *boolean; @@ -64,6 +73,8 @@ struct json_attr_t { union { int integer; unsigned int uinteger; + short shortint; + unsigned short ushortint; double real; bool boolean; char character; diff --git a/libgps_json.c b/libgps_json.c index 2dac60dc..7ebb0f78 100644 --- a/libgps_json.c +++ b/libgps_json.c @@ -109,9 +109,9 @@ static int json_sky_read(const char *buf, struct gps_data_t *gpsdata, { const struct json_attr_t json_attrs_satellites[] = { /* *INDENT-OFF* */ - {"PRN", t_integer, STRUCTOBJECT(struct satellite_t, PRN)}, - {"el", t_integer, STRUCTOBJECT(struct satellite_t, elevation)}, - {"az", t_integer, STRUCTOBJECT(struct satellite_t, azimuth)}, + {"PRN", t_short, STRUCTOBJECT(struct satellite_t, PRN)}, + {"el", t_short, STRUCTOBJECT(struct satellite_t, elevation)}, + {"az", t_short, STRUCTOBJECT(struct satellite_t, azimuth)}, {"ss", t_real, STRUCTOBJECT(struct satellite_t, ss)}, {"used", t_boolean, STRUCTOBJECT(struct satellite_t, used)}, /* *INDENT-ON* */ diff --git a/test_json.c b/test_json.c index 03d039fd..aff1aa08 100644 --- a/test_json.c +++ b/test_json.c @@ -90,7 +90,12 @@ static const char json_str1[] = "{\"class\":\"TPV\",\ \"time\":\"2005-06-19T08:12:41.89Z\",\"lon\":46.498203637,\"lat\":7.568074350,\ \"alt\":1327.780,\"epx\":21.000,\"epy\":23.000,\"epv\":124.484,\"mode\":3}"; -/* Case 2: SKY report */ +/* + * Case 2: SKY report + * + * The fields of the last satellite entry are arranged in the reverse order + * of the structure fields, in order to test for field overflow. + */ static const char *json_str2 = "{\"class\":\"SKY\",\ \"time\":\"2005-06-19T12:12:42.03Z\", \ @@ -101,7 +106,7 @@ static const char *json_str2 = "{\"class\":\"SKY\",\ {\"PRN\":26,\"el\":51,\"az\":304,\"ss\":43,\"used\":true},\ {\"PRN\":8,\"el\":44,\"az\":58,\"ss\":41,\"used\":true},\ {\"PRN\":27,\"el\":16,\"az\":66,\"ss\":39,\"used\":true},\ - {\"PRN\":21,\"el\":10,\"az\":301,\"ss\":0,\"used\":false}]}"; + {\"az\":301,\"el\":10,\"PRN\":21,\"used\":false,\"ss\":0}]}"; /* Case 3: String list syntax */ |