17#define JSONB_API static
19#define JSONB_API extern
22#ifndef JSONB_MAX_DEPTH
30#define JSONB_MAX_DEPTH 128
110 jsonb *builder,
char buf[],
size_t bufsize,
const char key[],
size_t len);
185 jsonb *builder,
char buf[],
size_t bufsize,
const char str[],
size_t len);
204#define TRACE(prev, next) next
219 default:
return "unknown";
222#define TRACE(prev, next) \
224 enum jsonbstate _prev = prev, _next = next; \
225 fprintf(stderr, "%s():L%d | %s -> %s\n", __func__, __LINE__, \
226 _jsonb_eval_state(_prev), _jsonb_eval_state(_next)); \
228#define DECORATOR(d) d
231#define STACK_HEAD(b, state) *(b)->top = (state)
232#define STACK_PUSH(b, state) TRACE(*(b)->top, *++(b)->top = (state))
233#define STACK_POP(b) TRACE(*(b)->top, DECORATOR(*)--(b)->top)
235#define BUFFER_COPY_CHAR(b, c, _pos, buf, bufsize) \
237 if ((b)->pos + (_pos) + 1 + 1 > (bufsize)) { \
238 return JSONB_ERROR_NOMEM; \
240 (buf)[(b)->pos + (_pos)++] = (c); \
241 (buf)[(b)->pos + (_pos)] = '\0'; \
243#define BUFFER_COPY(b, value, len, _pos, buf, bufsize) \
246 if ((b)->pos + (_pos) + (len) + 1 > (bufsize)) { \
247 return JSONB_ERROR_NOMEM; \
249 for (i = 0; i < (len); ++i) \
250 (buf)[(b)->pos + (_pos) + i] = (value)[i]; \
252 (buf)[(b)->pos + (_pos)] = '\0'; \
258 static jsonb empty_builder;
321 size_t *pos,
char buf[],
size_t bufsize,
const char str[],
size_t len)
323 char *esc_tok = NULL, _esc_tok[8] =
"\\u00";
324 char *esc_buf = NULL;
330 for (i = 0; i < len; ++i) {
331 unsigned char c = str[i];
334 case 0x22: esc_tok =
"\\\"";
break;
335 case 0x5C: esc_tok =
"\\\\";
break;
336 case '\b': esc_tok =
"\\b";
break;
337 case '\f': esc_tok =
"\\f";
break;
338 case '\n': esc_tok =
"\\n";
break;
339 case '\r': esc_tok =
"\\r";
break;
340 case '\t': esc_tok =
"\\t";
break;
341 default:
if (c <= 0x1F) {
342 static const char tohex[] =
"0123456789abcdef";
343 _esc_tok[4] = tohex[c >> 4];
344 _esc_tok[5] = tohex[c & 0xF];
351 for (j = 0; esc_tok[j]; j++) {
354 *esc_buf++ = esc_tok[j];
356 extra_bytes += j - 1;
366 *pos += len + extra_bytes;
371 for (j = 0; j < len; ++j)
372 buf[*pos + j] = str[j];
376 esc_buf = buf + *pos;
392 ret = _jsonb_escape(&pos, buf + b->
pos, bufsize, key, len);
465 jsonb *b,
char buf[],
size_t bufsize,
const char token[],
size_t len)
502 if (
boolean)
return jsonb_token(b, buf, bufsize,
"true", 4);
514 jsonb *b,
char buf[],
size_t bufsize,
const char str[],
size_t len)
543 ret = _jsonb_escape(&pos, buf + b->
pos, bufsize, str, len);
555 long len = sprintf(token,
"%.17G", number);
struct jsonb jsonb
Handle for building a JSON string.
jsonbcode jsonb_array(jsonb *builder, char buf[], size_t bufsize)
Push an array to the builder.
Definition: json-build.h:408
jsonbstate
json-builder serializing state
Definition: json-build.h:48
@ JSONB_OBJECT_VALUE
Definition: json-build.h:52
@ JSONB_INIT
Definition: json-build.h:49
@ JSONB_ARRAY_VALUE_OR_CLOSE
Definition: json-build.h:54
@ JSONB_OBJECT_NEXT_KEY_OR_CLOSE
Definition: json-build.h:53
@ JSONB_ARRAY_NEXT_VALUE_OR_CLOSE
Definition: json-build.h:55
@ JSONB_ERROR
Definition: json-build.h:56
@ JSONB_DONE
Definition: json-build.h:57
@ JSONB_OBJECT_KEY_OR_CLOSE
Definition: json-build.h:51
@ JSONB_ARRAY_OR_OBJECT_OR_VALUE
Definition: json-build.h:50
#define JSONB_MAX_DEPTH
Definition: json-build.h:30
jsonbcode jsonb_key(jsonb *builder, char buf[], size_t bufsize, const char key[], size_t len)
Push a key to the builder.
Definition: json-build.h:382
#define BUFFER_COPY_CHAR(b, c, _pos, buf, bufsize)
Definition: json-build.h:235
jsonbcode jsonb_token(jsonb *builder, char buf[], size_t bufsize, const char token[], size_t len)
Push a raw JSON token to the builder.
Definition: json-build.h:464
jsonbcode jsonb_string(jsonb *builder, char buf[], size_t bufsize, const char str[], size_t len)
Push a string token to the builder.
Definition: json-build.h:513
jsonbcode jsonb_object(jsonb *builder, char buf[], size_t bufsize)
Push an object to the builder.
Definition: json-build.h:264
#define STACK_PUSH(b, state)
Definition: json-build.h:232
jsonbcode jsonb_bool(jsonb *builder, char buf[], size_t bufsize, int boolean)
Push a boolean token to the builder.
Definition: json-build.h:500
jsonbcode jsonb_array_pop(jsonb *builder, char buf[], size_t bufsize)
Pop an array from the builder.
Definition: json-build.h:441
#define BUFFER_COPY(b, value, len, _pos, buf, bufsize)
Definition: json-build.h:243
jsonbcode jsonb_null(jsonb *builder, char buf[], size_t bufsize)
Push a null token to the builder.
Definition: json-build.h:507
jsonbcode jsonb_number(jsonb *builder, char buf[], size_t bufsize, double number)
Push a number token to the builder.
Definition: json-build.h:552
void jsonb_init(jsonb *builder)
Initialize a jsonb handle.
Definition: json-build.h:256
#define STACK_POP(b)
Definition: json-build.h:233
#define STACK_HEAD(b, state)
Definition: json-build.h:231
jsonbcode
json-builder return codes
Definition: json-build.h:34
@ JSONB_END
Definition: json-build.h:38
@ JSONB_ERROR_INPUT
Definition: json-build.h:42
@ JSONB_ERROR_STACK
Definition: json-build.h:44
@ JSONB_ERROR_NOMEM
Definition: json-build.h:40
@ JSONB_OK
Definition: json-build.h:36
jsonbcode jsonb_object_pop(jsonb *builder, char buf[], size_t bufsize)
Pop an object from the builder.
Definition: json-build.h:297
#define JSONB_API
Definition: json-build.h:19
Handle for building a JSON string.
Definition: json-build.h:61
enum jsonbstate * top
Definition: json-build.h:65
size_t pos
Definition: json-build.h:67
enum jsonbstate stack[128+1]
Definition: json-build.h:63