This is Unofficial EPICS BASE Doxygen Site
JSON print/parse

Namespaces

 epics::pvData::yajl
 

Classes

struct  epics::pvData::JSONPrintOptions
 Options used during printing. More...
 

Functions

void epics::pvData::printJSON (std::ostream &strm, const PVStructure &val, const BitSet &mask, const JSONPrintOptions &opts)
 
void epics::pvData::printJSON (std::ostream &strm, const PVField &val, const JSONPrintOptions &opts)
 
FORCE_INLINE void epics::pvData::printJSON (std::ostream &strm, const PVField::const_shared_pointer &val, const JSONPrintOptions &opts=JSONPrintOptions())
 
epics::pvData::PVStructure::shared_pointer epics::pvData::parseJSON (std::istream &strm)
 
epicsShareFunc void epics::pvData::parseJSON (std::istream &strm, PVField &dest, BitSet *assigned)
 
FORCE_INLINE void epics::pvData::parseJSON (std::istream &strm, const PVField::shared_pointer &dest, BitSet *assigned=0)
 
bool epics::pvData::yajl_parse_helper (std::istream &src, yajl_handle handle)
 

Detailed Description

Printing PVField as JSON and parsing JSON into PVField.

Function Documentation

FORCE_INLINE void epics::pvData::parseJSON ( std::istream &  strm,
const PVField::shared_pointer &  dest,
BitSet assigned = 0 
)

Definition at line 109 of file json.h.

112 {
113  parseJSON(strm, *dest, assigned);
114 }
FORCE_INLINE void parseJSON(std::istream &strm, const PVField::shared_pointer &dest, BitSet *assigned=0)
Definition: json.h:109
epicsShareFunc PVStructure::shared_pointer epics::pvData::parseJSON ( std::istream &  strm)

Parse JSON text into a PVStructure

Restrictions:

  • Top level must be {} dict/object
  • field values must be number, string, array, or dict/object
  • array values must be number or string

Definition at line 255 of file parseany.cpp.

256 {
257 #ifndef EPICS_YAJL_VERSION
258  yajl_parser_config conf;
259  memset(&conf, 0, sizeof(conf));
260  conf.allowComments = 1;
261  conf.checkUTF8 = 1;
262 #endif
263 
264  context ctxt;
265 
266 #ifndef EPICS_YAJL_VERSION
267  handler handle(yajl_alloc(&jtree_cbs, &conf, NULL, &ctxt));
268 #else
269  handler handle(yajl_alloc(&jtree_cbs, NULL, &ctxt));
270 
271  yajl_config(handle, yajl_allow_comments, 1);
272 #endif
273 
274  if(!yajl_parse_helper(strm, handle))
275  throw std::runtime_error(ctxt.msg);
276 
277  return ctxt.cur->buildPVStructure();
278 }
int yajl_config(yajl_handle h, yajl_option opt,...)
Definition: yajl.c:85
#define NULL
Definition: catime.c:38
yajl_handle yajl_alloc(const yajl_callbacks *callbacks, yajl_alloc_funcs *afs, void *ctx)
Definition: yajl.c:46
bool yajl_parse_helper(std::istream &src, yajl_handle handle)
Definition: parsehelper.cpp:29
epicsShareFunc void epics::pvData::parseJSON ( std::istream &  strm,
PVField dest,
BitSet assigned = 0 
)

Parse JSON and store into the provided PVStructure.

Restrictions:

  • array of union not supported
  • Only scalar value assigned to union
Parameters
strmRead JSON text from stream
destStore in fields of this structure
assignedWhich fields of dest were assigned. (Optional)
Exceptions
std::runtime_erroron failure. dest and assigned may be modified.
Version
Overload added after 7.0.0

Definition at line 311 of file parseinto.cpp.

314 {
315 #ifndef EPICS_YAJL_VERSION
316  yajl_parser_config conf;
317  memset(&conf, 0, sizeof(conf));
318  conf.allowComments = 1;
319  conf.checkUTF8 = 1;
320 #endif
321 
322  // we won't create refs to 'dest' which presist beyond this call.
323  // however, it is convienent to treat 'dest' in the same manner as
324  // any union/structureArray memebers it may contain.
325  PVFieldPtr fakedest(&dest, noop());
326 
327  context ctxt(fakedest, assigned);
328 
329 #ifndef EPICS_YAJL_VERSION
330  handler handle(yajl_alloc(&jtree_cbs, &conf, NULL, &ctxt));
331 #else
332  handler handle(yajl_alloc(&jtree_cbs, NULL, &ctxt));
333 
334  yajl_config(handle, yajl_allow_comments, 1);
335 #endif
336 
337 
338  if(!yajl_parse_helper(strm, handle))
339  throw std::runtime_error(ctxt.msg);
340 
341  if(!ctxt.stack.empty())
342  throw std::logic_error("field stack not empty");
343  assert(fakedest.use_count()==1);
344 }
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
int yajl_config(yajl_handle h, yajl_option opt,...)
Definition: yajl.c:85
#define NULL
Definition: catime.c:38
yajl_handle yajl_alloc(const yajl_callbacks *callbacks, yajl_alloc_funcs *afs, void *ctx)
Definition: yajl.c:46
std::tr1::shared_ptr< PVField > PVFieldPtr
Definition: pvData.h:66
bool yajl_parse_helper(std::istream &src, yajl_handle handle)
Definition: parsehelper.cpp:29
FORCE_INLINE void epics::pvData::printJSON ( std::ostream &  strm,
const PVField::const_shared_pointer &  val,
const JSONPrintOptions opts = JSONPrintOptions() 
)

Definition at line 71 of file json.h.

74 {
75  printJSON(strm, *val, opts);
76 }
FORCE_INLINE void printJSON(std::ostream &strm, const PVField::const_shared_pointer &val, const JSONPrintOptions &opts=JSONPrintOptions())
Definition: json.h:71
epicsShareFunc void epics::pvData::printJSON ( std::ostream &  strm,
const PVStructure val,
const BitSet mask,
const JSONPrintOptions opts = JSONPrintOptions() 
)

Print PVStructure as JSON

'mask' selects those fields which will be printed.

Version
Overload added after 7.0.0

Definition at line 211 of file print.cpp.

215 {
216  args A(strm, opts);
217  pvd::BitSet emask(mask);
218  expandBS(val, emask, true);
219  if(!emask.get(0)) return;
220  show_struct(A, &val, &emask);
221 }
A vector of bits.
Definition: bitSet.h:56
epicsShareFunc void epics::pvData::printJSON ( std::ostream &  strm,
const PVField val,
const JSONPrintOptions opts = JSONPrintOptions() 
)

Print PVField as JSON

Version
Overload added after 7.0.0

Definition at line 223 of file print.cpp.

226 {
227  args A(strm, opts);
228  show_field(A, &val, 0);
229 }
epicsShareFunc bool epics::pvData::yajl_parse_helper ( std::istream &  src,
yajl_handle  handle 
)

Wrapper around yajl_parse()

Parse entire input stream. Errors if extranious non-whitespace found after the point were parsing completes.

Parameters
srcThe stream from which input charactors are read
handleA parser handle previously allocated with yajl_alloc(). Not free'd on success or failure.
Returns
true if parsing completes successfully. false if parsing cancelled by callback. throws other errors
Note
The form of this call depends on EPICS_YAJL_VERSION

Definition at line 29 of file parsehelper.cpp.

31 {
32  unsigned linenum=0;
33 #ifndef EPICS_YAJL_VERSION
34  bool done = false;
35 #endif
36 
37  std::string line;
38  while(std::getline(src, line)) {
39  linenum++;
40 
41 #ifndef EPICS_YAJL_VERSION
42  if(done) {
43  check_trailing(line);
44  continue;
45  }
46 #endif
47 
48  yajl_status sts = yajl_parse(handle, (const unsigned char*)line.c_str(), line.size());
49 
50  switch(sts) {
51  case yajl_status_ok: {
52  size_t consumed = yajl_get_bytes_consumed(handle);
53 
54  if(consumed<line.size()) {
55  check_trailing(line.substr(consumed));
56  }
57 
58 #ifndef EPICS_YAJL_VERSION
59  done = true;
60 #endif
61  break;
62  }
64  return false;
65 #ifndef EPICS_YAJL_VERSION
66  case yajl_status_insufficient_data:
67  // continue with next line
68  break;
69 #endif
70  case yajl_status_error:
71  {
72  std::ostringstream msg;
73  unsigned char *raw = yajl_get_error(handle, 1, (const unsigned char*)line.c_str(), line.size());
74  if(!raw) {
75  msg<<"Unknown error on line "<<linenum;
76  } else {
77  try {
78  msg<<"Error on line "<<linenum<<" : "<<(const char*)raw;
79  }catch(...){
80  yajl_free_error(handle, raw);
81  throw;
82  }
83  yajl_free_error(handle, raw);
84  }
85  throw std::runtime_error(msg.str());
86  }
87  }
88  }
89 
90  if(!src.eof() || src.bad()) {
91  std::ostringstream msg;
92  msg<<"I/O error after line "<<linenum;
93  throw std::runtime_error(msg.str());
94 
95 #ifndef EPICS_YAJL_VERSION
96  } else if(!done) {
97  switch(yajl_parse_complete(handle)) {
98 #else
99  } else {
100  switch(yajl_complete_parse(handle)) {
101 #endif
102  case yajl_status_ok:
103  break;
105  return false;
106 #ifndef EPICS_YAJL_VERSION
107  case yajl_status_insufficient_data:
108  throw std::runtime_error("unexpected end of input");
109 #endif
110  case yajl_status_error:
111  throw std::runtime_error("Error while completing parsing");
112  }
113  }
114 
115  return true;
116 }
unsigned char * yajl_get_error(yajl_handle hand, int verbose, const unsigned char *jsonText, size_t jsonTextLen)
Definition: yajl.c:160
int linenum
Definition: flex.c:71
char * line
Definition: reader.c:25
void yajl_free_error(yajl_handle hand, unsigned char *str)
Definition: yajl.c:175
yajl_status
Definition: yajl_parse.h:32
yajl_status yajl_parse(yajl_handle hand, const unsigned char *jsonText, size_t jsonTextLen)
Definition: yajl.c:121
void done(int k)
Definition: antelope.c:77
yajl_status yajl_complete_parse(yajl_handle hand)
Definition: yajl.c:142
size_t yajl_get_bytes_consumed(yajl_handle hand)
Definition: yajl.c:167