1 /* Copyright 2018, UCAR/Unidata. 2 Copyright 2018 Unidata 3 4 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 6 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 8 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 10 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 */ 14 15 #ifndef NCJSON_H 16 #define NCJSON_H 1 17 18 #include "vtk_netcdf_mangle.h" 19 20 /* Json object sorts (note use of term sort rather than e.g. type or discriminant) */ 21 #define NCJ_UNDEF 0 22 #define NCJ_STRING 1 23 #define NCJ_INT 2 24 #define NCJ_DOUBLE 3 25 #define NCJ_BOOLEAN 4 26 #define NCJ_DICT 5 27 #define NCJ_ARRAY 6 28 #define NCJ_NULL 7 29 30 #define NCJ_NSORTS 8 31 32 /* No flags are currently defined, but the argument is a placeholder */ 33 34 35 /* Define a struct to store primitive values 36 as unquoted strings. The sort will 37 provide more info. 38 Do not bother with a union since 39 the amount of saved space is minimal. 40 */ 41 42 typedef struct NCjson { 43 int sort; /* of this object */ 44 char* string; /* sort != DICT|ARRAY */ 45 struct NCjlist { 46 int len; 47 struct NCjson** contents; 48 } list; /* sort == DICT|ARRAY */ 49 } NCjson; 50 51 /* Support Windows declspec */ 52 #ifndef EXTERNL 53 # ifdef _WIN32 54 # ifdef NCJSON_INTERNAL /* define when compiling code */ 55 # define EXTERNL __declspec(dllexport) extern 56 # else 57 # define EXTERNL __declspec(dllimport) extern 58 # endif 59 # else /* !_WIN32 */ 60 # define EXTERNL extern 61 # endif 62 #endif /* !defined EXTERNL */ 63 64 #if defined(__cplusplus) 65 extern "C" { 66 #endif 67 68 /* int return value is either 1 (ok) or 0 (failure) */ 69 70 /* Parse */ 71 EXTERNL int NCJparse(const char* text, unsigned flags, NCjson** jsonp); 72 73 /* Build */ 74 EXTERNL int NCJnew(int sort, NCjson** object); 75 76 /* Recursively free NCjson instance */ 77 EXTERNL void NCJreclaim(NCjson*); 78 79 /* Assign a nul terminated string value to an NCjson object as its contents */ 80 EXTERNL int NCJnewstring(int sort, const char* value, NCjson** jsonp); 81 82 /* Assign a counted string value to an NCjson object as its contents */ 83 EXTERNL int NCJnewstringn(int sort, size_t len, const char* value, NCjson** jsonp); 84 85 /* Append value to an array or dict object. */ 86 EXTERNL int NCJappend(NCjson* object, NCjson* value); 87 88 /* Insert key-value pair into a dict object. key will be copied */ 89 EXTERNL int NCJinsert(NCjson* object, char* key, NCjson* value); 90 91 /* Unparser to convert NCjson object to text in buffer */ 92 EXTERNL int NCJunparse(const NCjson* json, unsigned flags, char** textp); 93 94 /* Utilities */ 95 EXTERNL int NCJaddstring(NCjson*, int sort, const char* s); 96 EXTERNL int NCJdictget(const NCjson* dict, const char* key, NCjson** valuep); 97 98 /* dump NCjson* object to output file */ 99 EXTERNL void NCJdump(const NCjson* json, unsigned flags, FILE*); 100 101 /* Convert one json sort to value of another type; don't use union so we can know when to reclaim sval */ 102 struct NCJconst {int bval; long long ival; double dval; char* sval;}; 103 EXTERNL int NCJcvt(const NCjson* value, int outsort, struct NCJconst* output); 104 105 /* Deep clone a json object */ 106 EXTERNL int NCJclone(const NCjson* json, NCjson** clonep); 107 108 /* Getters */ 109 #define NCJsort(x) ((x)->sort) 110 #define NCJstring(x) ((x)->string) 111 #define NCJlength(x) ((x)==NULL ? 0 : (x)->list.len) 112 #define NCJcontents(x) ((x)->list.contents) 113 #define NCJith(x,i) ((x)->list.contents[i]) 114 115 /* Setters */ 116 #define NCJsetsort(x,s) (x)->sort=(s) 117 #define NCJsetstring(x,y) (x)->string=(y) 118 #define NCJsetcontents(x,c) (x)->list.contents=(c) 119 #define NCJsetlength(x,l) (x)->list.len=(l) 120 121 /* Misc */ 122 #define NCJisatomic(j) ((j)->sort != NCJ_ARRAY && (j)->sort != NCJ_DICT && (j)->sort != NCJ_NULL && (j)->sort != NCJ_UNDEF) 123 124 #if defined(__cplusplus) 125 } 126 #endif 127 128 #endif /*NCJSON_H*/ 129