This is Unofficial EPICS BASE Doxygen Site
yajl_gen.h File Reference

Interface to YAJL's JSON generation facilities. More...

#include "yajl_common.h"
+ Include dependency graph for yajl_gen.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct yajl_gen_tyajl_gen
 
typedef void(* yajl_print_t) (void *ctx, const char *str, size_t len)
 

Enumerations

enum  yajl_gen_status {
  yajl_gen_status_ok = 0, yajl_gen_keys_must_be_strings, yajl_max_depth_exceeded, yajl_gen_in_error_state,
  yajl_gen_generation_complete, yajl_gen_invalid_number, yajl_gen_no_buf, yajl_gen_invalid_string
}
 
enum  yajl_gen_option {
  yajl_gen_beautify = 0x01, yajl_gen_indent_string = 0x02, yajl_gen_print_callback = 0x04, yajl_gen_validate_utf8 = 0x08,
  yajl_gen_escape_solidus = 0x10
}
 

Functions

YAJL_API int yajl_gen_config (yajl_gen g, yajl_gen_option opt,...)
 
YAJL_API yajl_gen yajl_gen_alloc (const yajl_alloc_funcs *allocFuncs)
 
YAJL_API void yajl_gen_free (yajl_gen handle)
 
YAJL_API yajl_gen_status yajl_gen_integer (yajl_gen hand, long long int number)
 
YAJL_API yajl_gen_status yajl_gen_double (yajl_gen hand, double number)
 
YAJL_API yajl_gen_status yajl_gen_number (yajl_gen hand, const char *num, size_t len)
 
YAJL_API yajl_gen_status yajl_gen_string (yajl_gen hand, const unsigned char *str, size_t len)
 
YAJL_API yajl_gen_status yajl_gen_null (yajl_gen hand)
 
YAJL_API yajl_gen_status yajl_gen_bool (yajl_gen hand, int boolean)
 
YAJL_API yajl_gen_status yajl_gen_map_open (yajl_gen hand)
 
YAJL_API yajl_gen_status yajl_gen_map_close (yajl_gen hand)
 
YAJL_API yajl_gen_status yajl_gen_array_open (yajl_gen hand)
 
YAJL_API yajl_gen_status yajl_gen_array_close (yajl_gen hand)
 
YAJL_API yajl_gen_status yajl_gen_get_buf (yajl_gen hand, const unsigned char **buf, size_t *len)
 
YAJL_API void yajl_gen_clear (yajl_gen hand)
 

Detailed Description

Interface to YAJL's JSON generation facilities.

Author
Lloyd Hilaiel

Definition in file yajl_gen.h.

Typedef Documentation

typedef struct yajl_gen_t* yajl_gen

An opaque handle to a generator

Definition at line 59 of file yajl_gen.h.

typedef void(* yajl_print_t) (void *ctx, const char *str, size_t len)

A callback used for "printing" the results.

Definition at line 62 of file yajl_gen.h.

Enumeration Type Documentation

Configuration parameters for the parser, these may be passed to yajl_gen_config() along with option specific argument(s). In general, all configuration parameters default to off.

Enumerator
yajl_gen_beautify 

Generate indented (beautiful) output

yajl_gen_indent_string 

Set an indent string which is used when yajl_gen_beautify() is enabled. Maybe something like \t or some number of spaces. The default is four spaces ' '.

yajl_gen_print_callback 

Set a function and context argument that should be used to output generated json. the function should conform to the yajl_print_t() prototype while the context argument is a void * of your choosing.

Example:

yajl_gen_validate_utf8 

Normally the generator does not validate that strings you pass to it via yajl_gen_string() are valid UTF8. Enabling this option will cause it to do so.

yajl_gen_escape_solidus 

The forward solidus (slash or '/' in human) is not required to be escaped in json text. By default, YAJL will not escape it in the iterest of saving bytes. Setting this flag will cause YAJL to always escape '/' in generated JSON strings.

Definition at line 69 of file yajl_gen.h.

Generator status codes

Enumerator
yajl_gen_status_ok 

No error

yajl_gen_keys_must_be_strings 

At a point where a map key is generated, a function other than yajl_gen_string() was called

yajl_max_depth_exceeded 

YAJL's maximum generation depth was exceeded. see YAJL_MAX_DEPTH

yajl_gen_in_error_state 

A generator function (yajl_gen_XXX) was called while in an error state

yajl_gen_generation_complete 

A complete JSON document has been generated

yajl_gen_invalid_number 

yajl_gen_double() was passed an invalid floating point value (infinity or NaN).

yajl_gen_no_buf 

A print callback was passed in, so there is no internal buffer to get from

yajl_gen_invalid_string 

Returned from yajl_gen_string() when the yajl_gen_validate_utf8() option is enabled and an invalid was passed by client code.

Definition at line 32 of file yajl_gen.h.

Function Documentation

YAJL_API yajl_gen yajl_gen_alloc ( const yajl_alloc_funcs allocFuncs)

Allocate a generator handle

Parameters
allocFuncsAn optional pointer to a structure which allows the client to overide the memory allocation used by yajl. May be NULL, in which case malloc(), free() and realloc() will be used.
Returns
an allocated handle on success, NULL on failure (bad params)

Definition at line 97 of file yajl_gen.c.

98 {
99  yajl_gen g = NULL;
100  yajl_alloc_funcs afsBuffer;
101 
102  /* first order of business is to set up memory allocation routines */
103  if (afs != NULL) {
104  if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
105  {
106  return NULL;
107  }
108  } else {
109  yajl_set_default_alloc_funcs(&afsBuffer);
110  afs = &afsBuffer;
111  }
112 
113  g = (yajl_gen) YA_MALLOC(afs, sizeof(struct yajl_gen_t));
114  if (!g) return NULL;
115 
116  memset((void *) g, 0, sizeof(struct yajl_gen_t));
117  /* copy in pointers to allocation routines */
118  memcpy((void *) &(g->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
119 
121  g->ctx = yajl_buf_alloc(&(g->alloc));
122  g->indentString = " ";
123 
124  return g;
125 }
struct yajl_gen_t * yajl_gen
Definition: yajl_gen.h:59
#define YA_MALLOC(afs, sz)
Definition: yajl_alloc.h:32
yajl_buf yajl_buf_alloc(yajl_alloc_funcs *alloc)
Definition: yajl_buf.c:56
#define NULL
Definition: catime.c:38
void yajl_set_default_alloc_funcs(yajl_alloc_funcs *yaf)
Definition: yajl_alloc.c:43
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
yajl_alloc_funcs alloc
Definition: yajl_gen.c:47
yajl_print_t print
Definition: yajl_gen.c:44
const char * indentString
Definition: yajl_gen.c:42
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
void * ctx
Definition: yajl_gen.c:45
YAJL_API yajl_gen_status yajl_gen_array_close ( yajl_gen  hand)

Finish generating a JSON array.

Definition at line 318 of file yajl_gen.c.

319 {
322  if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
325  g->print(g->ctx, "]", 1);
327  return yajl_gen_status_ok;
328 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define DECREMENT_DEPTH
Definition: yajl_gen.c:173
YAJL_API yajl_gen_status yajl_gen_array_open ( yajl_gen  hand)

Start generating a JSON array.

Definition at line 306 of file yajl_gen.c.

307 {
310  g->state[g->depth] = yajl_gen_array_start;
311  g->print(g->ctx, "[", 1);
312  if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
314  return yajl_gen_status_ok;
315 }
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define INCREMENT_DEPTH
Definition: yajl_gen.c:170
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define INSERT_SEP
Definition: yajl_gen.c:134
YAJL_API yajl_gen_status yajl_gen_bool ( yajl_gen  hand,
int  boolean 
)

Generate a true or false value from boolean.

Definition at line 267 of file yajl_gen.c.

268 {
269  const char * val = boolean ? "true" : "false";
270 
272  g->print(g->ctx, val, (unsigned int)strlen(val));
275  return yajl_gen_status_ok;
276 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define INSERT_SEP
Definition: yajl_gen.c:134
YAJL_API void yajl_gen_clear ( yajl_gen  hand)

Clear yajl's output buffer, but maintain all internal generation state. This function will not "reset" the generator state, and is intended to enable incremental JSON outputing.

Definition at line 341 of file yajl_gen.c.

342 {
343  if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_clear((yajl_buf)g->ctx);
344 }
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
void yajl_buf_clear(yajl_buf buf)
Definition: yajl_buf.c:86
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
YAJL_API int yajl_gen_config ( yajl_gen  g,
yajl_gen_option  opt,
  ... 
)

Allow the modification of generator options subsequent to handle allocation (via yajl_alloc())

Returns
zero in case of errors, non-zero otherwise

Definition at line 51 of file yajl_gen.c.

52 {
53  int rv = 1;
54  va_list ap;
55  va_start(ap, opt);
56 
57  switch(opt) {
58  case yajl_gen_beautify:
60  if (va_arg(ap, int)) g->flags |= opt;
61  else g->flags &= ~opt;
62  break;
64  const char *indent = va_arg(ap, const char *);
65  g->indentString = indent;
66  for (; *indent; indent++) {
67  if (*indent != '\n'
68  && *indent != '\v'
69  && *indent != '\f'
70  && *indent != '\t'
71  && *indent != '\r'
72  && *indent != ' ')
73  {
74  g->indentString = NULL;
75  rv = 0;
76  }
77  }
78  break;
79  }
81  yajl_buf_free(g->ctx);
82  g->print = va_arg(ap, const yajl_print_t);
83  g->ctx = va_arg(ap, void *);
84  break;
85  default:
86  rv = 0;
87  }
88 
89  va_end(ap);
90 
91  return rv;
92 }
#define NULL
Definition: catime.c:38
unsigned int flags
Definition: yajl_gen.c:40
yajl_print_t print
Definition: yajl_gen.c:44
void yajl_buf_free(yajl_buf buf)
Definition: yajl_buf.c:68
const char * indentString
Definition: yajl_gen.c:42
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
void * ctx
Definition: yajl_gen.c:45
YAJL_API yajl_gen_status yajl_gen_double ( yajl_gen  hand,
double  number 
)

Generate a floating point number. number may not be infinity or NaN, as these have no representation in JSON. In these cases the generator will return yajl_gen_invalid_number

Definition at line 212 of file yajl_gen.c.

213 {
214  char i[32];
216  if (isnan(number) || isinf(number)) return yajl_gen_invalid_number;
218  sprintf(i, "%.20g", number);
219  g->print(g->ctx, i, (unsigned int)strlen(i));
222  return yajl_gen_status_ok;
223 }
int i
Definition: scan.c:967
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define isinf(x)
Definition: epicsMath.h:16
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define isnan(x)
Definition: epicsMath.h:21
#define INSERT_SEP
Definition: yajl_gen.c:134
YAJL_API void yajl_gen_free ( yajl_gen  handle)

Free a generator handle

Definition at line 128 of file yajl_gen.c.

129 {
130  if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_free((yajl_buf)g->ctx);
131  YA_FREE(&(g->alloc), g);
132 }
#define YA_FREE(afs, ptr)
Definition: yajl_alloc.h:33
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
void yajl_buf_free(yajl_buf buf)
Definition: yajl_buf.c:68
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
YAJL_API yajl_gen_status yajl_gen_get_buf ( yajl_gen  hand,
const unsigned char **  buf,
size_t *  len 
)

Access the null terminated generator buffer. If incrementally outputing JSON, one should call yajl_gen_clear() to clear the buffer. This allows stream generation.

Definition at line 331 of file yajl_gen.c.

333 {
334  if (g->print != (yajl_print_t)&yajl_buf_append) return yajl_gen_no_buf;
335  *buf = yajl_buf_data((yajl_buf)g->ctx);
336  *len = yajl_buf_len((yajl_buf)g->ctx);
337  return yajl_gen_status_ok;
338 }
size_t yajl_buf_len(yajl_buf buf)
Definition: yajl_buf.c:97
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
const unsigned char * yajl_buf_data(yajl_buf buf)
Definition: yajl_buf.c:92
YAJL_API yajl_gen_status yajl_gen_integer ( yajl_gen  hand,
long long int  number 
)

Generate an integer number.

Definition at line 200 of file yajl_gen.c.

201 {
202  char i[32];
204  sprintf(i, "%lld", number);
205  g->print(g->ctx, i, (unsigned int)strlen(i));
208  return yajl_gen_status_ok;
209 }
int i
Definition: scan.c:967
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define INSERT_SEP
Definition: yajl_gen.c:134
YAJL_API yajl_gen_status yajl_gen_map_close ( yajl_gen  hand)

Finish generating a JSON map.

Definition at line 292 of file yajl_gen.c.

293 {
296 
297  if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
300  g->print(g->ctx, "}", 1);
302  return yajl_gen_status_ok;
303 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define DECREMENT_DEPTH
Definition: yajl_gen.c:173
YAJL_API yajl_gen_status yajl_gen_map_open ( yajl_gen  hand)

Start generating a JSON map. This should be followed by calls to yajl_gen_string() to provide a key and another yajl_gen routine to provide the value associated with that key.

Definition at line 279 of file yajl_gen.c.

280 {
283 
284  g->state[g->depth] = yajl_gen_map_start;
285  g->print(g->ctx, "{", 1);
286  if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
288  return yajl_gen_status_ok;
289 }
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define INCREMENT_DEPTH
Definition: yajl_gen.c:170
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define INSERT_SEP
Definition: yajl_gen.c:134
YAJL_API yajl_gen_status yajl_gen_null ( yajl_gen  hand)

Generate a null value.

Definition at line 257 of file yajl_gen.c.

258 {
260  g->print(g->ctx, "null", strlen("null"));
263  return yajl_gen_status_ok;
264 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define INSERT_SEP
Definition: yajl_gen.c:134
YAJL_API yajl_gen_status yajl_gen_number ( yajl_gen  hand,
const char *  num,
size_t  len 
)

Generate a number from the string given in num.

Definition at line 226 of file yajl_gen.c.

227 {
229  g->print(g->ctx, s, l);
232  return yajl_gen_status_ok;
233 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define INSERT_SEP
Definition: yajl_gen.c:134
YAJL_API yajl_gen_status yajl_gen_string ( yajl_gen  hand,
const unsigned char *  str,
size_t  len 
)

Generate a string value or map key from str.

Definition at line 236 of file yajl_gen.c.

238 {
239  // if validation is enabled, check that the string is valid utf8
240  // XXX: This checking could be done a little faster, in the same pass as
241  // the string encoding
242  if (g->flags & yajl_gen_validate_utf8) {
243  if (!yajl_string_validate_utf8(str, len)) {
245  }
246  }
248  g->print(g->ctx, "\"", 1);
249  yajl_string_encode(g->print, g->ctx, str, len, g->flags & yajl_gen_escape_solidus);
250  g->print(g->ctx, "\"", 1);
253  return yajl_gen_status_ok;
254 }
int yajl_string_validate_utf8(const unsigned char *s, size_t len)
Definition: yajl_encode.c:182
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define str(v)
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define INSERT_SEP
Definition: yajl_gen.c:134
void yajl_string_encode(const yajl_print_t print, void *ctx, const unsigned char *str, size_t len, int escape_solidus)
Definition: yajl_encode.c:32