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 5978 2026-09-11 16:13:48Z 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#ifndef NO_DOUBLE
102
109 int setDouble(double val);
110#endif
111
119 int setString(const char* val, size_t len);
120
128 int b64enc(const void* source, S32 slen);
129
139 int fmtString(const char* fmt,...);
140
149 int vFmtString(const char* fmt,va_list argList);
150
156 int setBoolean(bool val);
157
162 int setNull();
163
173 int setJV(struct JVal* val, bool iterateNext=false);
174
225 int set(const char* fmt, ...);
226
233 int setName(const char* name);
234
240 int beginObject();
241
247 int endObject();
248
254 int beginArray();
255
261 int endArray();
262
266 JErr* getErr();
267
273 int flush();
274
280 int commit();
281
286
287#endif
288 JErr* err;
289 struct
290 {
291 S32 level;
292 U8 data[64];
293 }objectStack;
294 BufPrint* out;
295 BaBool objectMember;
296 BaBool startNewObj;
298#ifdef __cplusplus
299extern "C" {
300#endif
304BA_API void JEncoder_constructor(JEncoder* o, JErr* err, BufPrint* out);
308#define JEncoder_destructor(o) JEncoder_flush(o)
320#define JEncoder_getErr(o) (o)->err
324BA_API int JEncoder_setInt(JEncoder* o, S32 val);
328BA_API int JEncoder_setLong(JEncoder* o, S64 val);
329#ifdef NO_DOUBLE
330#define JEncoder_setDouble JEncoder_setInt
331#else
335BA_API int JEncoder_setDouble(JEncoder* o, double val);
336#endif
337
341BA_API int JEncoder_fmtString(JEncoder* o, const char* fmt,...);
346 JEncoder* o, const char* fmt,va_list argList);
350BA_API int JEncoder_setString(JEncoder* o, const char* val, size_t len);
354BA_API int JEncoder_b64enc(JEncoder* o, const void* source, S32 slen);
366BA_API int JEncoder_setJV(
367 JEncoder* o, struct JVal* val, BaBool iterateNext);
368BA_API int JEncoder_vSetJV(
369 JEncoder* o,const char** fmt,va_list* argList);
373BA_API int JEncoder_set(JEncoder* o, const char* fmt, ...);
377BA_API int JEncoder_setName(JEncoder* o, const char* name);
397#define JEncoder_getBufPrint(o) (o)->out
398#ifdef __cplusplus
399}
400inline JEncoder::JEncoder(JErr* err, BufPrint* out) {
401 JEncoder_constructor(this,err, out); }
403 JEncoder_destructor(this); }
404inline int JEncoder::setInt(S32 val) {
405 return JEncoder_setInt(this, val); }
406inline int JEncoder::setLong(S64 val) {
407 return JEncoder_setLong(this, val); }
408#ifndef NO_DOUBLE
409inline int JEncoder::setDouble(double val) {
410 return JEncoder_setDouble(this, val); }
411#endif
412inline int JEncoder::fmtString(const char* fmt,...) {
413 int retv; va_list argList;
414 va_start(argList, fmt);
415 retv = JEncoder_fmtString(this, fmt, argList);
416 va_end(argList);
417 return retv;
418}
419inline int JEncoder::vFmtString(const char* fmt,va_list argList) {
420 return JEncoder_vFmtString(this, fmt, argList); }
421inline int JEncoder::setString(const char* val, size_t len) {
422 return JEncoder_setString(this, val, len); }
423inline int JEncoder::b64enc(const void* source, S32 slen) {
424 return JEncoder_b64enc(this, source, slen); }
425inline int JEncoder::setBoolean(bool val) {
426 return JEncoder_setBoolean(this, val ? TRUE : FALSE); }
427inline int JEncoder::setNull() {
428 return JEncoder_setNull(this); }
429inline int JEncoder::setJV(struct JVal* val, bool iterateNext) {
430 return JEncoder_setJV(this,val,iterateNext?TRUE:FALSE); }
431inline int JEncoder::set(const char* fmt, ...) {
432 int retv; va_list argList;
433 va_start(argList, fmt);
434 retv=JEncoder_vSetJV(this,&fmt,&argList);
435 va_end(argList);
436 return retv;
437}
438inline int JEncoder::setName(const char* name) {
439 return JEncoder_setName(this, name); }
441 return JEncoder_beginObject(this); }
443 return JEncoder_endObject(this); }
445 return JEncoder_beginArray(this); }
446inline int JEncoder::endArray() {
447 return JEncoder_endArray(this); }
449 return JEncoder_getErr(this); }
450inline int JEncoder::flush() {
451 return JEncoder_flush(this); }
452inline int JEncoder::commit() {
453 return JEncoder_commit(this); }
455 return JEncoder_getBufPrint(this); }
456#endif
457
461#define JE_MEMBER(o, m) #m, (o)->m
462
463 /* end of JSONRef */
465
466#endif
int setJV(struct JVal *val, bool iterateNext=false)
Serialize a JVal node and its child values.
Definition: JEncoder.h:429
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:400
int fmtString(const char *fmt,...)
Format text inside JSON quotation marks.
Definition: JEncoder.h:412
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:406
int setName(const char *name)
Write an object member name before its value.
Definition: JEncoder.h:438
int setBoolean(bool val)
Write a JSON boolean.
Definition: JEncoder.h:425
int beginObject()
Begin a JSON object value; call setName() before each member value.
Definition: JEncoder.h:440
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:450
int b64enc(const void *source, S32 slen)
Write binary data as a quoted standard Base64 string.
Definition: JEncoder.h:423
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:444
BA_API int JEncoder_fmtString(JEncoder *o, const char *fmt,...)
Format text inside JSON quotation marks.
BufPrint * getBufPrint()
Query output storage.
Definition: JEncoder.h:454
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:419
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:308
BA_API int JEncoder_setLong(JEncoder *o, S64 val)
Write a JSON number.
int setNull()
Write JSON null.
Definition: JEncoder.h:427
int endArray()
Close the current array after its final complete element.
Definition: JEncoder.h:446
int endObject()
Close the current object after its final complete member.
Definition: JEncoder.h:442
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:409
~JEncoder()
Call flush() without freeing the borrowed writer or error object.
Definition: JEncoder.h:402
int set(const char *fmt,...)
Encode/serialize C structs/data to JSON using formatted output.
Definition: JEncoder.h:431
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:320
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:448
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:452
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:397
int setString(const char *val, size_t len)
Write a quoted JSON string through BufPrint::jsonString.
Definition: JEncoder.h:421
int setInt(S32 val)
Write a JSON number.
Definition: JEncoder.h:404
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:87
int32_t S32
Signed 32-bit integer.
Definition: GenPrimT.h:85
uint8_t U8
Unsigned 8-bit integer.
Definition: GenPrimT.h:89
U8 BaBool
Boolean stored in an unsigned byte; FALSE is zero and TRUE is one.
Definition: GenPrimT.h:118
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
The JSON error container object.
Definition: JParser.h:173
JVal represents a value in a JSON tree.
Definition: JVal.h:78