MACSio  0.9
Multi-purpose, Application-Centric, Scalable I/O Proxy App
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
test1.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <string.h>
5 #include <assert.h>
6 
7 #include "json.h"
8 #include "parse_flags.h"
9 
10 static int sort_fn (const void *j1, const void *j2)
11 {
12  json_object * const *jso1, * const *jso2;
13  int i1, i2;
14 
15  jso1 = (json_object* const*)j1;
16  jso2 = (json_object* const*)j2;
17  if (!*jso1 && !*jso2)
18  return 0;
19  if (!*jso1)
20  return -1;
21  if (!*jso2)
22  return 1;
23 
24  i1 = json_object_get_int(*jso1);
25  i2 = json_object_get_int(*jso2);
26 
27  return i1 - i2;
28 }
29 
30 #ifdef TEST_FORMATTED
31 #define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags)
32 #else
33 /* no special define */
34 #endif
35 
36 int main(int argc, char **argv)
37 {
38  json_object *my_string, *my_int, *my_object, *my_array;
39  int i;
40 #ifdef TEST_FORMATTED
41  int sflags = 0;
42 #endif
43 
44  MC_SET_DEBUG(1);
45 
46 #ifdef TEST_FORMATTED
47  sflags = parse_flags(argc, argv);
48 #endif
49 
50  my_string = json_object_new_string("\t");
51  printf("my_string=%s\n", json_object_get_string(my_string));
52  printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
53  json_object_put(my_string);
54 
55  my_string = json_object_new_string("\\");
56  printf("my_string=%s\n", json_object_get_string(my_string));
57  printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
58  json_object_put(my_string);
59 
60  my_string = json_object_new_string("foo");
61  printf("my_string=%s\n", json_object_get_string(my_string));
62  printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
63 
64  my_int = json_object_new_int(9);
65  printf("my_int=%d\n", json_object_get_int(my_int));
66  printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
67 
68  my_array = json_object_new_array();
73  printf("my_array=\n");
74  for(i=0; i < json_object_array_length(my_array); i++)
75  {
76  json_object *obj = json_object_array_get_idx(my_array, i);
77  printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
78  }
79  printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
80 
81  json_object_put(my_array);
82 
83  my_array = json_object_new_array();
88  printf("my_array=\n");
89  for(i=0; i < json_object_array_length(my_array); i++)
90  {
91  json_object *obj = json_object_array_get_idx(my_array, i);
92  printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
93  }
94  printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
96  printf("my_array=\n");
97  for(i=0; i < json_object_array_length(my_array); i++)
98  {
99  json_object *obj = json_object_array_get_idx(my_array, i);
100  printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
101  }
102  printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
103 
104  my_object = json_object_new_object();
105  json_object_object_add(my_object, "abc", json_object_new_int(12));
106  json_object_object_add(my_object, "foo", json_object_new_string("bar"));
107  json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
108  json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
109  json_object_object_add(my_object, "baz", json_object_new_string("bang"));
110 
111  json_object *baz_obj = json_object_new_string("fark");
112  json_object_get(baz_obj);
113  json_object_object_add(my_object, "baz", baz_obj);
114  json_object_object_del(my_object, "baz");
115 
116  /* baz_obj should still be valid */
117  printf("baz_obj.to_string()=%s\n", json_object_to_json_string(baz_obj));
118  json_object_put(baz_obj);
119 
120  /*json_object_object_add(my_object, "arr", my_array);*/
121  printf("my_object=\n");
122  json_object_object_foreach(my_object, key, val)
123  {
124  printf("\t%s: %s\n", key, json_object_to_json_string(val));
125  }
126  printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
127 
128  json_object_put(my_string);
129  json_object_put(my_int);
130  json_object_put(my_object);
131  json_object_put(my_array);
132 
133  {
134  int i;
135  int dims[3] = {2, 7, 3};
136  int nvals = dims[0]*dims[1]*dims[2];
138  float *fbuf = 0;
139  int *ibuf;
140  for (i = 0; i < nvals; i++)
141  {
142  double *p = (double*)json_object_extarr_data(extarr)+i;
143  *p = (double) i/2.0;
144  }
145  printf("extarr.to_string()=%s\n", json_object_to_json_string(extarr));
146 
147  json_object_extarr_data_as_float(extarr, &fbuf);
148  for (i = 0; i < nvals; i++)
149  printf("fbuf[%d]=%f\n",i,fbuf[i]);
150  free(fbuf);
151 
152  ibuf = (int *) malloc(nvals * sizeof(int));
153  json_object_extarr_data_as_int(extarr, &ibuf);
154  for (i = 0; i < nvals; i++)
155  printf("ibuf[%d]=%d\n",i,ibuf[i]);
156  free(ibuf);
157 
158  json_object_put(extarr);
159  }
160  {
161  const char *input = "( 6, 3, 2, 7, 3, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20, 20.5 )";
162  struct json_object *parse_result = json_tokener_parse((char*)input);
163  const char *unjson = json_object_get_string(parse_result);
164  printf("parse_result=%s\n", unjson);
165  }
166 
167  {
168  json_object *filobj = json_object_from_file("foo.json");
169  json_object *tmpobj;
170  printf("filobj (without extarr vals) =%s\n",
173  printf("filobj=%s, nbytes=%d\n", json_object_to_json_string(filobj),
174  json_object_object_nbytes(filobj));
175  printf("\"steve/cameron/b\" = %d, \"abc\" = %d\n",
176  json_object_path_get_int(filobj, "steve/cameron/b"),
177  json_object_path_get_int(filobj, "abc"));
178  if (!json_object_path_set_int(filobj, "steve/cameron/b", 37))
179  printf("path_set_int failed\n");
180  if (!json_object_path_set_int(filobj, "abc", 91))
181  printf("path_set_int failed\n");
182  printf("\"steve/cameron/b\" = %d, \"abc\" = %d\n",
183  json_object_path_get_int(filobj, "steve/cameron/b"),
184  json_object_path_get_int(filobj, "abc"));
185  printf("\"steve/cameron/b\" = %d, \"abc\" = %d\n",
186  json_object_apath_get_int(filobj, "steve/cameron/b"),
187  json_object_apath_get_int(filobj, "abc"));
188  printf("\"array/2\" = %d\n",
189  json_object_apath_get_int(filobj, "array/2"));
190  printf("\"array2/6/cameron/a\" = %d\n",
191  JsonGetInt(filobj, "array2",6,"cameron/a"));
192  printf("\"array2/6/cameron/a\" = %d\n",
193  JsonGetInt(filobj, "array2/",6,"/cameron/a"));
194  tmpobj = JsonGetObj(filobj, "array2");
195  printf("\"6/cameron/a\" = %d\n",
196  JsonGetInt(tmpobj, "",6,"/cameron/a"));
197  printf("\"array2/2\" = \"%s\"\n",
198  json_object_apath_get_string(filobj, "array2/2"));
199  printf("Finding \"cameron/a\" from root = %d\n",
200  json_object_get_int(JsonFindObj(filobj, "cameron/a")));
201  }
202  {
203  struct json_object *eobj = json_object_new_enum();
204  json_object_enum_add(eobj, "val31", 31, JSON_C_FALSE);
205  json_object_enum_add(eobj, "val276", 276, JSON_C_FALSE);
206  json_object_enum_add(eobj, "val17", 17, JSON_C_TRUE);
207  printf("eobj=%s\n", json_object_to_json_string(eobj));
208  }
209 
210  return 0;
211 }
#define JsonGetObj(OBJ,...)
Get the object at specified path.
Definition: json_object.h:771
struct json_object * json_object_new_extarr_alloc(enum json_extarr_type etype, int ndims, int const *dims)
Definition: json_object.c:1115
json_bool json_object_path_set_int(struct json_object *obj, char const *key_path, int32_t val)
Definition: json_object.c:2046
void json_object_enum_add(struct json_object *jso, char const *name, int64_t val, json_bool selected)
Add a name/value pair to an enumeration.
Definition: json_object.c:1327
void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *))
Definition: json_object.c:849
void json_object_object_del(struct json_object *obj, const char *key)
Definition: json_object.c:441
struct json_object * json_object_new_string(const char *s)
Definition: json_object.c:727
int parse_flags(int argc, char **argv)
Definition: parse_flags.c:28
struct json_object * json_object_new_object(void)
Definition: json_object.c:369
#define JSON_C_TO_STRING_NO_EXTARR_VALS
Definition: json_object.h:62
void const * json_object_extarr_data(struct json_object *jso)
Definition: json_object.c:1206
char const * json_object_apath_get_string(struct json_object *src, char const *key_path)
Query string value at specified path.
Definition: json_object.c:1852
int64_t json_object_object_nbytes(struct json_object *const obj)
Definition: json_object.c:2295
int json_object_extarr_data_as_int(struct json_object *jso, int **buf)
int main(int argc, char **argv)
Definition: test1.c:36
#define JsonFindObj(OBJ,...)
Definition: json_object.h:772
struct json_object * json_object_new_int(int32_t i)
Definition: json_object.c:495
static int sort_fn(const void *j1, const void *j2)
Definition: test1.c:10
int json_object_array_add(struct json_object *obj, struct json_object *val)
Definition: json_object.c:859
int json_object_array_length(struct json_object *obj)
Definition: json_object.c:854
#define JsonGetInt(OBJ,...)
Get the int value at specified path.
Definition: json_object.h:747
int json_object_apath_get_int(struct json_object *src, char const *key_path)
Query int value at specified path.
Definition: json_object.c:1782
struct json_object * json_object_new_array(void)
Definition: json_object.c:828
struct json_object * json_object_new_boolean(json_bool b)
Definition: json_object.c:458
#define JSON_C_TRUE
Definition: json_object.h:65
const char * json_object_get_string(struct json_object *obj)
Definition: json_object.c:751
#define MC_SET_DEBUG(x)
Definition: debug.h:60
struct json_object * json_object_new_enum(void)
Create a new, empty enumeration object.
Definition: json_object.c:1305
struct json_object * json_object_array_get_idx(struct json_object *obj, int idx)
Definition: json_object.c:870
int json_object_array_put_idx(struct json_object *obj, int idx, struct json_object *val)
Definition: json_object.c:864
struct json_object * json_object_get(struct json_object *obj)
Definition: json_object.c:147
int json_object_put(struct json_object *obj)
Definition: json_object.c:155
int32_t json_object_get_int(struct json_object *obj)
Definition: json_object.c:504
#define JSON_C_TO_STRING_SPACED
Definition: json_object.h:46
const char * json_object_to_json_string_ext(struct json_object *obj, int flags)
Definition: json_object.c:277
int json_object_extarr_data_as_float(struct json_object *jso, float **buf)
#define JSON_C_FALSE
Definition: json_object.h:64
int32_t json_object_path_get_int(struct json_object *src, char const *key_path)
Get integer value for object at specified path.
Definition: json_object.c:2103
#define json_object_object_foreach(obj, key, val)
Definition: json_object.h:387
const char * json_object_to_json_string(struct json_object *obj)
Definition: json_object.c:295
struct json_object * json_object_from_file(const char *filename)
Definition: json_util.c:68
struct json_object * json_tokener_parse(const char *str)
Definition: json_tokener.c:158
void json_object_object_add(struct json_object *obj, const char *key, struct json_object *val)
Definition: json_object.c:391