Robotran C Documentation
Classes | Macros | Typedefs | Functions
json.cc File Reference
#include "json.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Classes

struct  SB
 

Macros

#define out_of_memory()
 
#define sb_need(sb, need)
 
#define sb_putc(sb, c)
 
#define is_space(c)   ((c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == ' ')
 
#define is_digit(c)   ((c) >= '0' && (c) <= '9')
 
#define problem(...)
 

Typedefs

typedef uint32_t uchar_t
 

Functions

static char * json_strdup (const char *str)
 
static void sb_init (SB *sb)
 
static void sb_grow (SB *sb, int need)
 
static void sb_put (SB *sb, const char *bytes, int count)
 
static void sb_puts (SB *sb, const char *str)
 
static char * sb_finish (SB *sb)
 
static void sb_free (SB *sb)
 
static int utf8_validate_cz (const char *s)
 
static bool utf8_validate (const char *s)
 
static int utf8_read_char (const char *s, uchar_t *out)
 
static int utf8_write_char (uchar_t unicode, char *out)
 
static bool from_surrogate_pair (uint16_t uc, uint16_t lc, uchar_t *unicode)
 
static void to_surrogate_pair (uchar_t unicode, uint16_t *uc, uint16_t *lc)
 
static bool parse_value (const char **sp, JsonNode **out)
 
static bool parse_string (const char **sp, char **out)
 
static bool parse_number (const char **sp, double *out)
 
static bool parse_array (const char **sp, JsonNode **out)
 
static bool parse_object (const char **sp, JsonNode **out)
 
static bool parse_hex16 (const char **sp, uint16_t *out)
 
static bool expect_literal (const char **sp, const char *str)
 
static void skip_space (const char **sp)
 
static void emit_value (SB *out, const JsonNode *node)
 
static void emit_value_indented (SB *out, const JsonNode *node, const char *space, int indent_level)
 
static void emit_string (SB *out, const char *str)
 
static void emit_number (SB *out, double num)
 
static void emit_array (SB *out, const JsonNode *array)
 
static void emit_array_indented (SB *out, const JsonNode *array, const char *space, int indent_level)
 
static void emit_object (SB *out, const JsonNode *object)
 
static void emit_object_indented (SB *out, const JsonNode *object, const char *space, int indent_level)
 
static int write_hex16 (char *out, uint16_t val)
 
static JsonNodemknode (JsonTag tag)
 
static void append_node (JsonNode *parent, JsonNode *child)
 
static void prepend_node (JsonNode *parent, JsonNode *child)
 
static void append_member (JsonNode *object, char *key, JsonNode *value)
 
static bool tag_is_valid (unsigned int tag)
 
static bool number_is_valid (const char *num)
 
JsonNodejson_decode (const char *json)
 
char * json_encode (const JsonNode *node)
 
char * json_encode_string (const char *str)
 
char * json_stringify (const JsonNode *node, const char *space)
 
void json_delete (JsonNode *node)
 
bool json_validate (const char *json)
 
JsonNodejson_find_element (JsonNode *array, int index)
 
JsonNodejson_find_member (JsonNode *object, const char *name)
 
JsonNodejson_first_child (const JsonNode *node)
 
JsonNodejson_mknull (void)
 
JsonNodejson_mkbool (bool b)
 
static JsonNodemkstring (char *s)
 
JsonNodejson_mkstring (const char *s)
 
JsonNodejson_mknumber (double n)
 
JsonNodejson_mkarray (void)
 
JsonNodejson_mkobject (void)
 
void json_append_element (JsonNode *array, JsonNode *element)
 
void json_prepend_element (JsonNode *array, JsonNode *element)
 
void json_append_member (JsonNode *object, const char *key, JsonNode *value)
 
void json_prepend_member (JsonNode *object, const char *key, JsonNode *value)
 
void json_remove_from_parent (JsonNode *node)
 
bool json_check (const JsonNode *node, char errmsg[256])
 

Macro Definition Documentation

◆ is_digit

#define is_digit (   c)    ((c) >= '0' && (c) <= '9')

◆ is_space

#define is_space (   c)    ((c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == ' ')

◆ out_of_memory

#define out_of_memory ( )
Value:
do { \
fprintf(stderr, "Out of memory.\n"); \
exit(EXIT_FAILURE); \
} while (0)

◆ problem

#define problem (   ...)
Value:
do { \
if (errmsg != NULL) \
snprintf(errmsg, 256, __VA_ARGS__); \
return false; \
} while (0)

◆ sb_need

#define sb_need (   sb,
  need 
)
Value:
do { \
if ((sb)->end - (sb)->cur < (need)) \
sb_grow(sb, need); \
} while (0)

◆ sb_putc

#define sb_putc (   sb,
 
)
Value:
do { \
if ((sb)->cur >= (sb)->end) \
sb_grow(sb, 1); \
*(sb)->cur++ = (c); \
} while (0)

Typedef Documentation

◆ uchar_t

typedef uint32_t uchar_t

Function Documentation

◆ append_member()

static void append_member ( JsonNode object,
char *  key,
JsonNode value 
)
static

◆ append_node()

static void append_node ( JsonNode parent,
JsonNode child 
)
static

◆ emit_array()

static void emit_array ( SB out,
const JsonNode array 
)
static

◆ emit_array_indented()

static void emit_array_indented ( SB out,
const JsonNode array,
const char *  space,
int  indent_level 
)
static

◆ emit_number()

static void emit_number ( SB out,
double  num 
)
static

◆ emit_object()

static void emit_object ( SB out,
const JsonNode object 
)
static

◆ emit_object_indented()

static void emit_object_indented ( SB out,
const JsonNode object,
const char *  space,
int  indent_level 
)
static

◆ emit_string()

void emit_string ( SB out,
const char *  str 
)
static

◆ emit_value()

static void emit_value ( SB out,
const JsonNode node 
)
static

◆ emit_value_indented()

void emit_value_indented ( SB out,
const JsonNode node,
const char *  space,
int  indent_level 
)
static

◆ expect_literal()

static bool expect_literal ( const char **  sp,
const char *  str 
)
static

◆ from_surrogate_pair()

static bool from_surrogate_pair ( uint16_t  uc,
uint16_t  lc,
uchar_t unicode 
)
static

◆ json_append_element()

void json_append_element ( JsonNode array,
JsonNode element 
)

◆ json_append_member()

void json_append_member ( JsonNode object,
const char *  key,
JsonNode value 
)

◆ json_check()

bool json_check ( const JsonNode node,
char  errmsg[256] 
)

◆ json_decode()

JsonNode* json_decode ( const char *  json)

◆ json_delete()

void json_delete ( JsonNode node)

◆ json_encode()

char* json_encode ( const JsonNode node)

◆ json_encode_string()

char* json_encode_string ( const char *  str)

◆ json_find_element()

JsonNode* json_find_element ( JsonNode array,
int  index 
)

◆ json_find_member()

JsonNode* json_find_member ( JsonNode object,
const char *  name 
)

◆ json_first_child()

JsonNode* json_first_child ( const JsonNode node)

◆ json_mkarray()

JsonNode* json_mkarray ( void  )

◆ json_mkbool()

JsonNode* json_mkbool ( bool  b)

◆ json_mknull()

JsonNode* json_mknull ( void  )

◆ json_mknumber()

JsonNode* json_mknumber ( double  n)

◆ json_mkobject()

JsonNode* json_mkobject ( void  )

◆ json_mkstring()

JsonNode* json_mkstring ( const char *  s)

◆ json_prepend_element()

void json_prepend_element ( JsonNode array,
JsonNode element 
)

◆ json_prepend_member()

void json_prepend_member ( JsonNode object,
const char *  key,
JsonNode value 
)

◆ json_remove_from_parent()

void json_remove_from_parent ( JsonNode node)

◆ json_strdup()

static char* json_strdup ( const char *  str)
static

◆ json_stringify()

char* json_stringify ( const JsonNode node,
const char *  space 
)

◆ json_validate()

bool json_validate ( const char *  json)

◆ mknode()

static JsonNode * mknode ( JsonTag  tag)
static

◆ mkstring()

static JsonNode* mkstring ( char *  s)
static

◆ number_is_valid()

static bool number_is_valid ( const char *  num)
static

◆ parse_array()

static bool parse_array ( const char **  sp,
JsonNode **  out 
)
static

◆ parse_hex16()

static bool parse_hex16 ( const char **  sp,
uint16_t *  out 
)
static

◆ parse_number()

bool parse_number ( const char **  sp,
double *  out 
)
static

◆ parse_object()

static bool parse_object ( const char **  sp,
JsonNode **  out 
)
static

◆ parse_string()

bool parse_string ( const char **  sp,
char **  out 
)
static

◆ parse_value()

static bool parse_value ( const char **  sp,
JsonNode **  out 
)
static

◆ prepend_node()

static void prepend_node ( JsonNode parent,
JsonNode child 
)
static

◆ sb_finish()

static char* sb_finish ( SB sb)
static

◆ sb_free()

static void sb_free ( SB sb)
static

◆ sb_grow()

static void sb_grow ( SB sb,
int  need 
)
static

◆ sb_init()

static void sb_init ( SB sb)
static

◆ sb_put()

static void sb_put ( SB sb,
const char *  bytes,
int  count 
)
static

◆ sb_puts()

static void sb_puts ( SB sb,
const char *  str 
)
static

◆ skip_space()

static void skip_space ( const char **  sp)
static

◆ tag_is_valid()

static bool tag_is_valid ( unsigned int  tag)
static

◆ to_surrogate_pair()

static void to_surrogate_pair ( uchar_t  unicode,
uint16_t *  uc,
uint16_t *  lc 
)
static

◆ utf8_read_char()

static int utf8_read_char ( const char *  s,
uchar_t out 
)
static

◆ utf8_validate()

static bool utf8_validate ( const char *  s)
static

◆ utf8_validate_cz()

static int utf8_validate_cz ( const char *  s)
static

◆ utf8_write_char()

static int utf8_write_char ( uchar_t  unicode,
char *  out 
)
static

◆ write_hex16()

static int write_hex16 ( char *  out,
uint16_t  val 
)
static