json_more_utils.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <math.h>
7 #include "debug.h"
8 #include "bits.h"
9 #include "json_object.h"
10 #include "json_more_utils.h"
11 #include "json_tokener.h"
12 #include "JSON_checker.h"
13 #include "linkhash.h"
14 
15 
16 double convert_to_double(JO jo);
17 
18 #ifndef NAN
19 #define NAN strtod("NaN",0)
20 #endif
21 
22 int json_stream_skip(FILE*f) {
23  int count = 0;
25  while(1) {
26  char c;
27  if(1 != fread(&c,1,1,f)) {
28  if(feof(f)) {
29  if(count==0) return 0;
30  mc_error("EOF after %d bytes were read.\n", count);
31  return 0;
32  }
33  mc_error("Reading error: %s\n", strerror(errno));
34  return 0;
35  } else {
36  if(!JSON_checker_push(c)) {
37  mc_error("Malformed JSON object. (read %d bytes).\n", count);
38  return 0;
39  }
40 
42  return 1;
43  }
44  count++;
45  }
46 }
47 
49 
50  size_t buf_size = 100000;
51  char * buf = (char*) malloc(buf_size);
52 
53  int count = 0;
54 
56  while(1) {
57  if( ((size_t)count) > buf_size - 2) {
58  buf_size *= 2;
59  char * new_buf = realloc(buf, buf_size);
60  if(!new_buf) {
61  mc_error("Having read %d bytes, cannot allocate a block of size %d.",
62  count, buf_size);
63  free(buf);
64  return 0;
65  }
66  buf = new_buf;
67  }
68  char c;
69  if(1 != fread(&c,1,1,f)) {
70  if(feof(f)) {
71  if(count==0) { free(buf); return 0; }
72  mc_error("EOF while %d were read: \n\t'%.*s'. \n", count, count, buf);
73  free(buf); return 0;
74  }
75  mc_error("Reading error: %s\n", strerror(errno));
76  return 0;
77  } else {
78  if(count==0 && isspace(c)) continue;
79 
80  buf[count] = c;
81  count++;
82 
83  if(!JSON_checker_push(c)) {
84  mc_error("Malformed JSON object: \n'%.*s'\n", count, buf);
85  free(buf); return 0;
86  }
87 
88  if(JSON_checker_finished()) {
89 /* sm_de("Found object.\n"); */
90  JO jo = json_tokener_parse_len(buf, count);
91 
92  free(buf); return jo;
93  }
94 
95  }
96  }
97 }
98 
99 
100 
101 struct json_object* json_tokener_parse_len(const char *str, int len) {
102  struct json_tokener* tok;
103  struct json_object* obj;
104 
105  tok = json_tokener_new();
106  obj = json_tokener_parse_ex(tok, str, len);
107  if(tok->err != json_tokener_success) {
108  obj = error_ptr(-tok->err);
109  json_tokener_free(tok);
110  mc_error("Malformed JSON object: \n'%.*s'\n", len, str);
111  return 0;
112  }
113  json_tokener_free(tok);
114  return obj;
115 }
116 
117 int jo_has_field(JO s, const char*name) {
118  return json_object_object_get(s, name) != 0;
119 }
120 
121 
122 int jo_read_double_array(JO s, const char*name, double*p, int n, double when_null) {
123  JO jo = json_object_object_get(s, name);
124  if(!jo) {
125 /* mc_error("Field '%s' not found.\n", name); */
126  return 0;
127  }
128 
129  return jo_read_from_double_array (jo, p, n, when_null);
130 }
131 
132 /* Returns 0 if jo is not a double array, or its length is not n */
133 int jo_read_from_double_array (JO jo, double *p, int n, double when_null) {
135  mc_error("This is not an array: '%s'\n",json_object_to_json_string(jo));
136  return 0;
137  }
138 
139  {
140  int size = json_object_array_length(jo);
141  if(size < n) {
142  mc_error("I expected at least %d elements, got %d. \nArray: '%s'\n",
143  n, size, json_object_to_json_string(jo));
144  return 0;
145  }
146  }
147 
148  {
149  int i; for(i=0;i<n;i++) {
150  JO v = json_object_array_get_idx(jo, i);
151  if(!v)
152  p[i] = when_null;
153  else
155  p[i] = json_object_get_double(v);
156  } else
158  p[i] = json_object_get_int(v);
159  } else
160  p[i] = when_null;
161  }
162  }
163  return 1;
164 }
165 
166 
167 
168 
169 int jo_read_int_array(JO s, const char*name, int*p, int n, int when_null) {
170  int size, i;
171  JO jo = json_object_object_get(s, name);
172  if(!jo) {
173 /* mc_error("Field '%s' not found.\n", name); */
174  return 0;
175  }
177  mc_error("This is not an array: '%s'\n",json_object_to_json_string(jo));
178  return 0;
179  }
180  size = json_object_array_length(jo);
181  if(size < n) {
182  mc_error("I expected at least %d elements, got %d. \nArray: '%s'\n",
183  n, size, json_object_to_json_string(jo));
184  return 0;
185  }
186  for(i=0;i<n;i++) {
187  JO v = json_object_array_get_idx(jo, i);
188  if(!v || !json_object_is_type(v, (enum json_type) json_type_int))
189  p[i] = when_null;
190  else
191  p[i] = json_object_get_int(v);
192  }
193  return 1;
194 }
195 
197  return (v == v) ? /* NAN is null */
199 }
200 
201 JO jo_new_double_array(const double *v, int n) {
202  JO array = json_object_new_array();
203  int i; for(i=0;i<n;i++) {
205  }
206  return array;
207 }
208 
209 JO jo_new_int_array(const int *v, int n) {
210  JO array = json_object_new_array();
211  int i; for(i=0;i<n;i++) {
213  }
214  return array;
215 }
216 
218 int json_to_int(JO jo, int*ptr) {
219 
220  if(!jo) {
221 /* mc_error("Field '%s' not found.\n", name); */
222  return 0;
223  }
224 
225  if(!json_object_is_type(jo, (enum json_type) json_type_int)) {
226  mc_error("I was looking for a int, instead got '%s'.\n",
228  return 0;
229  }
230 
231  *ptr = json_object_get_int(jo);
232 
233  return 1;
234 }
235 
236 int json_to_double(JO jo, double*ptr) {
238  *ptr = json_object_get_double(jo);
239  return 1;
240  } else if(json_object_is_type(jo, (enum json_type) json_type_int)) {
241  *ptr = json_object_get_int(jo);
242  return 1;
243  } else{
244  *ptr = NAN;
245  return 0;
246  }
247 }
248 
249 int jo_read_int(JO jo, const char*name, int*p) {
250  JO v = json_object_object_get(jo, name);
251  if(!v) {
252  return 0;
253  }
254  return json_to_int(v, p);
255 }
256 
257 double convert_to_double(JO jo) {
259  return json_object_get_double(jo);
260  else if(json_object_is_type(jo, (enum json_type) json_type_int)) {
261  return json_object_get_int(jo);
262  }
263  else
264  return NAN;
265 }
266 
267 int jo_read_double(JO jo, const char*name, double*p) {
268  JO v = json_object_object_get(jo, name);
269 
270  if(!v) {
271 /* mc_error("Field '%s' not found.\n", name); */
272  return 0;
273  }
274 
275  *p = convert_to_double(v);
276  return 1;
277 }
278 
279 
280 int jo_read_string(JO jo, const char*name, char*dest_string, size_t max_len) {
281  JO v = json_object_object_get(jo, name);
282  if(!v) return 0;
284  strncpy(dest_string, json_object_get_string(v), max_len);
285  return 1;
286  } else {
287  strncpy(dest_string, "<string not found>", max_len);
288  return 0;
289  }
290 }
291 
292 void jo_add_string(JO root, const char*name, const char*v) {
293  jo_add(root, name, jo_new_string(v));
294 }
295 
296 void jo_add_double_array(JO root, const char*name, const double*v, int n) {
297  jo_add(root, name, jo_new_double_array(v, n));
298 }
299 
300 void jo_add_int_array(JO root, const char*name, const int*v, int n) {
301  jo_add(root, name, jo_new_int_array(v, n));
302 }
303 
304 void jo_add_int(JO root, const char*name, int v) {
305  jo_add(root, name, jo_new_int(v));
306 }
307 
308 void jo_add_double(JO root, const char*name, double v) {
309  jo_add(root, name, jo_double_or_null(v));
310 }
311 
312 JO json_parse(const char*str) {
313  return json_tokener_parse_len(str, (int)strlen(str));
314 }
315 
316 const char* json_write(JO jo) {
317  return json_object_to_json_string(jo);
318 }
319 
320 /*
321 JO find_object_with_name(JO root, const char*name) {
322  json_object_object_foreach(root, key, val) {
323  if(!strcmp(key, name)) return root;
324  if(json_object_is_type(val, json_type_object)) {
325  JO jo = find_object_with_name(val, name);
326  if(jo) return jo;
327  }
328  }
329  return 0;
330 }*/
331 
char * json_object_get_string(struct json_object *this)
Definition: json_object.c:452
int json_object_array_length(struct json_object *this)
Definition: json_object.c:516
int jo_read_string(JO jo, const char *name, char *dest_string, size_t max_len)
double json_object_get_double(struct json_object *this)
Definition: json_object.c:395
const char * json_write(JO jo)
struct json_object * json_object_object_get(struct json_object *this, const char *key)
Definition: json_object.c:277
#define NAN
enum json_tokener_error err
Definition: json_tokener.h:74
struct json_object * json_object_new_double(double d)
Definition: json_object.c:386
int json_object_is_type(struct json_object *this, int type)
Definition: json_object.c:184
void jo_add_int_array(JO root, const char *name, const int *v, int n)
int jo_read_double(JO jo, const char *name, double *p)
void json_tokener_free(struct json_tokener *tok)
Definition: json_tokener.c:66
#define jo_new_int
#define error_ptr(error)
Definition: bits.h:24
int json_object_get_int(struct json_object *this)
Definition: json_object.c:341
int json_stream_skip(FILE *f)
void mc_error(const char *msg,...)
Definition: debug.c:72
#define jo_add
int json_object_array_add(struct json_object *this, struct json_object *val)
Definition: json_object.c:522
struct json_object * json_tokener_parse_len(const char *str, int len)
#define jo_new_null()
JO jo_new_double_array(const double *v, int n)
int json_to_int(JO jo, int *ptr)
struct @0 p
int JSON_checker_finished()
Definition: JSON_checker.c:263
const char * json_object_to_json_string(struct json_object *this)
Definition: json_object.c:199
int JSON_checker_push(int b)
Definition: JSON_checker.c:287
struct json_object * json_object_new_array()
Definition: json_object.c:495
void jo_add_string(JO root, const char *name, const char *v)
double convert_to_double(JO jo)
int json_to_double(JO jo, double *ptr)
#define jo_new_string
int jo_read_double_array(JO s, const char *name, double *p, int n, double when_null)
void jo_add_double_array(JO root, const char *name, const double *v, int n)
json_type
Definition: json_object.h:37
struct json_object * json_tokener_parse_ex(struct json_tokener *tok, const char *str, int len)
Definition: json_tokener.c:133
struct json_object * json_object_new_int(int i)
Definition: json_object.c:332
void JSON_checker_init()
Definition: JSON_checker.c:257
JO json_parse(const char *str)
void jo_add_double(JO root, const char *name, double v)
JO json_read_stream(FILE *f)
struct json_tokener * json_tokener_new()
Definition: json_tokener.c:58
int jo_read_from_double_array(JO jo, double *p, int n, double when_null)
struct json_object * json_object_array_get_idx(struct json_object *this, int idx)
Definition: json_object.c:535
int jo_read_int_array(JO s, const char *name, int *p, int n, int when_null)
char buf[100]
Definition: ld_recover.c:87
int jo_read_int(JO jo, const char *name, int *p)
void jo_add_int(JO root, const char *name, int v)
JO jo_double_or_null(double v)
int jo_has_field(JO s, const char *name)
JO jo_new_int_array(const int *v, int n)


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23