00001 #include <string.h>
00002 #include <ctype.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <errno.h>
00006 #include <math.h>
00007 #include "debug.h"
00008 #include "bits.h"
00009 #include "json_object.h"
00010 #include "json_more_utils.h"
00011 #include "json_tokener.h"
00012 #include "JSON_checker.h"
00013 #include "linkhash.h"
00014
00015
00016 double convert_to_double(JO jo);
00017
00018 #ifndef NAN
00019 #define NAN strtod("NaN",0)
00020 #endif
00021
00022 int json_stream_skip(FILE*f) {
00023 int count = 0;
00024 JSON_checker_init();
00025 while(1) {
00026 char c;
00027 if(1 != fread(&c,1,1,f)) {
00028 if(feof(f)) {
00029 if(count==0) return 0;
00030 mc_error("EOF after %d bytes were read.\n", count);
00031 return 0;
00032 }
00033 mc_error("Reading error: %s\n", strerror(errno));
00034 return 0;
00035 } else {
00036 if(!JSON_checker_push(c)) {
00037 mc_error("Malformed JSON object. (read %d bytes).\n", count);
00038 return 0;
00039 }
00040
00041 if(JSON_checker_finished())
00042 return 1;
00043 }
00044 count++;
00045 }
00046 }
00047
00048 JO json_read_stream(FILE*f) {
00049
00050 size_t buf_size = 100000;
00051 char * buf = (char*) malloc(buf_size);
00052
00053 int count = 0;
00054
00055 JSON_checker_init();
00056 while(1) {
00057 if( ((size_t)count) > buf_size - 2) {
00058 buf_size *= 2;
00059 char * new_buf = realloc(buf, buf_size);
00060 if(!new_buf) {
00061 mc_error("Having read %d bytes, cannot allocate a block of size %d.",
00062 count, buf_size);
00063 free(buf);
00064 return 0;
00065 }
00066 buf = new_buf;
00067 }
00068 char c;
00069 if(1 != fread(&c,1,1,f)) {
00070 if(feof(f)) {
00071 if(count==0) { free(buf); return 0; }
00072 mc_error("EOF while %d were read: \n\t'%.*s'. \n", count, count, buf);
00073 free(buf); return 0;
00074 }
00075 mc_error("Reading error: %s\n", strerror(errno));
00076 return 0;
00077 } else {
00078 if(count==0 && isspace(c)) continue;
00079
00080 buf[count] = c;
00081 count++;
00082
00083 if(!JSON_checker_push(c)) {
00084 mc_error("Malformed JSON object: \n'%.*s'\n", count, buf);
00085 free(buf); return 0;
00086 }
00087
00088 if(JSON_checker_finished()) {
00089
00090 JO jo = json_tokener_parse_len(buf, count);
00091
00092 free(buf); return jo;
00093 }
00094
00095 }
00096 }
00097 }
00098
00099
00100
00101 struct json_object* json_tokener_parse_len(const char *str, int len) {
00102 struct json_tokener* tok;
00103 struct json_object* obj;
00104
00105 tok = json_tokener_new();
00106 obj = json_tokener_parse_ex(tok, str, len);
00107 if(tok->err != json_tokener_success) {
00108 obj = error_ptr(-tok->err);
00109 json_tokener_free(tok);
00110 mc_error("Malformed JSON object: \n'%.*s'\n", len, str);
00111 return 0;
00112 }
00113 json_tokener_free(tok);
00114 return obj;
00115 }
00116
00117 int jo_has_field(JO s, const char*name) {
00118 return json_object_object_get(s, name) != 0;
00119 }
00120
00121
00122 int jo_read_double_array(JO s, const char*name, double*p, int n, double when_null) {
00123 JO jo = json_object_object_get(s, name);
00124 if(!jo) {
00125
00126 return 0;
00127 }
00128
00129 return jo_read_from_double_array (jo, p, n, when_null);
00130 }
00131
00132
00133 int jo_read_from_double_array (JO jo, double *p, int n, double when_null) {
00134 if(!json_object_is_type(jo, (enum json_type) json_type_array)) {
00135 mc_error("This is not an array: '%s'\n",json_object_to_json_string(jo));
00136 return 0;
00137 }
00138
00139 {
00140 int size = json_object_array_length(jo);
00141 if(size < n) {
00142 mc_error("I expected at least %d elements, got %d. \nArray: '%s'\n",
00143 n, size, json_object_to_json_string(jo));
00144 return 0;
00145 }
00146 }
00147
00148 {
00149 int i; for(i=0;i<n;i++) {
00150 JO v = json_object_array_get_idx(jo, i);
00151 if(!v)
00152 p[i] = when_null;
00153 else
00154 if(json_object_is_type(v, (enum json_type) json_type_double)) {
00155 p[i] = json_object_get_double(v);
00156 } else
00157 if(json_object_is_type(v, (enum json_type) json_type_int)) {
00158 p[i] = json_object_get_int(v);
00159 } else
00160 p[i] = when_null;
00161 }
00162 }
00163 return 1;
00164 }
00165
00166
00167
00168
00169 int jo_read_int_array(JO s, const char*name, int*p, int n, int when_null) {
00170 int size, i;
00171 JO jo = json_object_object_get(s, name);
00172 if(!jo) {
00173
00174 return 0;
00175 }
00176 if(!json_object_is_type(jo, (enum json_type) json_type_array)) {
00177 mc_error("This is not an array: '%s'\n",json_object_to_json_string(jo));
00178 return 0;
00179 }
00180 size = json_object_array_length(jo);
00181 if(size < n) {
00182 mc_error("I expected at least %d elements, got %d. \nArray: '%s'\n",
00183 n, size, json_object_to_json_string(jo));
00184 return 0;
00185 }
00186 for(i=0;i<n;i++) {
00187 JO v = json_object_array_get_idx(jo, i);
00188 if(!v || !json_object_is_type(v, (enum json_type) json_type_int))
00189 p[i] = when_null;
00190 else
00191 p[i] = json_object_get_int(v);
00192 }
00193 return 1;
00194 }
00195
00196 JO jo_double_or_null(double v) {
00197 return (v == v) ?
00198 json_object_new_double(v) : jo_new_null() ;
00199 }
00200
00201 JO jo_new_double_array(const double *v, int n) {
00202 JO array = json_object_new_array();
00203 int i; for(i=0;i<n;i++) {
00204 json_object_array_add(array, jo_double_or_null(v[i]));
00205 }
00206 return array;
00207 }
00208
00209 JO jo_new_int_array(const int *v, int n) {
00210 JO array = json_object_new_array();
00211 int i; for(i=0;i<n;i++) {
00212 json_object_array_add(array, json_object_new_int(v[i]));
00213 }
00214 return array;
00215 }
00216
00218 int json_to_int(JO jo, int*ptr) {
00219
00220 if(!jo) {
00221
00222 return 0;
00223 }
00224
00225 if(!json_object_is_type(jo, (enum json_type) json_type_int)) {
00226 mc_error("I was looking for a int, instead got '%s'.\n",
00227 json_object_to_json_string(jo));
00228 return 0;
00229 }
00230
00231 *ptr = json_object_get_int(jo);
00232
00233 return 1;
00234 }
00235
00236 int json_to_double(JO jo, double*ptr) {
00237 if(json_object_is_type(jo, (enum json_type) json_type_double)) {
00238 *ptr = json_object_get_double(jo);
00239 return 1;
00240 } else if(json_object_is_type(jo, (enum json_type) json_type_int)) {
00241 *ptr = json_object_get_int(jo);
00242 return 1;
00243 } else{
00244 *ptr = NAN;
00245 return 0;
00246 }
00247 }
00248
00249 int jo_read_int(JO jo, const char*name, int*p) {
00250 JO v = json_object_object_get(jo, name);
00251 if(!v) {
00252 return 0;
00253 }
00254 return json_to_int(v, p);
00255 }
00256
00257 double convert_to_double(JO jo) {
00258 if(json_object_is_type(jo, (enum json_type) json_type_double))
00259 return json_object_get_double(jo);
00260 else if(json_object_is_type(jo, (enum json_type) json_type_int)) {
00261 return json_object_get_int(jo);
00262 }
00263 else
00264 return NAN;
00265 }
00266
00267 int jo_read_double(JO jo, const char*name, double*p) {
00268 JO v = json_object_object_get(jo, name);
00269
00270 if(!v) {
00271
00272 return 0;
00273 }
00274
00275 *p = convert_to_double(v);
00276 return 1;
00277 }
00278
00279
00280 int jo_read_string(JO jo, const char*name, char*dest_string, size_t max_len) {
00281 JO v = json_object_object_get(jo, name);
00282 if(!v) return 0;
00283 if(json_object_is_type(v, (enum json_type) json_type_string)) {
00284 strncpy(dest_string, json_object_get_string(v), max_len);
00285 return 1;
00286 } else {
00287 strncpy(dest_string, "<string not found>", max_len);
00288 return 0;
00289 }
00290 }
00291
00292 void jo_add_string(JO root, const char*name, const char*v) {
00293 jo_add(root, name, jo_new_string(v));
00294 }
00295
00296 void jo_add_double_array(JO root, const char*name, const double*v, int n) {
00297 jo_add(root, name, jo_new_double_array(v, n));
00298 }
00299
00300 void jo_add_int_array(JO root, const char*name, const int*v, int n) {
00301 jo_add(root, name, jo_new_int_array(v, n));
00302 }
00303
00304 void jo_add_int(JO root, const char*name, int v) {
00305 jo_add(root, name, jo_new_int(v));
00306 }
00307
00308 void jo_add_double(JO root, const char*name, double v) {
00309 jo_add(root, name, jo_double_or_null(v));
00310 }
00311
00312 JO json_parse(const char*str) {
00313 return json_tokener_parse_len(str, (int)strlen(str));
00314 }
00315
00316 const char* json_write(JO jo) {
00317 return json_object_to_json_string(jo);
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331