COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Cosmos::Support::Json Class Reference

#include <jsonclass.h>

Collaboration diagram for Cosmos::Support::Json:

Classes

struct  Value
 

Public Types

enum  Type : uint16_t {
  Type::Undefined, Type::Null, Type::False, Type::True,
  Type::Number, Type::String, Type::Array, Type::Object
}
 
typedef pair< string, ValueMember
 
typedef map< string, ValueObject
 
typedef vector< ValueArray
 

Public Member Functions

 Json (string json="")
 
int32_t extract_contents (string json)
 
int32_t extract_members (string::iterator &begin, string::iterator &end, Object &members)
 
int32_t extract_values (string::iterator &begin, string::iterator &end, Array &values)
 
int32_t extract_string (string::iterator &bit, string::iterator &eit, string &ostring)
 
int32_t extract_name (string::iterator &bit, string::iterator &eit, string &name)
 
int32_t extract_value (string::iterator &bit, string::iterator &eit, Value &value)
 
int32_t extract_member (string::iterator &bit, string::iterator &eit, Member &value)
 
int32_t skip_character (string::iterator &bit, string::iterator &eit, const char character)
 
int32_t skip_to_character (string::iterator &bit, string::iterator &eit, string characters)
 
int32_t skip_white (string::iterator &bit, string::iterator &eit)
 

Public Attributes

string Content
 
Type JType
 
Object ObjectContents
 
Array ArrayContents
 

Private Attributes

string::iterator begin
 
string::iterator end
 

Member Typedef Documentation

typedef pair<string, Value> Cosmos::Support::Json::Member
typedef map<string, Value> Cosmos::Support::Json::Object

Member Enumeration Documentation

enum Cosmos::Support::Json::Type : uint16_t
strong

JSON Basic Types enum defining the basic types of value to be found in a JSON member

Enumerator
Undefined 
Null 
False 
True 
Number 
String 
Array 
Object 
18  : uint16_t
19  {
20  Undefined,
21  Null,
22  False,
23  True,
24  Number,
25  String,
26  Array,
27  Object
28  };
True
Definition: inputfile.py:6
map< string, Value > Object
Definition: jsonclass.h:47
vector< Value > Array
Definition: jsonclass.h:48

Constructor & Destructor Documentation

Cosmos::Support::Json::Json ( string  json = "")
9  {
10  if (!json.empty())
11  {
12  extract_contents(json);
13  }
14  }
int32_t extract_contents(string json)
Definition: jsonclass.cpp:16

Member Function Documentation

int32_t Cosmos::Support::Json::extract_contents ( string  json)

JSON Member elements Structure containing the 2 basic parts of a JSON member, plus some supporting information. JSON Object Complete JSON Object, containing both the string of characters and a heirarchical vector of values.

17  {
18  int32_t iretn = 0;
19 
20  Content = json;
21 
22  // Determine overall type
23  string::iterator begin = json.begin();
24  string::iterator end = json.end();
25 
26  iretn = skip_to_character(begin, end, "[{");
27  switch (iretn)
28  {
29  case '[':
30  {
32  iretn = skip_character(begin, end, '[');
33  if (iretn <= 0)
34  {
35  return iretn;
36  }
37  iretn = extract_values(begin, end, ArrayContents);
38  if (iretn < 0)
39  {
40  return iretn;
41  }
42  iretn = skip_white(begin, end);
43  if (iretn < 0)
44  {
45  return iretn;
46  }
47  iretn = skip_character(begin, end, ']');
48  if (iretn <= 0)
49  {
50  return iretn;
51  }
52  }
53  break;
54  case '{':
55  {
57  iretn = skip_character(begin, end, '{');
58  if (iretn <= 0)
59  {
60  return iretn;
61  }
62  iretn = extract_members(begin, end, ObjectContents);
63  if (iretn < 0)
64  {
65  return iretn;
66  }
67  iretn = skip_white(begin, end);
68  if (iretn < 0)
69  {
70  return iretn;
71  }
72  iretn = skip_character(begin, end, '}');
73  if (iretn <= 0)
74  {
75  return iretn;
76  }
77  }
78  break;
79  default:
80  return iretn;
81  }
82  return iretn;
83  }
int32_t extract_members(string::iterator &begin, string::iterator &end, Object &members)
Definition: jsonclass.cpp:89
int iretn
Definition: rw_test.cpp:37
int32_t extract_values(string::iterator &begin, string::iterator &end, Array &values)
Definition: jsonclass.cpp:112
int32_t skip_character(string::iterator &bit, string::iterator &eit, const char character)
Definition: jsonclass.cpp:449
string::iterator begin
Definition: jsonclass.h:87
string Content
Definition: jsonclass.h:79
Type JType
Definition: jsonclass.h:80
Object ObjectContents
Definition: jsonclass.h:81
int32_t skip_to_character(string::iterator &bit, string::iterator &eit, string characters)
Definition: jsonclass.cpp:473
string::iterator end
Definition: jsonclass.h:88
Array ArrayContents
Definition: jsonclass.h:82
int32_t skip_white(string::iterator &bit, string::iterator &eit)
Definition: jsonclass.cpp:433
int32_t Cosmos::Support::Json::extract_members ( string::iterator &  begin,
string::iterator &  end,
Object members 
)

Extract JSON values. Build JSON Object from values extracted from string containing JSON.

Parameters
jsonString containing JSON values.
Returns
::Object containing heirarchical vector of ::Member
90  {
91  members.clear();
92  while (begin != end)
93  {
94  Member tmember;
95  int32_t iretn = extract_member(begin, end, tmember);
96  if (iretn < 0)
97  {
98  return iretn;
99  }
100  else if (iretn > 0)
101  {
102  members.emplace(tmember);
103  }
104  else {
105  break;
106  }
107  }
108 
109  return static_cast<int32_t>(members.size());
110  }
int32_t extract_member(string::iterator &bit, string::iterator &eit, Member &value)
Definition: jsonclass.cpp:141
int iretn
Definition: rw_test.cpp:37
string::iterator begin
Definition: jsonclass.h:87
pair< string, Value > Member
Definition: jsonclass.h:46
string::iterator end
Definition: jsonclass.h:88
int32_t Cosmos::Support::Json::extract_values ( string::iterator &  begin,
string::iterator &  end,
Array values 
)
113  {
114  values.clear();
115  while (begin != end)
116  {
117  Value tvalue;
118  int32_t iretn = extract_value(begin, end, tvalue);
119  if (iretn < 0)
120  {
121  return iretn;
122  }
123  else if (iretn > 0)
124  {
125  values.push_back(tvalue);
126  }
127  else {
128  break;
129  }
130  }
131 
132  return static_cast<int32_t>(values.size());
133  }
int iretn
Definition: rw_test.cpp:37
string::iterator begin
Definition: jsonclass.h:87
int32_t extract_value(string::iterator &bit, string::iterator &eit, Value &value)
Definition: jsonclass.cpp:158
string::iterator end
Definition: jsonclass.h:88
int32_t Cosmos::Support::Json::extract_string ( string::iterator &  begin,
string::iterator &  end,
string &  ostring 
)

Extract next JSON string. Extract the next JSON String from the c++ string represented by the supplied iterator. Leave the iterator pointing to the beginning of the next value.

Parameters
itReference to string iterator.
Returns
The discovered string, or an empty string.
544  {
545  int32_t iretn = 0;
546 
547  if (begin == end || *begin == 0)
548  return (JSON_ERROR_EOS);
549 
550  //Skip whitespace before string
551  if ((iretn = skip_white(begin, end)) < 0)
552  {
553  return iretn;
554  }
555 
556  //Skip '"' before string
557  if ((iretn = skip_character(begin, end, '"')) <= 0)
558  {
559  return iretn;
560  }
561 
562  // Start of object, get string
563  ostring.clear();
564 
565  while (begin != end)
566  {
567  if (*(begin) == '"')
568  {
569  break; // end of string
570  }
571  if (*(begin) == '\\') // escaped character
572  {
573  if (begin + 1 != end)
574  {
575  switch (*(begin+1))
576  {
577  case '"':
578  case '\\':
579  case '/':
580  ostring.push_back(*(begin+1));
581  break;
582  case 'b':
583  ostring.push_back('\b');
584  break;
585  case 'f':
586  ostring.push_back('\f');
587  break;
588  case 'n':
589  ostring.push_back('\n');
590  break;
591  case 'r':
592  ostring.push_back('\r');
593  break;
594  case 't':
595  ostring.push_back('\t');
596  break;
597  default:
598  if (begin + 2 == end)
599  {
600  begin += 2;
601  }
602  else
603  {
604  begin += 3;
605  }
606  }
607  ++begin;
608  }
609  }
610  else
611  {
612  ostring.push_back(*(begin)); // just a character
613  }
614  ++begin;
615  }
616 
617  //Skip '"' afer string
618  if ((iretn = skip_character(begin, end, '"')) <= 0)
619  {
620  return iretn;
621  }
622 
623  if (begin == end)
624  {
625  return JSON_ERROR_SCAN;
626  }
627 
628  // i2 is last character in string +1, index is length of extracted string
629 // if (ostring.size() >= JSON_MAX_DATA)
630 // {
631 // ostring.resize(JSON_MAX_DATA-1);
632 // }
633 
634  return iretn;
635  }
int iretn
Definition: rw_test.cpp:37
int32_t skip_character(string::iterator &bit, string::iterator &eit, const char character)
Definition: jsonclass.cpp:449
string::iterator begin
Definition: jsonclass.h:87
#define JSON_ERROR_EOS
Definition: cosmos-errno.h:95
#define JSON_ERROR_SCAN
Definition: cosmos-errno.h:96
string::iterator end
Definition: jsonclass.h:88
int32_t skip_white(string::iterator &bit, string::iterator &eit)
Definition: jsonclass.cpp:433
int32_t Cosmos::Support::Json::extract_name ( string::iterator &  bit,
string::iterator &  eit,
string &  name 
)
501  {
502  int32_t iretn = 1;
503 
504  if (begin == end || *begin == 0)
505  return (JSON_ERROR_EOS);
506 
507  //Skip whitespace before name
508  if ((iretn = skip_white(begin, end)) < 0)
509  {
510  return iretn;
511  }
512 
513  //Parse name
514  iretn = extract_string(begin, end, name);
515  if (iretn <= 0)
516  {
517  return iretn;
518  }
519 
520  //Skip whitespace after name
521  if ((iretn = skip_white(begin, end)) < 0)
522  {
523  return iretn;
524  }
525 
526  if ((iretn = skip_character(begin, end, ':')) <= 0)
527  return iretn;
528 
529  //Skip whitespace after seperator
530  if ((iretn = skip_white(begin, end)) < 0)
531  {
532  return iretn;
533  }
534 
535  return 1;
536  }
int iretn
Definition: rw_test.cpp:37
int32_t skip_character(string::iterator &bit, string::iterator &eit, const char character)
Definition: jsonclass.cpp:449
string::iterator begin
Definition: jsonclass.h:87
#define JSON_ERROR_EOS
Definition: cosmos-errno.h:95
string name
Definition: cubesat2obj.cpp:6
int32_t extract_string(string::iterator &bit, string::iterator &eit, string &ostring)
Definition: jsonclass.cpp:543
string::iterator end
Definition: jsonclass.h:88
int32_t skip_white(string::iterator &bit, string::iterator &eit)
Definition: jsonclass.cpp:433
int32_t Cosmos::Support::Json::extract_value ( string::iterator &  begin,
string::iterator &  end,
Value value 
)

Extract next JSON value. Extract the next JSON value from the c++ string represented by the supplied iterator. Leave the iterator pointing to the beginning of the next value.

Parameters
itReference to string iterator.
Returns
A pointer the the discovered value, or a nullptr.
159  {
160  int32_t iretn;
161 
162  iretn = skip_white(begin, end);
163  if (iretn < 0)
164  {
165  return iretn;
166  }
167 
168  value.begin = begin;
169  switch (*begin)
170  {
171  case '}':
172  case ']':
173  return 0;
174  case '{':
175  value.type = Type::Object;
176  iretn = skip_character(begin, end, '{');
177  if (iretn <= 0)
178  {
179  return iretn;
180  }
181  iretn = extract_members(begin, end, value.object);
182  if (iretn < 0)
183  {
184  return iretn;
185  }
186  iretn = skip_white(begin, end);
187  if (iretn < 0)
188  {
189  return iretn;
190  }
191  iretn = skip_character(begin, end, '}');
192  if (iretn <= 0)
193  {
194  return iretn;
195  }
196 // if (++begin == end || *begin == 0)
197 // {
198 // return JSON_ERROR_EOS;
199 // }
200  break;
201  case '[':
202  value.type = Type::Array;
203  iretn = skip_character(begin, end, '[');
204  if (iretn <= 0)
205  {
206  return iretn;
207  }
208  iretn = extract_values(begin, end, value.array);
209  if (iretn < 0)
210  {
211  return iretn;
212  }
213  iretn = skip_white(begin, end);
214  if (iretn < 0)
215  {
216  return iretn;
217  }
218  iretn = skip_character(begin, end, ']');
219  if (iretn <= 0)
220  {
221  return iretn;
222  }
223 // if (++begin == end || *begin == 0)
224 // {
225 // return JSON_ERROR_EOS;
226 // }
227  break;
228  case '"':
229  {
230  value.svalue.clear();
231  uint16_t ilen = (end - begin) + 1;
232  uint16_t i2;
233  for (i2=1; i2<ilen; i2++) //start from ptr[1] rather than ptr[0] which is a '"'?
234  {
235  if (*(begin+i2) == '"')
236  break; //exits for loop?
237  if (*(begin+i2) == '\\')
238  {
239  switch (*(begin+i2+1))
240  {
241  case '"':
242  case '\\':
243  case '/':
244  value.svalue.push_back(*(begin+i2+1));
245  break;
246  case 'b':
247  value.svalue.push_back('\b');
248  break;
249  case 'f':
250  value.svalue.push_back('\f');
251  break;
252  case 'n':
253  value.svalue.push_back('\n');
254  break;
255  case 'r':
256  value.svalue.push_back('\r');
257  break;
258  case 't':
259  value.svalue.push_back('\t');
260  break;
261  default:
262  i2 += 3;
263  }
264  i2++;
265  }
266  else
267  {
268  value.svalue.push_back(*(begin+i2));
269  }
270  }
271 
272  if (i2 >= ilen)
273  {
274  begin = begin + (ilen-1);
275  return(JSON_ERROR_SCAN);
276  }
277 
278  // i2 is last character in string +1, index is length of extracted string
279 
280  begin += (i2 + 1);
281  value.type = Type::String;
282  }
283  break;
284  case 'f':
285  if (++begin == end || *begin == 0)
286  {
287  return JSON_ERROR_EOS;
288  }
289  if (*begin != 'a')
290  {
291  return JSON_ERROR_SCAN;
292  }
293  if (++begin == end || *begin == 0)
294  {
295  return JSON_ERROR_EOS;
296  }
297  if (*begin != 'l')
298  {
299  return JSON_ERROR_SCAN;
300  }
301  if (++begin == end || *begin == 0)
302  {
303  return JSON_ERROR_EOS;
304  }
305  if (*begin != 's')
306  {
307  return JSON_ERROR_SCAN;
308  }
309  if (++begin == end || *begin == 0)
310  {
311  return JSON_ERROR_EOS;
312  }
313  if (*begin != 'e')
314  {
315  return JSON_ERROR_SCAN;
316  }
317  value.type = Type::False;
318  if (++begin == end || *begin == 0)
319  {
320  return JSON_ERROR_EOS;
321  }
322  break;
323  case 't':
324 
325  if (++begin == end || *begin == 0)
326  {
327  return JSON_ERROR_EOS;
328  }
329  if (*begin != 'r')
330  {
331  return JSON_ERROR_SCAN;
332  }
333 
334  if (++begin == end || *begin == 0)
335  {
336  return JSON_ERROR_EOS;
337  }
338  if (*begin != 'u')
339  {
340  return JSON_ERROR_SCAN;
341  }
342 
343  if (++begin == end || *begin == 0)
344  {
345  return JSON_ERROR_EOS;
346  }
347  if (*begin != 'e')
348  {
349  return JSON_ERROR_SCAN;
350  }
351  value.type = Type::True;
352  if (++begin == end || *begin == 0)
353  {
354  return JSON_ERROR_EOS;
355  }
356  break;
357  case 'n':
358 
359  if (++begin == end || *begin == 0)
360  {
361  return JSON_ERROR_EOS;
362  }
363  if (*begin != 'u')
364  {
365  return JSON_ERROR_SCAN;
366  }
367 
368  if (++begin == end || *begin == 0)
369  {
370  return JSON_ERROR_EOS;
371  }
372  if (*begin != 'l')
373  {
374  return JSON_ERROR_SCAN;
375  }
376 
377  if (++begin == end || *begin == 0)
378  {
379  return JSON_ERROR_EOS;
380  }
381  if (*begin != 'l')
382  {
383  return JSON_ERROR_SCAN;
384  }
385  value.type = Type::Null;
386  if (++begin == end || *begin == 0)
387  {
388  return JSON_ERROR_EOS;
389  }
390  break;
391  case 0:
392  return JSON_ERROR_EOS;
393  default:
394  value.svalue.clear();
395  do
396  {
397  if (*begin == 0)
398  {
399  return JSON_ERROR_EOS;
400  }
401  value.svalue.push_back(*begin);
402  if (++begin == end)
403  {
404  return JSON_ERROR_EOS;
405  }
406  } while ((*begin>='0'&&*begin<='9')||*begin=='e'||*begin=='E'||*begin=='.'||*begin=='-'||*begin=='+');
407  value.type = Type::Number;
408  sscanf(value.svalue.c_str(), "%lf", &value.nvalue);
409  break;
410  }
411  skip_white(begin, end);
412  skip_character(begin, end, ',');
413  skip_white(begin, end);
414  value.end = begin;
415 
416  switch (value.type)
417  {
418  case Type::True:
419  case Type::False:
420  case Type::Null:
421  case Type::Number:
422  case Type::String:
423  case Type::Object:
424  case Type::Array:
425  break;
426  case Type::Undefined:
427  default:
428  return JSON_ERROR_SCAN;
429  }
430  return static_cast<int32_t>(value.type);
431  }
int32_t extract_members(string::iterator &begin, string::iterator &end, Object &members)
Definition: jsonclass.cpp:89
int iretn
Definition: rw_test.cpp:37
int32_t extract_values(string::iterator &begin, string::iterator &end, Array &values)
Definition: jsonclass.cpp:112
int32_t skip_character(string::iterator &bit, string::iterator &eit, const char character)
Definition: jsonclass.cpp:449
string::iterator begin
Definition: jsonclass.h:87
#define JSON_ERROR_EOS
Definition: cosmos-errno.h:95
#define JSON_ERROR_SCAN
Definition: cosmos-errno.h:96
string::iterator end
Definition: jsonclass.h:88
int32_t skip_white(string::iterator &bit, string::iterator &eit)
Definition: jsonclass.cpp:433
int32_t Cosmos::Support::Json::extract_member ( string::iterator &  begin,
string::iterator &  end,
Member member 
)

Extract JSON member. Extract next member of JSON Obect from provided string.

Parameters
beginBeginning string iterator.
endEnd string iterator.
value::Member containing value.
Returns
Zero or negative error.
142  {
143  int32_t iretn = 0;
144  iretn = extract_name(begin, end, member.first);
145  if (iretn <= 0)
146  {
147  return iretn;
148  }
149  iretn = extract_value(begin, end, member.second);
150  return iretn;
151  }
int iretn
Definition: rw_test.cpp:37
string::iterator begin
Definition: jsonclass.h:87
int32_t extract_name(string::iterator &bit, string::iterator &eit, string &name)
Definition: jsonclass.cpp:500
int32_t extract_value(string::iterator &bit, string::iterator &eit, Value &value)
Definition: jsonclass.cpp:158
string::iterator end
Definition: jsonclass.h:88
int32_t Cosmos::Support::Json::skip_character ( string::iterator &  bit,
string::iterator &  eit,
const char  character 
)
450  {
451  int32_t iretn;
452 
453  iretn = skip_white(begin, end);
454  if (iretn < 0)
455  {
456  return iretn;
457  }
458 
459  if (begin == end || *begin == 0)
460  {
461  return (JSON_ERROR_EOS);
462  }
463 
464  if (*begin != character)
465  {
466  return 0;
467  }
468  ++begin;
469 
470  return 1;
471  }
int iretn
Definition: rw_test.cpp:37
string::iterator begin
Definition: jsonclass.h:87
#define JSON_ERROR_EOS
Definition: cosmos-errno.h:95
string::iterator end
Definition: jsonclass.h:88
int32_t skip_white(string::iterator &bit, string::iterator &eit)
Definition: jsonclass.cpp:433
int32_t Cosmos::Support::Json::skip_to_character ( string::iterator &  bit,
string::iterator &  eit,
string  characters 
)
474  {
475 
476  if (begin == end || *begin == 0)
477  {
478  return (JSON_ERROR_EOS);
479  }
480 
481  while (begin != end && *begin != 0)
482  {
483  for (char character : characters)
484  {
485  if (*begin == character)
486  {
487  return *begin;
488  }
489  }
490  ++begin;
491  if (begin == end || *begin == 0)
492  {
493  return (JSON_ERROR_EOS);
494  }
495  }
496 
497  return (0);
498  }
string::iterator begin
Definition: jsonclass.h:87
#define JSON_ERROR_EOS
Definition: cosmos-errno.h:95
string::iterator end
Definition: jsonclass.h:88
int32_t Cosmos::Support::Json::skip_white ( string::iterator &  bit,
string::iterator &  eit 
)
434  {
435  if (*begin == 0)
436  return (JSON_ERROR_EOS);
437 
438  while (begin != end && *begin != 0 && isspace(*begin))
439  {
440  ++begin;
441  }
442 
443  if (begin == end || *begin == 0)
444  return (JSON_ERROR_EOS);
445  else
446  return 0;
447  }
string::iterator begin
Definition: jsonclass.h:87
#define JSON_ERROR_EOS
Definition: cosmos-errno.h:95
string::iterator end
Definition: jsonclass.h:88

Member Data Documentation

string Cosmos::Support::Json::Content
Type Cosmos::Support::Json::JType
Object Cosmos::Support::Json::ObjectContents
Array Cosmos::Support::Json::ArrayContents
string::iterator Cosmos::Support::Json::begin
private
string::iterator Cosmos::Support::Json::end
private

The documentation for this class was generated from the following files: