libwebsockets
Lightweight C library for HTML5 websockets
lws-lejp.h File Reference
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _lejp_stack
 
struct  _lejp_parsing_stack
 
struct  lejp_ctx
 

Macros

#define LWS_ARRAY_SIZE(_x)   (sizeof(_x) / sizeof(_x[0]))
 
#define LEJP_FLAG_WS_KEEP   64
 
#define LEJP_FLAG_WS_COMMENTLINE   32
 
#define LEJP_FLAG_CB_IS_VALUE   64
 
#define LEJP_MAX_PARSING_STACK_DEPTH   8
 
#define LEJP_MAX_DEPTH   16
 
#define LEJP_MAX_INDEX_DEPTH   12
 
#define LEJP_MAX_PATH   192
 
#define LEJP_STRING_CHUNK   254
 
#define LEJP_FLAG_FEAT_OBJECT_INDEXES   (1 << 0)
 
#define LEJP_FLAG_FEAT_LEADING_WC   (1 << 1)
 
#define LEJP_FLAG_LATEST
 

Typedefs

typedef signed char(* lejp_callback) (struct lejp_ctx *ctx, char reason)
 

Enumerations

enum  lejp_states {
  LEJP_IDLE = 0 , LEJP_MEMBERS = 1 , LEJP_M_P = 2 , LEJP_MP_STRING = LEJP_FLAG_WS_KEEP | 3 ,
  LEJP_MP_STRING_ESC = LEJP_FLAG_WS_KEEP | 4 , LEJP_MP_STRING_ESC_U1 = LEJP_FLAG_WS_KEEP | 5 , LEJP_MP_STRING_ESC_U2 = LEJP_FLAG_WS_KEEP | 6 , LEJP_MP_STRING_ESC_U3 = LEJP_FLAG_WS_KEEP | 7 ,
  LEJP_MP_STRING_ESC_U4 = LEJP_FLAG_WS_KEEP | 8 , LEJP_MP_DELIM = 9 , LEJP_MP_VALUE = 10 , LEJP_MP_VALUE_NUM_INT = LEJP_FLAG_WS_KEEP | 11 ,
  LEJP_MP_VALUE_NUM_EXP = LEJP_FLAG_WS_KEEP | 12 , LEJP_MP_VALUE_TOK = LEJP_FLAG_WS_KEEP | 13 , LEJP_MP_COMMA_OR_END = 14 , LEJP_MP_ARRAY_END = 15
}
 
enum  lejp_reasons {
  LEJP_CONTINUE = -1 , LEJP_REJECT_IDLE_NO_BRACE = -2 , LEJP_REJECT_MEMBERS_NO_CLOSE = -3 , LEJP_REJECT_MP_NO_OPEN_QUOTE = -4 ,
  LEJP_REJECT_MP_STRING_UNDERRUN = -5 , LEJP_REJECT_MP_ILLEGAL_CTRL = -6 , LEJP_REJECT_MP_STRING_ESC_ILLEGAL_ESC = -7 , LEJP_REJECT_ILLEGAL_HEX = -8 ,
  LEJP_REJECT_MP_DELIM_MISSING_COLON = -9 , LEJP_REJECT_MP_DELIM_BAD_VALUE_START = -10 , LEJP_REJECT_MP_VAL_NUM_INT_NO_FRAC = -11 , LEJP_REJECT_MP_VAL_NUM_FORMAT = -12 ,
  LEJP_REJECT_MP_VAL_NUM_EXP_BAD_EXP = -13 , LEJP_REJECT_MP_VAL_TOK_UNKNOWN = -14 , LEJP_REJECT_MP_C_OR_E_UNDERF = -15 , LEJP_REJECT_MP_C_OR_E_NOTARRAY = -16 ,
  LEJP_REJECT_MP_ARRAY_END_MISSING = -17 , LEJP_REJECT_STACK_OVERFLOW = -18 , LEJP_REJECT_MP_DELIM_ISTACK = -19 , LEJP_REJECT_NUM_TOO_LONG = -20 ,
  LEJP_REJECT_MP_C_OR_E_NEITHER = -21 , LEJP_REJECT_UNKNOWN = -22 , LEJP_REJECT_CALLBACK = -23
}
 
enum  lejp_callbacks {
  LEJPCB_CONSTRUCTED = 0 , LEJPCB_DESTRUCTED = 1 , LEJPCB_START = 2 , LEJPCB_COMPLETE = 3 ,
  LEJPCB_FAILED = 4 , LEJPCB_PAIR_NAME = 5 , LEJPCB_VAL_TRUE = LEJP_FLAG_CB_IS_VALUE | 6 , LEJPCB_VAL_FALSE = LEJP_FLAG_CB_IS_VALUE | 7 ,
  LEJPCB_VAL_NULL = LEJP_FLAG_CB_IS_VALUE | 8 , LEJPCB_VAL_NUM_INT = LEJP_FLAG_CB_IS_VALUE | 9 , LEJPCB_VAL_NUM_FLOAT = LEJP_FLAG_CB_IS_VALUE | 10 , LEJPCB_VAL_STR_START = 11 ,
  LEJPCB_VAL_STR_CHUNK = LEJP_FLAG_CB_IS_VALUE | 12 , LEJPCB_VAL_STR_END = LEJP_FLAG_CB_IS_VALUE | 13 , LEJPCB_ARRAY_START = 14 , LEJPCB_ARRAY_END = 15 ,
  LEJPCB_OBJECT_START = 16 , LEJPCB_OBJECT_END = 17 , LEJPCB_USER_START = 32
}
 
enum  num_flags { LEJP_SEEN_MINUS = (1 << 0) , LEJP_SEEN_POINT = (1 << 1) , LEJP_SEEN_POST_POINT = (1 << 2) , LEJP_SEEN_EXP = (1 << 3) }
 

Functions

LWS_EXTERN signed char _lejp_callback (struct lejp_ctx *ctx, char reason)
 
LWS_VISIBLE LWS_EXTERN void lejp_construct (struct lejp_ctx *ctx, signed char(*callback)(struct lejp_ctx *ctx, char reason), void *user, const char *const *paths, unsigned char paths_count)
 
LWS_VISIBLE LWS_EXTERN void lejp_destruct (struct lejp_ctx *ctx)
 
LWS_VISIBLE LWS_EXTERN int lejp_parse (struct lejp_ctx *ctx, const unsigned char *json, int len)
 
LWS_VISIBLE LWS_EXTERN void lejp_change_callback (struct lejp_ctx *ctx, signed char(*callback)(struct lejp_ctx *ctx, char reason))
 
LWS_VISIBLE LWS_EXTERN int lejp_parser_push (struct lejp_ctx *ctx, void *user, const char *const *paths, unsigned char paths_count, lejp_callback lejp_cb)
 
LWS_VISIBLE LWS_EXTERN int lejp_parser_pop (struct lejp_ctx *ctx)
 
LWS_VISIBLE LWS_EXTERN void lejp_check_path_match (struct lejp_ctx *ctx)
 
LWS_VISIBLE LWS_EXTERN int lejp_get_wildcard (struct lejp_ctx *ctx, int wildcard, char *dest, int len)
 
LWS_VISIBLE LWS_EXTERN const char * lejp_error_to_string (int e)
 

Data Structure Documentation

◆ _lejp_stack

struct _lejp_stack

Definition at line 204 of file lws-lejp.h.

+ Collaboration diagram for _lejp_stack:
Data Fields
char s
char p
char i
char b

◆ lejp_ctx

struct lejp_ctx

Definition at line 220 of file lws-lejp.h.

+ Collaboration diagram for lejp_ctx:
Data Fields
void * user
struct _lejp_parsing_stack pst[LEJP_MAX_PARSING_STACK_DEPTH]
struct _lejp_stack st[LEJP_MAX_DEPTH]
uint16_t i[LEJP_MAX_INDEX_DEPTH]
uint16_t wild[LEJP_MAX_INDEX_DEPTH]
char path[LEJP_MAX_PATH]
char buf[LEJP_STRING_CHUNK+1]
size_t path_stride
uint32_t line
uint16_t uni
uint16_t flags
uint8_t npos
uint8_t dcount
uint8_t f
uint8_t sp
uint8_t ipos
uint8_t count_paths
uint8_t path_match
uint8_t path_match_len
uint8_t wildcount
uint8_t pst_sp
uint8_t outer_array

Macro Definition Documentation

◆ LWS_ARRAY_SIZE

#define LWS_ARRAY_SIZE (   _x)    (sizeof(_x) / sizeof(_x[0]))

Definition at line 35 of file lws-lejp.h.

◆ LEJP_FLAG_WS_KEEP

#define LEJP_FLAG_WS_KEEP   64

Definition at line 37 of file lws-lejp.h.

◆ LEJP_FLAG_WS_COMMENTLINE

#define LEJP_FLAG_WS_COMMENTLINE   32

Definition at line 38 of file lws-lejp.h.

◆ LEJP_FLAG_CB_IS_VALUE

#define LEJP_FLAG_CB_IS_VALUE   64

Definition at line 85 of file lws-lejp.h.

◆ LEJP_MAX_PARSING_STACK_DEPTH

#define LEJP_MAX_PARSING_STACK_DEPTH   8

Definition at line 181 of file lws-lejp.h.

◆ LEJP_MAX_DEPTH

#define LEJP_MAX_DEPTH   16

Definition at line 184 of file lws-lejp.h.

◆ LEJP_MAX_INDEX_DEPTH

#define LEJP_MAX_INDEX_DEPTH   12

Definition at line 187 of file lws-lejp.h.

◆ LEJP_MAX_PATH

#define LEJP_MAX_PATH   192

Definition at line 190 of file lws-lejp.h.

◆ LEJP_STRING_CHUNK

#define LEJP_STRING_CHUNK   254

Definition at line 194 of file lws-lejp.h.

◆ LEJP_FLAG_FEAT_OBJECT_INDEXES

#define LEJP_FLAG_FEAT_OBJECT_INDEXES   (1 << 0)

Definition at line 248 of file lws-lejp.h.

◆ LEJP_FLAG_FEAT_LEADING_WC

#define LEJP_FLAG_FEAT_LEADING_WC   (1 << 1)

Definition at line 249 of file lws-lejp.h.

◆ LEJP_FLAG_LATEST

#define LEJP_FLAG_LATEST
Value:
LEJP_FLAG_FEAT_LEADING_WC)
#define LEJP_FLAG_FEAT_OBJECT_INDEXES
Definition: lws-lejp.h:248

Definition at line 250 of file lws-lejp.h.

Typedef Documentation

◆ lejp_callback

typedef signed char(* lejp_callback) (struct lejp_ctx *ctx, char reason)

Definition at line 178 of file lws-lejp.h.

Enumeration Type Documentation

◆ lejp_states

Enumerator
LEJP_IDLE 
LEJP_MEMBERS 
LEJP_M_P 
LEJP_MP_STRING 
LEJP_MP_STRING_ESC 
LEJP_MP_STRING_ESC_U1 
LEJP_MP_STRING_ESC_U2 
LEJP_MP_STRING_ESC_U3 
LEJP_MP_STRING_ESC_U4 
LEJP_MP_DELIM 
LEJP_MP_VALUE 
LEJP_MP_VALUE_NUM_INT 
LEJP_MP_VALUE_NUM_EXP 
LEJP_MP_VALUE_TOK 
LEJP_MP_COMMA_OR_END 
LEJP_MP_ARRAY_END 

Definition at line 40 of file lws-lejp.h.

40  {
41  LEJP_IDLE = 0,
42  LEJP_MEMBERS = 1,
43  LEJP_M_P = 2,
50  LEJP_MP_DELIM = 9,
51  LEJP_MP_VALUE = 10,
56  LEJP_MP_ARRAY_END = 15,
57 };
@ LEJP_MP_VALUE_TOK
Definition: lws-lejp.h:54
@ LEJP_MP_STRING
Definition: lws-lejp.h:44
@ LEJP_M_P
Definition: lws-lejp.h:43
@ LEJP_MP_STRING_ESC_U4
Definition: lws-lejp.h:49
@ LEJP_MP_VALUE_NUM_EXP
Definition: lws-lejp.h:53
@ LEJP_MEMBERS
Definition: lws-lejp.h:42
@ LEJP_MP_STRING_ESC_U1
Definition: lws-lejp.h:46
@ LEJP_MP_DELIM
Definition: lws-lejp.h:50
@ LEJP_IDLE
Definition: lws-lejp.h:41
@ LEJP_MP_VALUE_NUM_INT
Definition: lws-lejp.h:52
@ LEJP_MP_STRING_ESC_U3
Definition: lws-lejp.h:48
@ LEJP_MP_ARRAY_END
Definition: lws-lejp.h:56
@ LEJP_MP_COMMA_OR_END
Definition: lws-lejp.h:55
@ LEJP_MP_STRING_ESC
Definition: lws-lejp.h:45
@ LEJP_MP_VALUE
Definition: lws-lejp.h:51
@ LEJP_MP_STRING_ESC_U2
Definition: lws-lejp.h:47
#define LEJP_FLAG_WS_KEEP
Definition: lws-lejp.h:37

◆ lejp_reasons

Enumerator
LEJP_CONTINUE 
LEJP_REJECT_IDLE_NO_BRACE 
LEJP_REJECT_MEMBERS_NO_CLOSE 
LEJP_REJECT_MP_NO_OPEN_QUOTE 
LEJP_REJECT_MP_STRING_UNDERRUN 
LEJP_REJECT_MP_ILLEGAL_CTRL 
LEJP_REJECT_MP_STRING_ESC_ILLEGAL_ESC 
LEJP_REJECT_ILLEGAL_HEX 
LEJP_REJECT_MP_DELIM_MISSING_COLON 
LEJP_REJECT_MP_DELIM_BAD_VALUE_START 
LEJP_REJECT_MP_VAL_NUM_INT_NO_FRAC 
LEJP_REJECT_MP_VAL_NUM_FORMAT 
LEJP_REJECT_MP_VAL_NUM_EXP_BAD_EXP 
LEJP_REJECT_MP_VAL_TOK_UNKNOWN 
LEJP_REJECT_MP_C_OR_E_UNDERF 
LEJP_REJECT_MP_C_OR_E_NOTARRAY 
LEJP_REJECT_MP_ARRAY_END_MISSING 
LEJP_REJECT_STACK_OVERFLOW 
LEJP_REJECT_MP_DELIM_ISTACK 
LEJP_REJECT_NUM_TOO_LONG 
LEJP_REJECT_MP_C_OR_E_NEITHER 
LEJP_REJECT_UNKNOWN 
LEJP_REJECT_CALLBACK 

Definition at line 59 of file lws-lejp.h.

59  {
60  LEJP_CONTINUE = -1,
81  LEJP_REJECT_UNKNOWN = -22,
83 };
@ LEJP_REJECT_ILLEGAL_HEX
Definition: lws-lejp.h:67
@ LEJP_REJECT_MP_C_OR_E_NEITHER
Definition: lws-lejp.h:80
@ LEJP_REJECT_MP_VAL_NUM_FORMAT
Definition: lws-lejp.h:71
@ LEJP_REJECT_MP_ARRAY_END_MISSING
Definition: lws-lejp.h:76
@ LEJP_REJECT_UNKNOWN
Definition: lws-lejp.h:81
@ LEJP_CONTINUE
Definition: lws-lejp.h:60
@ LEJP_REJECT_MP_DELIM_MISSING_COLON
Definition: lws-lejp.h:68
@ LEJP_REJECT_MP_C_OR_E_UNDERF
Definition: lws-lejp.h:74
@ LEJP_REJECT_MP_STRING_ESC_ILLEGAL_ESC
Definition: lws-lejp.h:66
@ LEJP_REJECT_MP_VAL_TOK_UNKNOWN
Definition: lws-lejp.h:73
@ LEJP_REJECT_MP_C_OR_E_NOTARRAY
Definition: lws-lejp.h:75
@ LEJP_REJECT_MP_VAL_NUM_INT_NO_FRAC
Definition: lws-lejp.h:70
@ LEJP_REJECT_MEMBERS_NO_CLOSE
Definition: lws-lejp.h:62
@ LEJP_REJECT_MP_DELIM_ISTACK
Definition: lws-lejp.h:78
@ LEJP_REJECT_IDLE_NO_BRACE
Definition: lws-lejp.h:61
@ LEJP_REJECT_CALLBACK
Definition: lws-lejp.h:82
@ LEJP_REJECT_NUM_TOO_LONG
Definition: lws-lejp.h:79
@ LEJP_REJECT_MP_STRING_UNDERRUN
Definition: lws-lejp.h:64
@ LEJP_REJECT_MP_NO_OPEN_QUOTE
Definition: lws-lejp.h:63
@ LEJP_REJECT_MP_ILLEGAL_CTRL
Definition: lws-lejp.h:65
@ LEJP_REJECT_STACK_OVERFLOW
Definition: lws-lejp.h:77
@ LEJP_REJECT_MP_DELIM_BAD_VALUE_START
Definition: lws-lejp.h:69
@ LEJP_REJECT_MP_VAL_NUM_EXP_BAD_EXP
Definition: lws-lejp.h:72

◆ lejp_callbacks

Enumerator
LEJPCB_CONSTRUCTED 
LEJPCB_DESTRUCTED 
LEJPCB_START 
LEJPCB_COMPLETE 
LEJPCB_FAILED 
LEJPCB_PAIR_NAME 
LEJPCB_VAL_TRUE 
LEJPCB_VAL_FALSE 
LEJPCB_VAL_NULL 
LEJPCB_VAL_NUM_INT 
LEJPCB_VAL_NUM_FLOAT 
LEJPCB_VAL_STR_START 
LEJPCB_VAL_STR_CHUNK 
LEJPCB_VAL_STR_END 
LEJPCB_ARRAY_START 
LEJPCB_ARRAY_END 
LEJPCB_OBJECT_START 
LEJPCB_OBJECT_END 
LEJPCB_USER_START 

Definition at line 87 of file lws-lejp.h.

87  {
90 
91  LEJPCB_START = 2,
92  LEJPCB_COMPLETE = 3,
93  LEJPCB_FAILED = 4,
94 
95  LEJPCB_PAIR_NAME = 5,
96 
102  LEJPCB_VAL_STR_START = 11, /* notice handle separately */
105 
106  LEJPCB_ARRAY_START = 14,
107  LEJPCB_ARRAY_END = 15,
108 
109  LEJPCB_OBJECT_START = 16,
110  LEJPCB_OBJECT_END = 17,
111 
112  LEJPCB_USER_START = 32,
113 };
@ LEJPCB_VAL_FALSE
Definition: lws-lejp.h:98
@ LEJPCB_OBJECT_END
Definition: lws-lejp.h:110
@ LEJPCB_USER_START
Definition: lws-lejp.h:112
@ LEJPCB_START
Definition: lws-lejp.h:91
@ LEJPCB_VAL_NUM_FLOAT
Definition: lws-lejp.h:101
@ LEJPCB_ARRAY_END
Definition: lws-lejp.h:107
@ LEJPCB_OBJECT_START
Definition: lws-lejp.h:109
@ LEJPCB_VAL_TRUE
Definition: lws-lejp.h:97
@ LEJPCB_VAL_NUM_INT
Definition: lws-lejp.h:100
@ LEJPCB_CONSTRUCTED
Definition: lws-lejp.h:88
@ LEJPCB_FAILED
Definition: lws-lejp.h:93
@ LEJPCB_COMPLETE
Definition: lws-lejp.h:92
@ LEJPCB_ARRAY_START
Definition: lws-lejp.h:106
@ LEJPCB_VAL_STR_END
Definition: lws-lejp.h:104
@ LEJPCB_VAL_NULL
Definition: lws-lejp.h:99
@ LEJPCB_VAL_STR_CHUNK
Definition: lws-lejp.h:103
@ LEJPCB_VAL_STR_START
Definition: lws-lejp.h:102
@ LEJPCB_DESTRUCTED
Definition: lws-lejp.h:89
@ LEJPCB_PAIR_NAME
Definition: lws-lejp.h:95
#define LEJP_FLAG_CB_IS_VALUE
Definition: lws-lejp.h:85

◆ num_flags

enum num_flags
Enumerator
LEJP_SEEN_MINUS 
LEJP_SEEN_POINT 
LEJP_SEEN_POST_POINT 
LEJP_SEEN_EXP 

Definition at line 197 of file lws-lejp.h.

197  {
198  LEJP_SEEN_MINUS = (1 << 0),
199  LEJP_SEEN_POINT = (1 << 1),
200  LEJP_SEEN_POST_POINT = (1 << 2),
201  LEJP_SEEN_EXP = (1 << 3)
202 };
@ LEJP_SEEN_EXP
Definition: lws-lejp.h:201
@ LEJP_SEEN_POINT
Definition: lws-lejp.h:199
@ LEJP_SEEN_MINUS
Definition: lws-lejp.h:198
@ LEJP_SEEN_POST_POINT
Definition: lws-lejp.h:200

Function Documentation

◆ _lejp_callback()

LWS_EXTERN signed char _lejp_callback ( struct lejp_ctx ctx,
char  reason 
)

_lejp_callback() - User parser actions

Parameters
ctxLEJP context
reasonCallback reason
 Your user callback is associated with the context at construction time,
 and receives calls as the parsing progresses.

 All of the callbacks may be ignored and just return 0.

 The reasons it might get called, found in @reason, are:
LEJPCB_CONSTRUCTED: The context was just constructed... you might want to perform one-time allocation for the life of the context.

LEJPCB_DESTRUCTED: The context is being destructed... if you made any allocations at construction-time, you can free them now

LEJPCB_START: Parsing is beginning at the first byte of input

LEJPCB_COMPLETE: Parsing has completed successfully. You'll get a 0 or positive return code from lejp_parse indicating the amount of unused bytes left in the input buffer

LEJPCB_FAILED: Parsing failed. You'll get a negative error code returned from lejp_parse

LEJPCB_PAIR_NAME: When a "name":"value" pair has had the name parsed, this callback occurs. You can find the new name at the end of ctx->path[]

LEJPCB_VAL_TRUE: The "true" value appeared

LEJPCB_VAL_FALSE: The "false" value appeared

LEJPCB_VAL_NULL: The "null" value appeared

LEJPCB_VAL_NUM_INT: A string representing an integer is in ctx->buf

LEJPCB_VAL_NUM_FLOAT: A string representing a float is in ctx->buf

LEJPCB_VAL_STR_START: We are starting to parse a string, no data yet

LEJPCB_VAL_STR_CHUNK: We filled the string buffer in the ctx, but it's not the end of the string. We produce this to spill the intermediate buffer to the user code, so we can handle huge JSON strings using only the small buffer in the ctx. If the whole JSON string fits in the ctx buffer, you won't get these callbacks.

LEJPCB_VAL_STR_END: String parsing has completed, the last chunk of the string is in ctx->buf.

LEJPCB_ARRAY_START: An array started

LEJPCB_ARRAY_END: An array ended

LEJPCB_OBJECT_START: An object started

LEJPCB_OBJECT_END: An object ended

◆ lejp_construct()

LWS_VISIBLE LWS_EXTERN void lejp_construct ( struct lejp_ctx ctx,
signed char(*)(struct lejp_ctx *ctx, char reason)  callback,
void *  user,
const char *const *  paths,
unsigned char  paths_count 
)

◆ lejp_destruct()

LWS_VISIBLE LWS_EXTERN void lejp_destruct ( struct lejp_ctx ctx)

◆ lejp_parse()

LWS_VISIBLE LWS_EXTERN int lejp_parse ( struct lejp_ctx ctx,
const unsigned char *  json,
int  len 
)

◆ lejp_change_callback()

LWS_VISIBLE LWS_EXTERN void lejp_change_callback ( struct lejp_ctx ctx,
signed char(*)(struct lejp_ctx *ctx, char reason)  callback 
)

◆ lejp_parser_push()

LWS_VISIBLE LWS_EXTERN int lejp_parser_push ( struct lejp_ctx ctx,
void *  user,
const char *const *  paths,
unsigned char  paths_count,
lejp_callback  lejp_cb 
)

◆ lejp_parser_pop()

LWS_VISIBLE LWS_EXTERN int lejp_parser_pop ( struct lejp_ctx ctx)

◆ lejp_check_path_match()

LWS_VISIBLE LWS_EXTERN void lejp_check_path_match ( struct lejp_ctx ctx)

◆ lejp_get_wildcard()

LWS_VISIBLE LWS_EXTERN int lejp_get_wildcard ( struct lejp_ctx ctx,
int  wildcard,
char *  dest,
int  len 
)

◆ lejp_error_to_string()

LWS_VISIBLE LWS_EXTERN const char* lejp_error_to_string ( int  e)