Barracuda Application Server C/C++ Reference
NO
JEncoder.h
1 /*
2  * ____ _________ __ _
3  * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4  * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5  * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6  * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7  * /____/
8  *
9  * Barracuda Application Server
10  *
11  ****************************************************************************
12  * HEADER
13  *
14  * $Id: JEncoder.h 5029 2022-01-16 21:32:09Z wini $
15  *
16  * COPYRIGHT: Real Time Logic LLC, 2006-2022
17  *
18  * This software is copyrighted by and is the sole property of Real
19  * Time Logic LLC. All rights, title, ownership, or other interests in
20  * the software remain the property of Real Time Logic LLC. This
21  * software may only be used in accordance with the terms and
22  * conditions stipulated in the corresponding license agreement under
23  * which the software has been supplied. Any unauthorized use,
24  * duplication, transmission, distribution, or disclosure of this
25  * software is expressly forbidden.
26  *
27  * This Copyright notice may not be removed or modified without prior
28  * written consent of Real Time Logic LLC.
29  *
30  * Real Time Logic LLC. reserves the right to modify this software
31  * without notice.
32  *
33  * http://realtimelogic.com
34  ****************************************************************************
35  *
36  */
37 
38 #ifndef __JEncoder_h
39 #define __JEncoder_h
40 
41 #include "JVal.h"
42 #include "BufPrint.h"
43 
65 typedef struct JEncoder
66 {
67 #ifdef __cplusplus
68 
70  JEncoder(JErr* err, BufPrint* out);
71  ~JEncoder();
72 
75  int setInt(S32 val);
76 
79  int setLong(S64 val);
80 #ifndef NO_DOUBLE
81 
84  int setDouble(double val);
85 #endif
86 
90  int setString(const char* val);
91 
94  int b64enc(const void* source, S32 slen);
95 
101  int fmtString(const char* fmt,...);
102 
106  int vFmtString(const char* fmt,va_list argList);
107 
110  int setBoolean(bool val);
111 
114  int setNull();
115 
118  int setJV(struct JVal* val, bool iterateNext=false);
119 
161  int set(const char* fmt, ...);
162 
168  int setName(const char* name);
169 
181  int beginObject();
182 
185  int endObject();
186 
189  int beginArray();
190 
193  int endArray();
194 
197  JErr* getErr();
198 
201  int flush();
202 
205  int commit();
206 
210 
211 #endif
212  JErr* err;
213  struct
214  {
215  S32 level;
216  U8 data[64];
217  }objectStack;
218  BufPrint* out;
219  BaBool objectMember;
220  BaBool startNewObj;
222 #ifdef __cplusplus
223 extern "C" {
224 #endif
225 BA_API void JEncoder_constructor(JEncoder* o, JErr* err, BufPrint* out);
226 #define JEncoder_destructor(o) JEncoder_flush(o)
227 BA_API int JEncoder_flush(JEncoder* o);
228 BA_API int JEncoder_commit(JEncoder* o);
229 #define JEncoder_getErr(o) (o)->err
230 BA_API int JEncoder_setInt(JEncoder* o, S32 val);
231 BA_API int JEncoder_setLong(JEncoder* o, S64 val);
232 #ifdef NO_DOUBLE
233 #define JEncoder_setDouble JEncoder_setInt
234 #else
235 BA_API int JEncoder_setDouble(JEncoder* o, double val);
236 #endif
237 
238 BA_API int JEncoder_fmtString(JEncoder* o, const char* fmt,...);
239 BA_API int JEncoder_vFmtString(
240  JEncoder* o, const char* fmt,va_list argList);
241 BA_API int JEncoder_setString(JEncoder* o, const char* val);
242 BA_API int JEncoder_b64enc(JEncoder* o, const void* source, S32 slen);
243 BA_API int JEncoder_setBoolean(JEncoder* o, BaBool val);
244 BA_API int JEncoder_setNull(JEncoder* o);
245 BA_API int JEncoder_setJV(
246  JEncoder* o, struct JVal* val, BaBool iterateNext);
247 BA_API int JEncoder_vSetJV(
248  JEncoder* o,const char** fmt,va_list* argList);
249 BA_API int JEncoder_set(JEncoder* o, const char* fmt, ...);
250 BA_API int JEncoder_setName(JEncoder* o, const char* name);
251 BA_API int JEncoder_beginObject(JEncoder* o);
252 BA_API int JEncoder_endObject(JEncoder* o);
253 BA_API int JEncoder_beginArray(JEncoder* o);
254 BA_API int JEncoder_endArray(JEncoder* o);
255 #define JEncoder_getBufPrint(o) (o)->out
256 #ifdef __cplusplus
257 }
258 inline JEncoder::JEncoder(JErr* err, BufPrint* out) {
259  JEncoder_constructor(this,err, out); }
260 inline JEncoder::~JEncoder() {
261  JEncoder_destructor(this); }
262 inline int JEncoder::setInt(S32 val) {
263  return JEncoder_setInt(this, val); }
264 inline int JEncoder::setLong(S64 val) {
265  return JEncoder_setLong(this, val); }
266 #ifndef NO_DOUBLE
267 inline int JEncoder::setDouble(double val) {
268  return JEncoder_setDouble(this, val); }
269 #endif
270 inline int JEncoder::fmtString(const char* fmt,...) {
271  int retv; va_list argList;
272  va_start(argList, fmt);
273  retv = JEncoder_fmtString(this, fmt, argList);
274  va_end(argList);
275  return retv;
276 }
277 inline int JEncoder::vFmtString(const char* fmt,va_list argList) {
278  return JEncoder_vFmtString(this, fmt, argList); }
279 inline int JEncoder::setString(const char* val) {
280  return JEncoder_setString(this, val); }
281 inline int JEncoder::b64enc(const void* source, S32 slen) {
282  return JEncoder_b64enc(this, source, slen); }
283 inline int JEncoder::setBoolean(bool val) {
284  return JEncoder_setBoolean(this, val ? TRUE : FALSE); }
285 inline int JEncoder::setNull() {
286  return JEncoder_setNull(this); }
287 inline int JEncoder::setJV(struct JVal* val, bool iterateNext) {
288  return JEncoder_setJV(this,val,iterateNext?TRUE:FALSE); }
289 inline int JEncoder::set(const char* fmt, ...) {
290  int retv; va_list argList;
291  va_start(argList, fmt);
292  retv=JEncoder_vSetJV(this,&fmt,&argList);
293  va_end(argList);
294  return retv;
295 }
296 inline int JEncoder::setName(const char* name) {
297  return JEncoder_setName(this, name); }
298 inline int JEncoder::beginObject() {
299  return JEncoder_beginObject(this); }
300 inline int JEncoder::endObject() {
301  return JEncoder_endObject(this); }
302 inline int JEncoder::beginArray() {
303  return JEncoder_beginArray(this); }
304 inline int JEncoder::endArray() {
305  return JEncoder_endArray(this); }
307  return JEncoder_getErr(this); }
308 inline int JEncoder::flush() {
309  return JEncoder_flush(this); }
310 inline int JEncoder::commit() {
311  return JEncoder_commit(this); }
313  return JEncoder_getBufPrint(this); }
314 #endif
315 
319 #define JE_MEMBER(o, m) #m, (o)->m
320 
321  /* end of JSONRef */
323 
324 #endif
JEncoder::setString
int setString(const char *val)
Format a string value.
Definition: JEncoder.h:279
JEncoder::setInt
int setInt(S32 val)
Format an integer value.
Definition: JEncoder.h:262
JEncoder::flush
int flush()
Flush JEncoder data to the output stream.
Definition: JEncoder.h:308
JEncoder::endObject
int endObject()
End object formatting.
Definition: JEncoder.h:300
JEncoder::getBufPrint
BufPrint * getBufPrint()
Fetch the internal BufPrint object.
Definition: JEncoder.h:312
JEncoder
The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format.
Definition: JEncoder.h:66
JVal
JVal represents a value in a JSON tree.
Definition: JVal.h:73
JEncoder::vFmtString
int vFmtString(const char *fmt, va_list argList)
Equivalent to fmtString with variable argument list replaced by argList.
Definition: JEncoder.h:277
JEncoder::fmtString
int fmtString(const char *fmt,...)
Converts (according to format 'fmt') and writes to stream.
Definition: JEncoder.h:270
JEncoder::beginArray
int beginArray()
Begin array formatting.
Definition: JEncoder.h:302
JEncoder
struct JEncoder JEncoder
The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format.
JEncoder::commit
int commit()
Calls flush and enables the construction of a new object.
Definition: JEncoder.h:310
JEncoder::b64enc
int b64enc(const void *source, S32 slen)
Encode and emit (binary) data using B64 encoding.
Definition: JEncoder.h:281
BufPrint
The BufPrint class, which implements an ANSI compatible printf method, is a base class used by severa...
Definition: BufPrint.h:122
JEncoder::setNull
int setNull()
Set a JSON null value.
Definition: JEncoder.h:285
JEncoder::JEncoder
JEncoder(JErr *err, BufPrint *out)
Create an instance of the JEncoder class.
Definition: JEncoder.h:258
JErr
The JSON error container object.
Definition: JParser.h:155
JEncoder::set
int set(const char *fmt,...)
Encode/serialize C structs/data to JSON using formatted output.
Definition: JEncoder.h:289
JEncoder::endArray
int endArray()
End array formatting.
Definition: JEncoder.h:304
JEncoder::beginObject
int beginObject()
Begin object formatting.
Definition: JEncoder.h:298
JEncoder::setLong
int setLong(S64 val)
Format a long integer value.
Definition: JEncoder.h:264
JEncoder::setDouble
int setDouble(double val)
Format a double value.
Definition: JEncoder.h:267
JEncoder::setJV
int setJV(struct JVal *val, bool iterateNext=false)
Format a node or a tree of JVal nodes.
Definition: JEncoder.h:287
JEncoder::getErr
JErr * getErr()
Returns the JErr object.
Definition: JEncoder.h:306
JEncoder::setBoolean
int setBoolean(bool val)
Format a boolean value.
Definition: JEncoder.h:283
JEncoder::setName
int setName(const char *name)
Set the member name before calling any of the set<value> members if formatting an object.
Definition: JEncoder.h:296