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 5839 2026-07-29 13:27:22Z 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
38#ifndef __JEncoder_h
39#define __JEncoder_h
40
41#include "JVal.h"
42#include "BufPrint.h"
43
65typedef struct JEncoder
66{
67#ifdef __cplusplus
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
92 int setString(const char* val, size_t len);
93
96 int b64enc(const void* source, S32 slen);
97
103 int fmtString(const char* fmt,...);
104
108 int vFmtString(const char* fmt,va_list argList);
109
112 int setBoolean(bool val);
113
116 int setNull();
117
120 int setJV(struct JVal* val, bool iterateNext=false);
121
163 int set(const char* fmt, ...);
164
170 int setName(const char* name);
171
183 int beginObject();
184
187 int endObject();
188
191 int beginArray();
192
195 int endArray();
196
199 JErr* getErr();
200
203 int flush();
204
207 int commit();
208
212
213#endif
214 JErr* err;
215 struct
216 {
217 S32 level;
218 U8 data[64];
219 }objectStack;
220 BufPrint* out;
221 BaBool objectMember;
222 BaBool startNewObj;
224#ifdef __cplusplus
225extern "C" {
226#endif
227BA_API void JEncoder_constructor(JEncoder* o, JErr* err, BufPrint* out);
228#define JEncoder_destructor(o) JEncoder_flush(o)
229BA_API int JEncoder_flush(JEncoder* o);
230BA_API int JEncoder_commit(JEncoder* o);
231#define JEncoder_getErr(o) (o)->err
232BA_API int JEncoder_setInt(JEncoder* o, S32 val);
233BA_API int JEncoder_setLong(JEncoder* o, S64 val);
234#ifdef NO_DOUBLE
235#define JEncoder_setDouble JEncoder_setInt
236#else
237BA_API int JEncoder_setDouble(JEncoder* o, double val);
238#endif
239
240BA_API int JEncoder_fmtString(JEncoder* o, const char* fmt,...);
241BA_API int JEncoder_vFmtString(
242 JEncoder* o, const char* fmt,va_list argList);
243BA_API int JEncoder_setString(JEncoder* o, const char* val, size_t len);
244BA_API int JEncoder_b64enc(JEncoder* o, const void* source, S32 slen);
245BA_API int JEncoder_setBoolean(JEncoder* o, BaBool val);
246BA_API int JEncoder_setNull(JEncoder* o);
247BA_API int JEncoder_setJV(
248 JEncoder* o, struct JVal* val, BaBool iterateNext);
249BA_API int JEncoder_vSetJV(
250 JEncoder* o,const char** fmt,va_list* argList);
251BA_API int JEncoder_set(JEncoder* o, const char* fmt, ...);
252BA_API int JEncoder_setName(JEncoder* o, const char* name);
253BA_API int JEncoder_beginObject(JEncoder* o);
254BA_API int JEncoder_endObject(JEncoder* o);
255BA_API int JEncoder_beginArray(JEncoder* o);
256BA_API int JEncoder_endArray(JEncoder* o);
257#define JEncoder_getBufPrint(o) (o)->out
258#ifdef __cplusplus
259}
260inline JEncoder::JEncoder(JErr* err, BufPrint* out) {
261 JEncoder_constructor(this,err, out); }
262inline JEncoder::~JEncoder() {
263 JEncoder_destructor(this); }
264inline int JEncoder::setInt(S32 val) {
265 return JEncoder_setInt(this, val); }
266inline int JEncoder::setLong(S64 val) {
267 return JEncoder_setLong(this, val); }
268#ifndef NO_DOUBLE
269inline int JEncoder::setDouble(double val) {
270 return JEncoder_setDouble(this, val); }
271#endif
272inline int JEncoder::fmtString(const char* fmt,...) {
273 int retv; va_list argList;
274 va_start(argList, fmt);
275 retv = JEncoder_fmtString(this, fmt, argList);
276 va_end(argList);
277 return retv;
278}
279inline int JEncoder::vFmtString(const char* fmt,va_list argList) {
280 return JEncoder_vFmtString(this, fmt, argList); }
281inline int JEncoder::setString(const char* val, size_t len) {
282 return JEncoder_setString(this, val, len); }
283inline int JEncoder::b64enc(const void* source, S32 slen) {
284 return JEncoder_b64enc(this, source, slen); }
285inline int JEncoder::setBoolean(bool val) {
286 return JEncoder_setBoolean(this, val ? TRUE : FALSE); }
287inline int JEncoder::setNull() {
288 return JEncoder_setNull(this); }
289inline int JEncoder::setJV(struct JVal* val, bool iterateNext) {
290 return JEncoder_setJV(this,val,iterateNext?TRUE:FALSE); }
291inline int JEncoder::set(const char* fmt, ...) {
292 int retv; va_list argList;
293 va_start(argList, fmt);
294 retv=JEncoder_vSetJV(this,&fmt,&argList);
295 va_end(argList);
296 return retv;
297}
298inline int JEncoder::setName(const char* name) {
299 return JEncoder_setName(this, name); }
301 return JEncoder_beginObject(this); }
303 return JEncoder_endObject(this); }
305 return JEncoder_beginArray(this); }
306inline int JEncoder::endArray() {
307 return JEncoder_endArray(this); }
309 return JEncoder_getErr(this); }
310inline int JEncoder::flush() {
311 return JEncoder_flush(this); }
312inline int JEncoder::commit() {
313 return JEncoder_commit(this); }
315 return JEncoder_getBufPrint(this); }
316#endif
317
321#define JE_MEMBER(o, m) #m, (o)->m
322
323 /* end of JSONRef */
325
326#endif
int setJV(struct JVal *val, bool iterateNext=false)
Format a node or a tree of JVal nodes.
Definition: JEncoder.h:289
JEncoder(JErr *err, BufPrint *out)
Create an instance of the JEncoder class.
Definition: JEncoder.h:260
int fmtString(const char *fmt,...)
Converts (according to format 'fmt') and writes to stream.
Definition: JEncoder.h:272
int setLong(S64 val)
Format a long integer value.
Definition: JEncoder.h:266
int setName(const char *name)
Set the member name before calling any of the set<value> members if formatting an object.
Definition: JEncoder.h:298
int setBoolean(bool val)
Format a boolean value.
Definition: JEncoder.h:285
int beginObject()
Begin object formatting.
Definition: JEncoder.h:300
int flush()
Flush JEncoder data to the output stream.
Definition: JEncoder.h:310
int b64enc(const void *source, S32 slen)
Encode and emit (binary) data using B64 encoding.
Definition: JEncoder.h:283
int beginArray()
Begin array formatting.
Definition: JEncoder.h:304
BufPrint * getBufPrint()
Fetch the internal BufPrint object.
Definition: JEncoder.h:314
int vFmtString(const char *fmt, va_list argList)
Equivalent to fmtString with variable argument list replaced by argList.
Definition: JEncoder.h:279
int setNull()
Set a JSON null value.
Definition: JEncoder.h:287
int endArray()
End array formatting.
Definition: JEncoder.h:306
int endObject()
End object formatting.
Definition: JEncoder.h:302
int setDouble(double val)
Format a double value.
Definition: JEncoder.h:269
int set(const char *fmt,...)
Encode/serialize C structs/data to JSON using formatted output.
Definition: JEncoder.h:291
JErr * getErr()
Returns the JErr object.
Definition: JEncoder.h:308
int commit()
Calls flush and enables the construction of a new object.
Definition: JEncoder.h:312
int setString(const char *val, size_t len)
Format a string value.
Definition: JEncoder.h:281
int setInt(S32 val)
Format an integer value.
Definition: JEncoder.h:264
struct JEncoder JEncoder
The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format.
The BufPrint class, which implements an ANSI compatible printf method, is a base class used by severa...
Definition: BufPrint.h:122
The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format.
Definition: JEncoder.h:66
The JSON error container object.
Definition: JParser.h:155
JVal represents a value in a JSON tree.
Definition: JVal.h:73