Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
JEncoder Struct Reference

Detailed Description

The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format.

The JEncoder can also be used for assembling JSON text by calling the primitive methods in this class. Supply a valid sequence of names, values, and matching container delimiters. Writes are incremental: failure can leave partial output and does not roll back prior bytes. The borrowed JErr retains errors until explicitly reset by the caller.

Example:

JErr err;
char buf[40]; //Must be sufficiently large for the JSON string
BufPrint jBuf(buf,sizeof(buf));
JEncoder jEnc(&err,&jBuf);
int status = jEnc.set("{d}", "The number of the day is", (S32)5);
//jBuf.buf == buf
if(status == 0 && jBuf.cursor < sizeof(buf))
{
buf[jBuf.cursor]=0; // The encoder does not append a C-string terminator.
printf("%s\n",buf); // Prints: {"The number of the day is":5}
}
int32_t S32
Signed 32-bit integer.
Definition: GenPrimT.h:85
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

#include <JEncoder.h>

Public Member Functions

 JEncoder (JErr *err, BufPrint *out)
 Initialize an encoder without writing output or resetting err. More...
 
 ~JEncoder ()
 Call flush() without freeing the borrowed writer or error object. More...
 
int setInt (S32 val)
 Write a JSON number. More...
 
int setLong (S64 val)
 Write a JSON number. More...
 
int setDouble (double val)
 Write a JSON number. More...
 
int setString (const char *val, size_t len)
 Write a quoted JSON string through BufPrint::jsonString. More...
 
int b64enc (const void *source, S32 slen)
 Write binary data as a quoted standard Base64 string. More...
 
int fmtString (const char *fmt,...)
 Format text inside JSON quotation marks. More...
 
int vFmtString (const char *fmt, va_list argList)
 Write formatted text between JSON quotes, or JSON null. More...
 
int setBoolean (bool val)
 Write a JSON boolean. More...
 
int setNull ()
 Write JSON null. More...
 
int setJV (struct JVal *val, bool iterateNext=false)
 Serialize a JVal node and its child values. More...
 
int set (const char *fmt,...)
 Encode/serialize C structs/data to JSON using formatted output. More...
 
int setName (const char *name)
 Write an object member name before its value. More...
 
int beginObject ()
 Begin a JSON object value; call setName() before each member value. More...
 
int endObject ()
 Close the current object after its final complete member. More...
 
int beginArray ()
 Begin a JSON array value; array elements have no member-name call. More...
 
int endArray ()
 Close the current array after its final complete element. More...
 
JErr * getErr ()
 Query error storage. More...
 
int flush ()
 Flush the underlying writer if JErr is clear. More...
 
int commit ()
 Permit a new top-level value and flush the writer. More...
 
BufPrint * getBufPrint ()
 Query output storage. More...