PVData C++  8.0.5
json.h
1 /*
2  * Copyright information and license terms for this software can be
3  * found in the file LICENSE that is included with the distribution
4  */
5 #ifndef PV_JSON_H
6 #define PV_JSON_H
7 
8 #include <istream>
9 #include <ostream>
10 #include <string>
11 #include <map>
12 
13 #include <pv/pvdVersion.h>
14 #include <pv/pvData.h>
15 
16 #ifdef epicsExportSharedSymbols
17 # define pvjson_epicsExportSharedSymbols
18 # undef epicsExportSharedSymbols
19 #endif
20 
21 #include <yajl_parse.h>
22 
23 #ifdef pvjson_epicsExportSharedSymbols
24 # define epicsExportSharedSymbols
25 # include "shareLib.h"
26 #endif
27 
28 #include <shareLib.h>
29 
30 namespace epics{namespace pvData{
31 
32 class BitSet;
33 
34 /** @defgroup pvjson JSON print/parse
35  *
36  * Printing PVField as JSON and parsing JSON into PVField.
37  *
38  * @{
39  */
40 
41 //! Options used during printing
42 struct epicsShareClass JSONPrintOptions
43 {
44  bool multiLine; //!< include new lines
45  bool ignoreUnprintable;//!< ignore union/union array when encountered
46  unsigned indent; //!< Initial indentation (# of spaces)
47  bool json5; //!< Output extended JSON (eg. NaN). @since 8.1.0
49 };
50 
51 /** Print PVStructure as JSON
52  *
53  * 'mask' selects those fields which will be printed.
54  * @version Overload added after 7.0.0
55  */
57 void printJSON(std::ostream& strm,
58  const PVStructure& val,
59  const BitSet& mask,
61 
62 /** Print PVField as JSON
63  * @version Overload added after 7.0.0
64  */
66 void printJSON(std::ostream& strm,
67  const PVField& val,
69 
70 // To be deprecated in favor of previous form
72 void printJSON(std::ostream& strm,
75 {
76  printJSON(strm, *val, opts);
77 }
78 
79 /** Parse JSON text into a PVStructure
80  *
81  * Restrictions:
82  *
83  * - Top level must be {} dict/object
84  * - field values must be number, string, array, or dict/object
85  * - array values must be number or string
86  */
89 
90 /** Parse JSON and store into the provided PVStructure.
91  *
92  * Restrictions:
93  *
94  * - array of union not supported
95  * - Only scalar value assigned to union
96  *
97  * @param strm Read JSON text from stream
98  * @param dest Store in fields of this structure
99  * @param assigned Which fields of _dest_ were assigned. (Optional)
100  * @throws std::runtime_error on failure. dest and assigned may be modified.
101  * @version Overload added after 7.0.0
102  */
104 void parseJSON(std::istream& strm,
105  PVField& dest,
106  BitSet *assigned=0);
107 
108 // To be deprecated in favor of previous form
110 void parseJSON(std::istream& strm,
111  const PVField::shared_pointer& dest,
112  BitSet *assigned=0)
113 {
114  parseJSON(strm, *dest, assigned);
115 }
116 
117 
118 /** Wrapper around yajl_parse()
119  *
120  * Parse entire input stream.
121  * Errors if extranious non-whitespace found after the point were parsing completes.
122  *
123  * @param src The stream from which input charactors are read
124  * @param handle A parser handle previously allocated with yajl_alloc(). Not free'd on success or failure.
125  *
126  * @returns true if parsing completes successfully. false if parsing cancelled by callback. throws other errors
127  *
128  * @note The form of this call depends on EPICS_YAJL_VERSION
129  */
133 
134 namespace yajl {
135 // undef implies API version 0
136 #ifndef EPICS_YAJL_VERSION
137 typedef long integer_arg;
138 typedef unsigned size_arg;
139 #else
140 typedef long long integer_arg;
141 typedef size_t size_arg;
142 #endif
143 } // namespace epics::pvData::yajl
144 
145 /** @} */
146 
147 }} // namespace epics::pvData
148 
149 #endif // PV_JSON_H
#define FORCE_INLINE
Definition: templateMeta.h:20
Options used during printing.
Definition: json.h:42
epicsShareFunc bool yajl_parse_helper(std::istream &src, yajl_handle handle)