Vuo  2.0.0
VuoJsonUtilities.cc
Go to the documentation of this file.
1 
10 #include "VuoJsonUtilities.hh"
11 
17 string VuoJsonUtilities::parseString(json_object *o, string key, string defaultString, bool *foundValue)
18 {
19  string s = defaultString;
20  if (foundValue)
21  *foundValue = false;
22 
23  json_object *stringObject = NULL;
24  if (json_object_object_get_ex(o, key.c_str(), &stringObject))
25  {
26  if (json_object_get_type(stringObject) == json_type_string)
27  {
28  s = json_object_get_string(stringObject);
29  if (foundValue)
30  *foundValue = true;
31  }
32  }
33 
34  return s;
35 }
36 
42 string VuoJsonUtilities::parseObjectString(json_object *o, string outerKey, string innerKey, string defaultString, bool *foundValue)
43 {
44  string s = defaultString;
45  if (foundValue)
46  *foundValue = false;
47 
48  json_object *outerObject = NULL;
49  if (json_object_object_get_ex(o, outerKey.c_str(), &outerObject))
50  {
51  json_object *innerObject = NULL;
52  if (json_object_object_get_ex(outerObject, innerKey.c_str(), &innerObject))
53  {
54  if (json_object_get_type(innerObject) == json_type_string)
55  {
56  s = json_object_get_string(innerObject);
57  if (foundValue)
58  *foundValue = true;
59  }
60  }
61  }
62 
63  return s;
64 }
65 
71 int VuoJsonUtilities::parseInt(json_object *o, string key, int defaultInt, bool *foundValue)
72 {
73  int i = defaultInt;
74  if (foundValue)
75  *foundValue = false;
76 
77  json_object *intObject = NULL;
78  if (json_object_object_get_ex(o, key.c_str(), &intObject))
79  {
80  if (json_object_get_type(intObject) == json_type_int)
81  {
82  i = json_object_get_int(intObject);
83  if (foundValue)
84  *foundValue = true;
85  }
86  }
87 
88  return i;
89 }
90 
96 bool VuoJsonUtilities::parseBool(json_object *o, string key, bool defaultBool, bool *foundValue)
97 {
98  bool b = defaultBool;
99  if (foundValue)
100  *foundValue = false;
101 
102  json_object *boolObject = NULL;
103  if (json_object_object_get_ex(o, key.c_str(), &boolObject))
104  {
105  if (json_object_get_type(boolObject) == json_type_boolean)
106  {
107  b = json_object_get_boolean(boolObject);
108  if (foundValue)
109  *foundValue = true;
110  }
111  }
112 
113  return b;
114 }
115 
122 {
123  vector<string> items;
124  json_object *arrayObject = NULL;
125  if (json_object_object_get_ex(o, key.c_str(), &arrayObject))
126  {
127  if (json_object_get_type(arrayObject) == json_type_array)
128  {
129  int itemCount = json_object_array_length(arrayObject);
130  for (int i = 0; i < itemCount; ++i)
131  {
132  json_object *item = json_object_array_get_idx(arrayObject, i);
133  if (json_object_get_type(item) == json_type_string)
134  items.push_back( json_object_get_string(item) );
135  }
136  }
137  }
138  return items;
139 }
140 
144 json_object *VuoJsonUtilities::getJson(vector<string> strings)
145 {
146  json_object *a = json_object_new_array();
147  for (vector<string>::iterator i = strings.begin(); i != strings.end(); ++i)
148  json_object_array_add(a, json_object_new_string(i->c_str()));
149  return a;
150 }