Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/json.h

    r5d749ad r7a80925  
    11
    2 /* vim: set et ts=3 sw=3 ft=c:
    3  *
    4  * Copyright (C) 2012 James McLaughlin et al.  All rights reserved.
     2/* vim: set et ts=3 sw=3 sts=3 ft=c:
     3 *
     4 * Copyright (C) 2012, 2013, 2014 James McLaughlin et al.  All rights reserved.
    55 * https://github.com/udp/json-parser
    66 *
     
    3636#endif
    3737
     38#ifndef json_int_t
     39   #ifndef _MSC_VER
     40      #include <inttypes.h>
     41      #define json_int_t int64_t
     42   #else
     43      #define json_int_t __int64
     44   #endif
     45#endif
     46
     47#include <stdlib.h>
     48
    3849#ifdef __cplusplus
    3950
     
    5061   int settings;
    5162
     63   /* Custom allocator support (leave null to use malloc/free)
     64    */
     65
     66   void * (* mem_alloc) (size_t, int zero, void * user_data);
     67   void (* mem_free) (void *, void * user_data);
     68
     69   void * user_data;  /* will be passed to mem_alloc and mem_free */
     70
    5271} json_settings;
    5372
    54 #define json_relaxed_commas 1
     73#define json_enable_comments  0x01
    5574
    5675typedef enum
     
    7897   {
    7998      int boolean;
    80       long long integer;
     99      json_int_t integer;
    81100      double dbl;
    82101
     
    95114         {
    96115            json_char * name;
     116            unsigned int name_length;
     117
    97118            struct _json_value * value;
    98119
    99120         } * values;
     121
     122         #if defined(__cplusplus) && __cplusplus >= 201103L
     123         decltype(values) begin () const
     124         {  return values;
     125         }
     126         decltype(values) end () const
     127         {  return values + length;
     128         }
     129         #endif
    100130
    101131      } object;
     
    106136         struct _json_value ** values;
    107137
     138         #if defined(__cplusplus) && __cplusplus >= 201103L
     139         decltype(values) begin () const
     140         {  return values;
     141         }
     142         decltype(values) end () const
     143         {  return values + length;
     144         }
     145         #endif
     146
    108147      } array;
    109148
     
    163202         }
    164203
    165          inline operator long () const
    166          {  return u.integer;
     204         inline operator json_int_t () const
     205         { 
     206            switch (type)
     207            {
     208               case json_integer:
     209                  return u.integer;
     210
     211               case json_double:
     212                  return (json_int_t) u.dbl;
     213
     214               default:
     215                  return 0;
     216            };
    167217         }
    168218
    169219         inline operator bool () const
    170          {  return u.boolean != 0;
     220         { 
     221            if (type != json_boolean)
     222               return false;
     223
     224            return u.boolean != 0;
     225         }
     226
     227         inline operator double () const
     228         { 
     229            switch (type)
     230            {
     231               case json_integer:
     232                  return (double) u.integer;
     233
     234               case json_double:
     235                  return u.dbl;
     236
     237               default:
     238                  return 0;
     239            };
    171240         }
    172241
     
    175244} json_value;
    176245
    177 json_value * json_parse
    178    (const json_char * json);
    179 
    180 json_value * json_parse_ex
    181    (json_settings * settings, const json_char * json, char * error);
     246json_value * json_parse (const json_char * json,
     247                         size_t length);
     248
     249#define json_error_max 128
     250json_value * json_parse_ex (json_settings * settings,
     251                            const json_char * json,
     252                            size_t length,
     253                            char * error);
    182254
    183255void json_value_free (json_value *);
     256
     257
     258/* Not usually necessary, unless you used a custom mem_alloc and now want to
     259 * use a custom mem_free.
     260 */
     261void json_value_free_ex (json_settings * settings,
     262                         json_value *);
    184263
    185264
Note: See TracChangeset for help on using the changeset viewer.