diff options
| author | Wilmer van der Gaast <wilmer@gaast.net> | 2014-02-13 08:48:37 +0000 | 
|---|---|---|
| committer | Wilmer van der Gaast <wilmer@gaast.net> | 2014-02-13 08:48:37 +0000 | 
| commit | 7a80925eabe969ffc1e8e24c2ad31c80c33ab3cd (patch) | |
| tree | e070eccc63ed1819f999e9464d3fb6fb7a302cc3 /lib/json.h | |
| parent | e76cf264de6caaca24fa308cab78e770fc4a6508 (diff) | |
Update json-parser code to git rev 11a80f389769d1f66ca7fbe64ad89c82d3ba4ba9.
Few BitlBee-specific diffs now. Annoyingly need to link to libm now for the
use of the function pow() since the lib now does its own number parsing...
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 | 
