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
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
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
223extern "C" {
224#endif
225BA_API void JEncoder_constructor(JEncoder* o, JErr* err, BufPrint* out);
226#define JEncoder_destructor(o) JEncoder_flush(o)
227BA_API int JEncoder_flush(JEncoder* o);
228BA_API int JEncoder_commit(JEncoder* o);
229#define JEncoder_getErr(o) (o)->err
230BA_API int JEncoder_setInt(JEncoder* o, S32 val);
231BA_API int JEncoder_setLong(JEncoder* o, S64 val);
232#ifdef NO_DOUBLE
233#define JEncoder_setDouble JEncoder_setInt
234#else
235BA_API int JEncoder_setDouble(JEncoder* o, double val);
236#endif
237
238BA_API int JEncoder_fmtString(JEncoder* o, const char* fmt,...);
239BA_API int JEncoder_vFmtString(
240 JEncoder* o, const char* fmt,va_list argList);
241BA_API int JEncoder_setString(JEncoder* o, const char* val);
242BA_API int JEncoder_b64enc(JEncoder* o, const void* source, S32 slen);
243BA_API int JEncoder_setBoolean(JEncoder* o, BaBool val);
244BA_API int JEncoder_setNull(JEncoder* o);
245BA_API int JEncoder_setJV(
246 JEncoder* o, struct JVal* val, BaBool iterateNext);
247BA_API int JEncoder_vSetJV(
248 JEncoder* o,const char** fmt,va_list* argList);
249BA_API int JEncoder_set(JEncoder* o, const char* fmt, ...);
250BA_API int JEncoder_setName(JEncoder* o, const char* name);
251BA_API int JEncoder_beginObject(JEncoder* o);
252BA_API int JEncoder_endObject(JEncoder* o);
253BA_API int JEncoder_beginArray(JEncoder* o);
254BA_API int JEncoder_endArray(JEncoder* o);
255#define JEncoder_getBufPrint(o) (o)->out
256#ifdef __cplusplus
257}
258inline JEncoder::JEncoder(JErr* err, BufPrint* out) {
259 JEncoder_constructor(this,err, out); }
260inline JEncoder::~JEncoder() {
261 JEncoder_destructor(this); }
262inline int JEncoder::setInt(S32 val) {
263 return JEncoder_setInt(this, val); }
264inline int JEncoder::setLong(S64 val) {
265 return JEncoder_setLong(this, val); }
266#ifndef NO_DOUBLE
267inline int JEncoder::setDouble(double val) {
268 return JEncoder_setDouble(this, val); }
269#endif
270inline 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}
277inline int JEncoder::vFmtString(const char* fmt,va_list argList) {
278 return JEncoder_vFmtString(this, fmt, argList); }
279inline int JEncoder::setString(const char* val) {
280 return JEncoder_setString(this, val); }
281inline int JEncoder::b64enc(const void* source, S32 slen) {
282 return JEncoder_b64enc(this, source, slen); }
283inline int JEncoder::setBoolean(bool val) {
284 return JEncoder_setBoolean(this, val ? TRUE : FALSE); }
285inline int JEncoder::setNull() {
286 return JEncoder_setNull(this); }
287inline int JEncoder::setJV(struct JVal* val, bool iterateNext) {
288 return JEncoder_setJV(this,val,iterateNext?TRUE:FALSE); }
289inline 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}
296inline int JEncoder::setName(const char* name) {
297 return JEncoder_setName(this, name); }
299 return JEncoder_beginObject(this); }
301 return JEncoder_endObject(this); }
303 return JEncoder_beginArray(this); }
304inline int JEncoder::endArray() {
305 return JEncoder_endArray(this); }
307 return JEncoder_getErr(this); }
308inline int JEncoder::flush() {
309 return JEncoder_flush(this); }
310inline 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
int setJV(struct JVal *val, bool iterateNext=false)
Format a node or a tree of JVal nodes.
Definition: JEncoder.h:287
JEncoder(JErr *err, BufPrint *out)
Create an instance of the JEncoder class.
Definition: JEncoder.h:258
int fmtString(const char *fmt,...)
Converts (according to format 'fmt') and writes to stream.
Definition: JEncoder.h:270
int setLong(S64 val)
Format a long integer value.
Definition: JEncoder.h:264
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
int setBoolean(bool val)
Format a boolean value.
Definition: JEncoder.h:283
int beginObject()
Begin object formatting.
Definition: JEncoder.h:298
int flush()
Flush JEncoder data to the output stream.
Definition: JEncoder.h:308
int b64enc(const void *source, S32 slen)
Encode and emit (binary) data using B64 encoding.
Definition: JEncoder.h:281
int beginArray()
Begin array formatting.
Definition: JEncoder.h:302
BufPrint * getBufPrint()
Fetch the internal BufPrint object.
Definition: JEncoder.h:312
int setString(const char *val)
Format a string value.
Definition: JEncoder.h:279
int vFmtString(const char *fmt, va_list argList)
Equivalent to fmtString with variable argument list replaced by argList.
Definition: JEncoder.h:277
int setNull()
Set a JSON null value.
Definition: JEncoder.h:285
int endArray()
End array formatting.
Definition: JEncoder.h:304
int endObject()
End object formatting.
Definition: JEncoder.h:300
int setDouble(double val)
Format a double value.
Definition: JEncoder.h:267
int set(const char *fmt,...)
Encode/serialize C structs/data to JSON using formatted output.
Definition: JEncoder.h:289
JErr * getErr()
Returns the JErr object.
Definition: JEncoder.h:306
int commit()
Calls flush and enables the construction of a new object.
Definition: JEncoder.h:310
int setInt(S32 val)
Format an integer value.
Definition: JEncoder.h:262
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