--- a/src/cx/json.h Fri Nov 01 17:24:51 2024 +0100 +++ b/src/cx/json.h Fri Nov 01 17:35:42 2024 +0100 @@ -64,7 +64,7 @@ CX_JSON_OBJECT, CX_JSON_ARRAY, CX_JSON_STRING, - CX_JSON_INTEGER, // TODO: the spec does not know integer types + CX_JSON_INTEGER, CX_JSON_NUMBER, CX_JSON_LITERAL }; @@ -207,8 +207,7 @@ __attribute__((__nonnull__)) static inline bool cxJsonIsNumber(CxJsonValue *value) { - // TODO: this is not good, because an integer is also a number - return value->type == CX_JSON_NUMBER; + return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER; } __attribute__((__nonnull__)) @@ -241,20 +240,37 @@ return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL; } +__attribute__((__nonnull__, __returns_nonnull__)) +static inline char *cxJsonAsString(CxJsonValue *value) { + return value->value.string.ptr; +} + __attribute__((__nonnull__)) -static inline cxmutstr cxJsonAsString(CxJsonValue *value) { - // TODO: do we need a separate method to return this directly as cxstring? +static inline cxstring cxJsonAsCxString(CxJsonValue *value) { + return cx_strcast(value->value.string); +} + +__attribute__((__nonnull__)) +static inline cxmutstr cxJsonAsCxMutStr(CxJsonValue *value) { return value->value.string; } __attribute__((__nonnull__)) static inline double cxJsonAsDouble(CxJsonValue *value) { - return value->value.number; + if (value->type == CX_JSON_INTEGER) { + return (double) value->value.integer; + } else { + return value->value.number; + } } __attribute__((__nonnull__)) static inline int64_t cxJsonAsInteger(CxJsonValue *value) { - return value->value.integer; + if (value->type == CX_JSON_INTEGER) { + return value->value.integer; + } else { + return (int64_t) value->value.number; + } } __attribute__((__nonnull__))