Concord - C Discord API library
A Discord API wrapper library written in C
json-build.h File Reference
#include <stddef.h>
#include <stdio.h>
Include dependency graph for json-build.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  jsonb
 Handle for building a JSON string. More...
 

Macros

#define JSONB_API   extern
 
#define JSONB_MAX_DEPTH   128
 
#define TRACE(prev, next)   next
 
#define DECORATOR(a)
 
#define STACK_HEAD(b, state)   *(b)->top = (state)
 
#define STACK_PUSH(b, state)   TRACE(*(b)->top, *++(b)->top = (state))
 
#define STACK_POP(b)   TRACE(*(b)->top, DECORATOR(*)--(b)->top)
 
#define BUFFER_COPY_CHAR(b, c, _pos, buf, bufsize)
 
#define BUFFER_COPY(b, value, len, _pos, buf, bufsize)
 

Typedefs

typedef enum jsonbcode jsonbcode
 json-builder return codes More...
 
typedef struct jsonb jsonb
 Handle for building a JSON string. More...
 

Enumerations

enum  jsonbcode {
  JSONB_OK = 0 , JSONB_END , JSONB_ERROR_NOMEM = -1 , JSONB_ERROR_INPUT = -2 ,
  JSONB_ERROR_STACK = -3
}
 json-builder return codes More...
 
enum  jsonbstate {
  JSONB_INIT = 0 , JSONB_ARRAY_OR_OBJECT_OR_VALUE = JSONB_INIT , JSONB_OBJECT_KEY_OR_CLOSE , JSONB_OBJECT_VALUE ,
  JSONB_OBJECT_NEXT_KEY_OR_CLOSE , JSONB_ARRAY_VALUE_OR_CLOSE , JSONB_ARRAY_NEXT_VALUE_OR_CLOSE , JSONB_ERROR ,
  JSONB_DONE
}
 json-builder serializing state More...
 

Functions

void jsonb_init (jsonb *builder)
 Initialize a jsonb handle. More...
 
jsonbcode jsonb_object (jsonb *builder, char buf[], size_t bufsize)
 Push an object to the builder. More...
 
jsonbcode jsonb_object_pop (jsonb *builder, char buf[], size_t bufsize)
 Pop an object from the builder. More...
 
jsonbcode jsonb_key (jsonb *builder, char buf[], size_t bufsize, const char key[], size_t len)
 Push a key to the builder. More...
 
jsonbcode jsonb_array (jsonb *builder, char buf[], size_t bufsize)
 Push an array to the builder. More...
 
jsonbcode jsonb_array_pop (jsonb *builder, char buf[], size_t bufsize)
 Pop an array from the builder. More...
 
jsonbcode jsonb_token (jsonb *builder, char buf[], size_t bufsize, const char token[], size_t len)
 Push a raw JSON token to the builder. More...
 
jsonbcode jsonb_bool (jsonb *builder, char buf[], size_t bufsize, int boolean)
 Push a boolean token to the builder. More...
 
jsonbcode jsonb_null (jsonb *builder, char buf[], size_t bufsize)
 Push a null token to the builder. More...
 
jsonbcode jsonb_string (jsonb *builder, char buf[], size_t bufsize, const char str[], size_t len)
 Push a string token to the builder. More...
 
jsonbcode jsonb_number (jsonb *builder, char buf[], size_t bufsize, double number)
 Push a number token to the builder. More...
 

Macro Definition Documentation

◆ JSONB_API

#define JSONB_API   extern

◆ JSONB_MAX_DEPTH

#define JSONB_MAX_DEPTH   128

Maximum JSON nesting depth, if default value is unwanted then it should be defined before json-build.h is included:

#define JSONB_MAX_DEPTH 256 #include "json-build.h"

◆ TRACE

#define TRACE (   prev,
  next 
)    next

◆ DECORATOR

#define DECORATOR (   a)

◆ STACK_HEAD

#define STACK_HEAD (   b,
  state 
)    *(b)->top = (state)

◆ STACK_PUSH

#define STACK_PUSH (   b,
  state 
)    TRACE(*(b)->top, *++(b)->top = (state))

◆ STACK_POP

#define STACK_POP (   b)    TRACE(*(b)->top, DECORATOR(*)--(b)->top)

◆ BUFFER_COPY_CHAR

#define BUFFER_COPY_CHAR (   b,
  c,
  _pos,
  buf,
  bufsize 
)
Value:
do { \
if ((b)->pos + (_pos) + 1 + 1 > (bufsize)) { \
} \
(buf)[(b)->pos + (_pos)++] = (c); \
(buf)[(b)->pos + (_pos)] = '\0'; \
} while (0)
@ JSONB_ERROR_NOMEM
Definition: json-build.h:40

◆ BUFFER_COPY

#define BUFFER_COPY (   b,
  value,
  len,
  _pos,
  buf,
  bufsize 
)
Value:
do { \
size_t i; \
if ((b)->pos + (_pos) + (len) + 1 > (bufsize)) { \
} \
for (i = 0; i < (len); ++i) \
(buf)[(b)->pos + (_pos) + i] = (value)[i]; \
(_pos) += (len); \
(buf)[(b)->pos + (_pos)] = '\0'; \
} while (0)

Typedef Documentation

◆ jsonbcode

typedef enum jsonbcode jsonbcode

json-builder return codes

◆ jsonb

typedef struct jsonb jsonb

Handle for building a JSON string.

Enumeration Type Documentation

◆ jsonbcode

enum jsonbcode

json-builder return codes

Enumerator
JSONB_OK 

no error, operation was a success

JSONB_END 

string is complete, expects no more inputs

JSONB_ERROR_NOMEM 

not enough tokens were provided

JSONB_ERROR_INPUT 

token doesn't match expected value

JSONB_ERROR_STACK 

operation would lead to out of boundaries access

◆ jsonbstate

enum jsonbstate

json-builder serializing state

Enumerator
JSONB_INIT 
JSONB_ARRAY_OR_OBJECT_OR_VALUE 
JSONB_OBJECT_KEY_OR_CLOSE 
JSONB_OBJECT_VALUE 
JSONB_OBJECT_NEXT_KEY_OR_CLOSE 
JSONB_ARRAY_VALUE_OR_CLOSE 
JSONB_ARRAY_NEXT_VALUE_OR_CLOSE 
JSONB_ERROR 
JSONB_DONE 

Function Documentation

◆ jsonb_init()

void jsonb_init ( jsonb builder)

Initialize a jsonb handle.

Parameters
builderthe handle to be initialized

◆ jsonb_object()

jsonbcode jsonb_object ( jsonb builder,
char  buf[],
size_t  bufsize 
)

Push an object to the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
Returns
jsonbcode value

◆ jsonb_object_pop()

jsonbcode jsonb_object_pop ( jsonb builder,
char  buf[],
size_t  bufsize 
)

Pop an object from the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
Returns
jsonbcode value

◆ jsonb_key()

jsonbcode jsonb_key ( jsonb builder,
char  buf[],
size_t  bufsize,
const char  key[],
size_t  len 
)

Push a key to the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
keythe key to be inserted
lenthe key length
Returns
jsonbcode value

◆ jsonb_array()

jsonbcode jsonb_array ( jsonb builder,
char  buf[],
size_t  bufsize 
)

Push an array to the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
Returns
jsonbcode value

◆ jsonb_array_pop()

jsonbcode jsonb_array_pop ( jsonb builder,
char  buf[],
size_t  bufsize 
)

Pop an array from the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
Returns
jsonbcode value

◆ jsonb_token()

jsonbcode jsonb_token ( jsonb builder,
char  buf[],
size_t  bufsize,
const char  token[],
size_t  len 
)

Push a raw JSON token to the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
tokenthe token to be inserted
lenthe token length
Returns
jsonbcode value

◆ jsonb_bool()

jsonbcode jsonb_bool ( jsonb builder,
char  buf[],
size_t  bufsize,
int  boolean 
)

Push a boolean token to the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
booleanthe boolean to be inserted
Returns
jsonbcode value

◆ jsonb_null()

jsonbcode jsonb_null ( jsonb builder,
char  buf[],
size_t  bufsize 
)

Push a null token to the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
Returns
jsonbcode value

◆ jsonb_string()

jsonbcode jsonb_string ( jsonb builder,
char  buf[],
size_t  bufsize,
const char  str[],
size_t  len 
)

Push a string token to the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
strthe string to be inserted
lenthe string length
Returns
jsonbcode value

◆ jsonb_number()

jsonbcode jsonb_number ( jsonb builder,
char  buf[],
size_t  bufsize,
double  number 
)

Push a number token to the builder.

Parameters
builderthe builder initialized with jsonb_init()
bufthe JSON buffer
bufsizethe JSON buffer size
numberthe number to be inserted
Returns
jsonbcode value