le_json.h File Reference

Go to the source code of this file.

Typedefs

typedef void(* le_json_EventHandler_t) (le_json_Event_t event)
 
typedef void(* le_json_ErrorHandler_t) (le_json_Error_t error, const char *msg)
 
typedef struct le_json_ParsingSession * le_json_ParsingSessionRef_t
 

Enumerations

enum  le_json_Event_t {
  LE_JSON_OBJECT_START, LE_JSON_OBJECT_MEMBER, LE_JSON_OBJECT_END, LE_JSON_ARRAY_START,
  LE_JSON_ARRAY_END, LE_JSON_STRING, LE_JSON_NUMBER, LE_JSON_TRUE,
  LE_JSON_FALSE, LE_JSON_NULL, LE_JSON_DOC_END
}
 
enum  le_json_Error_t { LE_JSON_SYNTAX_ERROR, LE_JSON_READ_ERROR }
 
enum  le_json_ContextType_t {
  LE_JSON_CONTEXT_DOC, LE_JSON_CONTEXT_OBJECT, LE_JSON_CONTEXT_MEMBER, LE_JSON_CONTEXT_ARRAY,
  LE_JSON_CONTEXT_STRING, LE_JSON_CONTEXT_NUMBER, LE_JSON_CONTEXT_TRUE, LE_JSON_CONTEXT_FALSE,
  LE_JSON_CONTEXT_NULL
}
 

Functions

le_json_ParsingSessionRef_t le_json_Parse (int fd, le_json_EventHandler_t eventHandler, le_json_ErrorHandler_t errorHandler, void *opaquePtr)
 
void le_json_Cleanup (le_json_ParsingSessionRef_t session)
 
void le_json_SetEventHandler (le_json_EventHandler_t callbackFunc)
 
le_json_ContextType_t le_json_GetContextType (void)
 
void le_json_SetOpaquePtr (void *ptr)
 
void * le_json_GetOpaquePtr (void)
 
void le_json_SetErrorHandler (le_json_ErrorHandler_t callbackFunc)
 
const char * le_json_GetString (void)
 
double le_json_GetNumber (void)
 
const char * le_json_GetEventName (le_json_Event_t event)
 
const char * le_json_GetContextName (le_json_ContextType_t context)
 
size_t le_json_GetBytesRead (le_json_ParsingSessionRef_t session)
 
le_json_ParsingSessionRef_t le_json_GetSession (void)
 

Detailed Description

Legato JSON Parsing API include file.

Typedef Documentation

typedef void(* le_json_ErrorHandler_t) (le_json_Error_t error, const char *msg)

Callbacks for errors look like this.

Parameters
error[in] Indicates what type of error occured.
msg[in] Human-readable message describing the error. (Valid until handler returns.)
typedef void(* le_json_EventHandler_t) (le_json_Event_t event)

Callbacks for (non-error) parsing events look like this.

Parameters
event[in] Indicates what type of event occured.
typedef struct le_json_ParsingSession* le_json_ParsingSessionRef_t

Parsing session reference. Refers to a parsing session started by le_json_Parse(). Pass this to le_json_Cleanup() to stop the parsing and clean up memory allocated by the parser.

Enumeration Type Documentation

Enumeration of the different types of "contexts" that can exist during a parsing session.

Enumerator
LE_JSON_CONTEXT_DOC 

Top level of document, outside the main object/array.

LE_JSON_CONTEXT_OBJECT 

Parsing an object (set of named members).

LE_JSON_CONTEXT_MEMBER 

Parsing a member of an object.

LE_JSON_CONTEXT_ARRAY 

Parsing an array (list of unnamed elements).

LE_JSON_CONTEXT_STRING 

Parsing a string value.

LE_JSON_CONTEXT_NUMBER 

Parsing number value.

LE_JSON_CONTEXT_TRUE 

Parsing a true value.

LE_JSON_CONTEXT_FALSE 

Parsing a false value.

LE_JSON_CONTEXT_NULL 

Parsing a null value.

Enumeration of the different types of errors that can be reported during JSON document parsing.

Enumerator
LE_JSON_SYNTAX_ERROR 

Syntax error, such as a missing comma or extra comma.

LE_JSON_READ_ERROR 

Error when reading from the input byte stream.

Enumeration of all the different events that can be reported during JSON document parsing.

Enumerator
LE_JSON_OBJECT_START 

object started, subsequent object members are part of this object

LE_JSON_OBJECT_MEMBER 

object member name received: Call le_json_GetString() to get name

LE_JSON_OBJECT_END 

object finished, subsequent members/values are outside this object

LE_JSON_ARRAY_START 

array started, upcoming values are elements of this array

LE_JSON_ARRAY_END 

array finished, subsequent values are outside this array

LE_JSON_STRING 

string value received: call le_json_GetString() to get value

LE_JSON_NUMBER 

number value received: call le_json_GetNumber() to get value

LE_JSON_TRUE 

true value received

LE_JSON_FALSE 

false value received

LE_JSON_NULL 

null value received

LE_JSON_DOC_END 

End of the document reached. Parsing has stopped.

Function Documentation

void le_json_Cleanup ( le_json_ParsingSessionRef_t  session)

Stops parsing and cleans up memory allocated by the parser.

Warning
Be sure to stop parsing before closing the file descriptor.
Parameters
sessionThe parsing session to clean up (see le_json_Parse()).
size_t le_json_GetBytesRead ( le_json_ParsingSessionRef_t  session)
Returns
The number of bytes that have been read from the input stream so far.
Parameters
sessionParsing session.
const char* le_json_GetContextName ( le_json_ContextType_t  context)
Returns
Human readable string containing the name of a given JSON parsing context.
le_json_ContextType_t le_json_GetContextType ( void  )

Get the type of parsing context that the parser is currently in.

Returns
The context type.
Warning
This function can only be called inside event or error handlers.
const char* le_json_GetEventName ( le_json_Event_t  event)
Returns
Human readable string containing the name of a given JSON parsing event.
double le_json_GetNumber ( void  )

Fetches the value of a parsed number.

Warning
This function can only be called inside event handlers when LE_JSON_NUMBER events are being handled.
void* le_json_GetOpaquePtr ( void  )

Get the opaque pointer attached to the parser.

Returns
The pointer previously set by le_json_Parse() or a subsequent call to le_json_SetOpaquePtr().
Warning
This function can only be called inside event or error handlers.
le_json_ParsingSessionRef_t le_json_GetSession ( void  )

For use by an event handler or error handler to fetch the JSON parsing session reference for the session that called its handler function.

Returns
The session reference of the JSON parsing session.
Warning
This function can only be called inside event or error handlers.
const char* le_json_GetString ( void  )

Fetches a pointer to a string value or object member name.

This pointer is only valid until the event handler returns.

Warning
This function can only be called inside event handlers when LE_JSON_OBJECT_MEMBER or LE_JSON_STRING events are being handled.
le_json_ParsingSessionRef_t le_json_Parse ( int  fd,
le_json_EventHandler_t  eventHandler,
le_json_ErrorHandler_t  errorHandler,
void *  opaquePtr 
)

Parse a JSON document received via a file descriptor.

Returns
Reference to the JSON parsing session started by this function call.
Parameters
fdFile descriptor to read the JSON document from.
eventHandlerFunction to call when normal parsing events happen.
errorHandlerFunction to call when errors happen.
opaquePtrOpaque pointer to be fetched by handlers using le_json_GetOpaquePtr().
void le_json_SetErrorHandler ( le_json_ErrorHandler_t  callbackFunc)

Set the error handler function

Warning
This function can only be called inside event or error handlers.
Parameters
callbackFuncFunction to call when parsing errors happen.
void le_json_SetEventHandler ( le_json_EventHandler_t  callbackFunc)

Set the current context's event handler function.

Warning
This function can only be called inside event or error handlers.
Parameters
callbackFuncFunction to call when parsing events happen.
void le_json_SetOpaquePtr ( void *  ptr)

Set the opaque pointer attached to the parser.

Warning
This function can only be called inside event or error handlers.
Parameters
ptrOpaque pointer to be fetched using le_json_GetOpaquePtr().