diff options
Diffstat (limited to 'lib/json.h')
-rw-r--r-- | lib/json.h | 101 |
1 files changed, 90 insertions, 11 deletions
@@ -1,7 +1,7 @@ -/* vim: set et ts=3 sw=3 ft=c: +/* vim: set et ts=3 sw=3 sts=3 ft=c: * - * Copyright (C) 2012 James McLaughlin et al. All rights reserved. + * Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved. * https://github.com/udp/json-parser * * Redistribution and use in source and binary forms, with or without @@ -35,6 +35,17 @@ #define json_char char #endif +#ifndef json_int_t + #ifndef _MSC_VER + #include <inttypes.h> + #define json_int_t int64_t + #else + #define json_int_t __int64 + #endif +#endif + +#include <stdlib.h> + #ifdef __cplusplus #include <string.h> @@ -49,9 +60,17 @@ typedef struct unsigned long max_memory; int settings; + /* Custom allocator support (leave null to use malloc/free) + */ + + void * (* mem_alloc) (size_t, int zero, void * user_data); + void (* mem_free) (void *, void * user_data); + + void * user_data; /* will be passed to mem_alloc and mem_free */ + } json_settings; -#define json_relaxed_commas 1 +#define json_enable_comments 0x01 typedef enum { @@ -77,7 +96,7 @@ typedef struct _json_value union { int boolean; - long long integer; + json_int_t integer; double dbl; struct @@ -94,10 +113,21 @@ typedef struct _json_value struct { json_char * name; + unsigned int name_length; + struct _json_value * value; } * values; + #if defined(__cplusplus) && __cplusplus >= 201103L + decltype(values) begin () const + { return values; + } + decltype(values) end () const + { return values + length; + } + #endif + } object; struct @@ -105,6 +135,15 @@ typedef struct _json_value unsigned int length; struct _json_value ** values; + #if defined(__cplusplus) && __cplusplus >= 201103L + decltype(values) begin () const + { return values; + } + decltype(values) end () const + { return values + length; + } + #endif + } array; } u; @@ -162,27 +201,67 @@ typedef struct _json_value }; } - inline operator long () const - { return u.integer; + inline operator json_int_t () const + { + switch (type) + { + case json_integer: + return u.integer; + + case json_double: + return (json_int_t) u.dbl; + + default: + return 0; + }; } inline operator bool () const - { return u.boolean != 0; + { + if (type != json_boolean) + return false; + + return u.boolean != 0; + } + + inline operator double () const + { + switch (type) + { + case json_integer: + return (double) u.integer; + + case json_double: + return u.dbl; + + default: + return 0; + }; } #endif } json_value; -json_value * json_parse - (const json_char * json); +json_value * json_parse (const json_char * json, + size_t length); -json_value * json_parse_ex - (json_settings * settings, const json_char * json, char * error); +#define json_error_max 128 +json_value * json_parse_ex (json_settings * settings, + const json_char * json, + size_t length, + char * error); void json_value_free (json_value *); +/* Not usually necessary, unless you used a custom mem_alloc and now want to + * use a custom mem_free. + */ +void json_value_free_ex (json_settings * settings, + json_value *); + + #ifdef __cplusplus } /* extern "C" */ #endif |