json.h

00001 
00002 /* vim: set et ts=3 sw=3 ft=c:
00003  *
00004  * Copyright (C) 2012 James McLaughlin et al.  All rights reserved.
00005  * https://github.com/udp/json-parser
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *   notice, this list of conditions and the following disclaimer.
00013  *
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *   notice, this list of conditions and the following disclaimer in the
00016  *   documentation and/or other materials provided with the distribution.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00022  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  */
00030 
00031 #ifndef _JSON_H
00032 #define _JSON_H
00033 
00034 #ifndef json_char
00035    #define json_char char
00036 #endif
00037 
00038 #ifndef json_int_t
00039    #ifndef _MSC_VER
00040       #include <inttypes.h>
00041       #define json_int_t int64_t
00042    #else
00043       #define json_int_t __int64
00044    #endif
00045 #endif
00046 
00047 #include <stdlib.h>
00048 
00049 #ifdef __cplusplus
00050 
00051    #include <string.h>
00052 
00053    extern "C"
00054    {
00055 
00056 #endif
00057 
00058 typedef struct
00059 {
00060    unsigned long max_memory;
00061    int settings;
00062 
00063    /* Custom allocator support (leave null to use malloc/free)
00064     */
00065 
00066    void * (* mem_alloc) (size_t, int zero, void * user_data);
00067    void (* mem_free) (void *, void * user_data);
00068 
00069    void * user_data;  /* will be passed to mem_alloc and mem_free */
00070 
00071 } json_settings;
00072 
00073 #define json_relaxed_commas 1
00074 
00075 typedef enum
00076 {
00077    json_none,
00078    json_object,
00079    json_array,
00080    json_integer,
00081    json_double,
00082    json_string,
00083    json_boolean,
00084    json_null
00085 
00086 } json_type;
00087 
00088 extern const struct _json_value json_value_none;
00089 
00090 typedef struct _json_value
00091 {
00092    struct _json_value * parent;
00093 
00094    json_type type;
00095 
00096    union
00097    {
00098       int boolean;
00099       json_int_t integer;
00100       double dbl;
00101 
00102       struct
00103       {
00104          unsigned int length;
00105          json_char * ptr; /* null terminated */
00106 
00107       } string;
00108 
00109       struct
00110       {
00111          unsigned int length;
00112 
00113          struct
00114          {
00115             json_char * name;
00116             struct _json_value * value;
00117 
00118          } * values;
00119 
00120          #if defined(__cplusplus) && __cplusplus >= 201103L
00121          decltype(values) begin () const
00122          {  return values;
00123          }
00124          decltype(values) end () const
00125          {  return values + length;
00126          }
00127          #endif
00128 
00129       } object;
00130 
00131       struct
00132       {
00133          unsigned int length;
00134          struct _json_value ** values;
00135 
00136          #if defined(__cplusplus) && __cplusplus >= 201103L
00137          decltype(values) begin () const
00138          {  return values;
00139          }
00140          decltype(values) end () const
00141          {  return values + length;
00142          }
00143          #endif
00144 
00145       } array;
00146 
00147    } u;
00148 
00149    union
00150    {
00151       struct _json_value * next_alloc;
00152       void * object_mem;
00153 
00154    } _reserved;
00155 
00156 
00157    /* Some C++ operator sugar */
00158 
00159    #ifdef __cplusplus
00160 
00161       public:
00162 
00163          inline _json_value ()
00164          {  memset (this, 0, sizeof (_json_value));
00165          }
00166 
00167          inline const struct _json_value &operator [] (int index) const
00168          {
00169             if (type != json_array || index < 0
00170                      || ((unsigned int) index) >= u.array.length)
00171             {
00172                return json_value_none;
00173             }
00174 
00175             return *u.array.values [index];
00176          }
00177 
00178          inline const struct _json_value &operator [] (const char * index) const
00179          { 
00180             if (type != json_object)
00181                return json_value_none;
00182 
00183             for (unsigned int i = 0; i < u.object.length; ++ i)
00184                if (!strcmp (u.object.values [i].name, index))
00185                   return *u.object.values [i].value;
00186 
00187             return json_value_none;
00188          }
00189 
00190          inline operator const char * () const
00191          {  
00192             switch (type)
00193             {
00194                case json_string:
00195                   return u.string.ptr;
00196 
00197                default:
00198                   return "";
00199             };
00200          }
00201 
00202          inline operator json_int_t () const
00203          {  
00204             switch (type)
00205             {
00206                case json_integer:
00207                   return u.integer;
00208 
00209                case json_double:
00210                   return (json_int_t) u.dbl;
00211 
00212                default:
00213                   return 0;
00214             };
00215          }
00216 
00217          inline operator bool () const
00218          {  
00219             if (type != json_boolean)
00220                return false;
00221 
00222             return u.boolean != 0;
00223          }
00224 
00225          inline operator double () const
00226          {  
00227             switch (type)
00228             {
00229                case json_integer:
00230                   return (double) u.integer;
00231 
00232                case json_double:
00233                   return u.dbl;
00234 
00235                default:
00236                   return 0;
00237             };
00238          }
00239 
00240    #endif
00241 
00242 } json_value;
00243 
00244 json_value * json_parse (const json_char * json,
00245                          size_t length);
00246 
00247 json_value * json_parse_ex (json_settings * settings,
00248                             const json_char * json,
00249                             size_t length,
00250                             char * error);
00251 
00252 void json_value_free (json_value *);
00253 
00254 
00255 /* Not usually necessary, unless you used a custom mem_alloc and now want to
00256  * use a custom mem_free.
00257  */
00258 void json_value_free_ex (json_settings * settings,
00259                          json_value *);
00260 
00261 
00262 #ifdef __cplusplus
00263    } /* extern "C" */
00264 #endif
00265 
00266 #endif
00267 
00268 

Generated on 4 Jun 2015 for cctools by  doxygen 1.6.1