src/cx/json.h

changeset 966
1aa7ec3e46e7
parent 965
dfdfedbe2c86
child 967
a58f602ed2fe
equal deleted inserted replaced
965:dfdfedbe2c86 966:1aa7ec3e46e7
62 enum cx_json_value_type { 62 enum cx_json_value_type {
63 CX_JSON_NOTHING, // this allows us to always return non-NULL values 63 CX_JSON_NOTHING, // this allows us to always return non-NULL values
64 CX_JSON_OBJECT, 64 CX_JSON_OBJECT,
65 CX_JSON_ARRAY, 65 CX_JSON_ARRAY,
66 CX_JSON_STRING, 66 CX_JSON_STRING,
67 CX_JSON_INTEGER, // TODO: the spec does not know integer types 67 CX_JSON_INTEGER,
68 CX_JSON_NUMBER, 68 CX_JSON_NUMBER,
69 CX_JSON_LITERAL 69 CX_JSON_LITERAL
70 }; 70 };
71 71
72 enum cx_json_literal { 72 enum cx_json_literal {
205 return value->type == CX_JSON_STRING; 205 return value->type == CX_JSON_STRING;
206 } 206 }
207 207
208 __attribute__((__nonnull__)) 208 __attribute__((__nonnull__))
209 static inline bool cxJsonIsNumber(CxJsonValue *value) { 209 static inline bool cxJsonIsNumber(CxJsonValue *value) {
210 // TODO: this is not good, because an integer is also a number 210 return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER;
211 return value->type == CX_JSON_NUMBER;
212 } 211 }
213 212
214 __attribute__((__nonnull__)) 213 __attribute__((__nonnull__))
215 static inline bool cxJsonIsInteger(CxJsonValue *value) { 214 static inline bool cxJsonIsInteger(CxJsonValue *value) {
216 return value->type == CX_JSON_INTEGER; 215 return value->type == CX_JSON_INTEGER;
239 __attribute__((__nonnull__)) 238 __attribute__((__nonnull__))
240 static inline bool cxJsonIsNull(CxJsonValue *value) { 239 static inline bool cxJsonIsNull(CxJsonValue *value) {
241 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL; 240 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL;
242 } 241 }
243 242
244 __attribute__((__nonnull__)) 243 __attribute__((__nonnull__, __returns_nonnull__))
245 static inline cxmutstr cxJsonAsString(CxJsonValue *value) { 244 static inline char *cxJsonAsString(CxJsonValue *value) {
246 // TODO: do we need a separate method to return this directly as cxstring? 245 return value->value.string.ptr;
246 }
247
248 __attribute__((__nonnull__))
249 static inline cxstring cxJsonAsCxString(CxJsonValue *value) {
250 return cx_strcast(value->value.string);
251 }
252
253 __attribute__((__nonnull__))
254 static inline cxmutstr cxJsonAsCxMutStr(CxJsonValue *value) {
247 return value->value.string; 255 return value->value.string;
248 } 256 }
249 257
250 __attribute__((__nonnull__)) 258 __attribute__((__nonnull__))
251 static inline double cxJsonAsDouble(CxJsonValue *value) { 259 static inline double cxJsonAsDouble(CxJsonValue *value) {
252 return value->value.number; 260 if (value->type == CX_JSON_INTEGER) {
261 return (double) value->value.integer;
262 } else {
263 return value->value.number;
264 }
253 } 265 }
254 266
255 __attribute__((__nonnull__)) 267 __attribute__((__nonnull__))
256 static inline int64_t cxJsonAsInteger(CxJsonValue *value) { 268 static inline int64_t cxJsonAsInteger(CxJsonValue *value) {
257 return value->value.integer; 269 if (value->type == CX_JSON_INTEGER) {
270 return value->value.integer;
271 } else {
272 return (int64_t) value->value.number;
273 }
258 } 274 }
259 275
260 __attribute__((__nonnull__)) 276 __attribute__((__nonnull__))
261 static inline bool cxJsonAsBool(CxJsonValue *value) { 277 static inline bool cxJsonAsBool(CxJsonValue *value) {
262 return value->value.literal == CX_JSON_TRUE; 278 return value->value.literal == CX_JSON_TRUE;

mercurial