Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
JEncoder.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Application Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: JEncoder.h 6657 2026-10-28 18:19:39Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2006-2026
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
40#ifndef __JEncoder_h
41#define __JEncoder_h
42
43#include "JVal.h"
44#include "BufPrint.h"
45
73typedef struct JEncoder
74{
75#ifdef __cplusplus
81 JEncoder(JErr* err, BufPrint* out);
86 ~JEncoder();
87
93 int setInt(S32 val);
94
100 int setLong(S64 val);
101
108 int setDouble(double val);
109
117 int setString(const char* val, size_t len);
118
126 int b64enc(const void* source, S32 slen);
127
137 int fmtString(const char* fmt,...);
138
147 int vFmtString(const char* fmt,va_list argList);
148
154 int setBoolean(bool val);
155
160 int setNull();
161
162/* Expose the optional API to Doxygen without selecting it in product builds. */
163#if BA_ENABLE_JENCODER_JVAL || defined(__DOXYGEN__)
173 int setJV(struct JVal* val, bool iterateNext=false);
174#endif
175
226 int set(const char* fmt, ...);
227
234 int setName(const char* name);
235
241 int beginObject();
242
248 int endObject();
249
255 int beginArray();
256
262 int endArray();
263
267 JErr* getErr();
268
274 int flush();
275
281 int commit();
282
287
288#endif
289 JErr* err;
290 struct
291 {
292 S32 level;
293 U8 data[64];
294 }objectStack;
295 BufPrint* out;
296 BaBool objectMember;
297 BaBool startNewObj;
299#ifdef __cplusplus
300extern "C" {
301#endif
305BA_API void JEncoder_constructor(JEncoder* o, JErr* err, BufPrint* out);
309#define JEncoder_destructor(o) JEncoder_flush(o)
321#define JEncoder_getErr(o) (o)->err
325BA_API int JEncoder_setInt(JEncoder* o, S32 val);
329BA_API int JEncoder_setLong(JEncoder* o, S64 val);
333BA_API int JEncoder_setDouble(JEncoder* o, double val);
334
338BA_API int JEncoder_fmtString(JEncoder* o, const char* fmt,...);
343 JEncoder* o, const char* fmt,va_list argList);
347BA_API int JEncoder_setString(JEncoder* o, const char* val, size_t len);
351BA_API int JEncoder_b64enc(JEncoder* o, const void* source, S32 slen);
360#if BA_ENABLE_JENCODER_JVAL || defined(__DOXYGEN__)
364BA_API int JEncoder_setJV(
365 JEncoder* o, struct JVal* val, BaBool iterateNext);
366#endif
367BA_API int JEncoder_vSetJV(
368 JEncoder* o,const char** fmt,va_list* argList);
372BA_API int JEncoder_set(JEncoder* o, const char* fmt, ...);
376BA_API int JEncoder_setName(JEncoder* o, const char* name);
396#define JEncoder_getBufPrint(o) (o)->out
397#ifdef __cplusplus
398}
399inline JEncoder::JEncoder(JErr* err, BufPrint* out) {
400 JEncoder_constructor(this,err, out); }
402 JEncoder_destructor(this); }
403inline int JEncoder::setInt(S32 val) {
404 return JEncoder_setInt(this, val); }
405inline int JEncoder::setLong(S64 val) {
406 return JEncoder_setLong(this, val); }
407inline int JEncoder::setDouble(double val) {
408 return JEncoder_setDouble(this, val); }
409inline int JEncoder::fmtString(const char* fmt,...) {
410 int retv; va_list argList;
411 va_start(argList, fmt);
412 retv = JEncoder_fmtString(this, fmt, argList);
413 va_end(argList);
414 return retv;
415}
416inline int JEncoder::vFmtString(const char* fmt,va_list argList) {
417 return JEncoder_vFmtString(this, fmt, argList); }
418inline int JEncoder::setString(const char* val, size_t len) {
419 return JEncoder_setString(this, val, len); }
420inline int JEncoder::b64enc(const void* source, S32 slen) {
421 return JEncoder_b64enc(this, source, slen); }
422inline int JEncoder::setBoolean(bool val) {
423 return JEncoder_setBoolean(this, val ? TRUE : FALSE); }
424inline int JEncoder::setNull() {
425 return JEncoder_setNull(this); }
426#if BA_ENABLE_JENCODER_JVAL
427inline int JEncoder::setJV(struct JVal* val, bool iterateNext) {
428 return JEncoder_setJV(this,val,iterateNext?TRUE:FALSE); }
429#endif
430inline int JEncoder::set(const char* fmt, ...) {
431 int retv; va_list argList;
432 va_start(argList, fmt);
433 retv=JEncoder_vSetJV(this,&fmt,&argList);
434 va_end(argList);
435 return retv;
436}
437inline int JEncoder::setName(const char* name) {
438 return JEncoder_setName(this, name); }
440 return JEncoder_beginObject(this); }
442 return JEncoder_endObject(this); }
444 return JEncoder_beginArray(this); }
445inline int JEncoder::endArray() {
446 return JEncoder_endArray(this); }
448 return JEncoder_getErr(this); }
449inline int JEncoder::flush() {
450 return JEncoder_flush(this); }
451inline int JEncoder::commit() {
452 return JEncoder_commit(this); }
454 return JEncoder_getBufPrint(this); }
455#endif
456
460#define JE_MEMBER(o, m) #m, (o)->m
461
462 /* end of JSONRef */
464
465#endif
BA_API int JEncoder_set(JEncoder *o, const char *fmt,...)
Encode/serialize C structs/data to JSON using formatted output.
BA_API int JEncoder_setNull(JEncoder *o)
Write JSON null.
JEncoder(JErr *err, BufPrint *out)
Initialize an encoder without writing output or resetting err.
Definition: JEncoder.h:399
int fmtString(const char *fmt,...)
Format text inside JSON quotation marks.
Definition: JEncoder.h:409
BA_API int JEncoder_flush(JEncoder *o)
Flush the underlying writer if JErr is clear.
int setLong(S64 val)
Write a JSON number.
Definition: JEncoder.h:405
int setName(const char *name)
Write an object member name before its value.
Definition: JEncoder.h:437
int setBoolean(bool val)
Write a JSON boolean.
Definition: JEncoder.h:422
int beginObject()
Begin a JSON object value; call setName() before each member value.
Definition: JEncoder.h:439
BA_API int JEncoder_setName(JEncoder *o, const char *name)
Write an object member name before its value.
BA_API int JEncoder_setBoolean(JEncoder *o, BaBool val)
Write a JSON boolean.
int flush()
Flush the underlying writer if JErr is clear.
Definition: JEncoder.h:449
int b64enc(const void *source, S32 slen)
Write binary data as a quoted standard Base64 string.
Definition: JEncoder.h:420
BA_API int JEncoder_vFmtString(JEncoder *o, const char *fmt, va_list argList)
Write formatted text between JSON quotes, or JSON null.
int beginArray()
Begin a JSON array value; array elements have no member-name call.
Definition: JEncoder.h:443
BA_API int JEncoder_fmtString(JEncoder *o, const char *fmt,...)
Format text inside JSON quotation marks.
BufPrint * getBufPrint()
Query output storage.
Definition: JEncoder.h:453
BA_API void JEncoder_constructor(JEncoder *o, JErr *err, BufPrint *out)
Initialize an encoder without writing output or resetting err.
int vFmtString(const char *fmt, va_list argList)
Write formatted text between JSON quotes, or JSON null.
Definition: JEncoder.h:416
BA_API int JEncoder_setInt(JEncoder *o, S32 val)
Write a JSON number.
#define JEncoder_destructor(o)
Call flush() without freeing the borrowed writer or error object.
Definition: JEncoder.h:309
BA_API int JEncoder_setLong(JEncoder *o, S64 val)
Write a JSON number.
int setNull()
Write JSON null.
Definition: JEncoder.h:424
int endArray()
Close the current array after its final complete element.
Definition: JEncoder.h:445
int endObject()
Close the current object after its final complete member.
Definition: JEncoder.h:441
BA_API int JEncoder_endObject(JEncoder *o)
Close the current object after its final complete member.
int setDouble(double val)
Write a JSON number.
Definition: JEncoder.h:407
~JEncoder()
Call flush() without freeing the borrowed writer or error object.
Definition: JEncoder.h:401
int set(const char *fmt,...)
Encode/serialize C structs/data to JSON using formatted output.
Definition: JEncoder.h:430
BA_API int JEncoder_beginArray(JEncoder *o)
Begin a JSON array value; array elements have no member-name call.
#define JEncoder_getErr(o)
Query error storage.
Definition: JEncoder.h:321
BA_API int JEncoder_setDouble(JEncoder *o, double val)
Write a JSON number.
BA_API int JEncoder_commit(JEncoder *o)
Permit a new top-level value and flush the writer.
JErr * getErr()
Query error storage.
Definition: JEncoder.h:447
BA_API int JEncoder_b64enc(JEncoder *o, const void *source, S32 slen)
Write binary data as a quoted standard Base64 string.
int commit()
Permit a new top-level value and flush the writer.
Definition: JEncoder.h:451
BA_API int JEncoder_beginObject(JEncoder *o)
Begin a JSON object value; call setName() before each member value.
#define JEncoder_getBufPrint(o)
Query output storage.
Definition: JEncoder.h:396
int setString(const char *val, size_t len)
Write a quoted JSON string through BufPrint::jsonString.
Definition: JEncoder.h:418
int setInt(S32 val)
Write a JSON number.
Definition: JEncoder.h:403
BA_API int JEncoder_setJV(JEncoder *o, struct JVal *val, BaBool iterateNext)
Serialize a JVal node and its child values.
BA_API int JEncoder_endArray(JEncoder *o)
Close the current array after its final complete element.
BA_API int JEncoder_setString(JEncoder *o, const char *val, size_t len)
Write a quoted JSON string through BufPrint::jsonString.
struct JEncoder JEncoder
The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format.
int64_t S64
Signed 64-bit integer.
Definition: GenPrimT.h:90
int32_t S32
Signed 32-bit integer.
Definition: GenPrimT.h:88
uint8_t U8
Unsigned 8-bit integer.
Definition: GenPrimT.h:92
U8 BaBool
Boolean stored in an unsigned byte; FALSE is zero and TRUE is one.
Definition: GenPrimT.h:121
The BufPrint class, which implements a compact printf-style formatter, is a base class used by severa...
Definition: BufPrint.h:132
The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format.
Definition: JEncoder.h:74
int setJV(struct JVal *val, bool iterateNext=false)
Serialize a JVal node and its child values.
The JSON error container object.
Definition: JParser.h:173
JVal represents a value in a JSON tree.
Definition: JVal.h:78