Tiny JSON

json-c-parserEn esta entrada os presento tiny json un parser JSON en C. Este parser da una solución de compromiso entre versatilidad y bajo perfil de memoria. Lo que lo convierte en una excelente opción para proyectos de IoT.

Para que el parser cree instancias de elementos JSON se le pasa un array de elementos JSON. Normalmente la  memoria para este array es reservada estáticamente. Este parser no tiene límites en niveles de anidamiento de objetos JSON y array. El único límite es el tamaño del array de elementos JSON que se le pase. No usa recursividad.

Para mantener un bajo uso de memoria y poco procesamiento no se hace copia de cadenas de caracteres. Cuando se pide el nombre o el valor de un elemento JSON se devuelve un puntero que apunta dentro de la cadena original con el JSON.

Aprovechando los caracteres de control de JSON como comas, corchetes, llaves y comillas se machacan algunos de ellos con el caracter nulo. Así los punteros retornados cuando se piden el nombre o valor de las propiedades JSON son punteros a cadenas terminadas por cero.

En el repositorio hay dos ejemplos. Uno busca unas propiedades determinas en un JSON. El otro no sabe nada del JSON y lo explora. La siguiente función es la que ejecuta el parser:


/** Parse a string to get a json.
  * @param str String pointer with a JSON object. It will be modified.
  * @param mem Array of json properties to allocate.
  * @param qty Number of elementes of mem.
  * @retval Null pointer if any was wrong in the parse process.
  * @retval If the parser process was successfully a valid handler
  *         of a json object. This property is always unnamed and 
  *         its type is JSON_OBJ. */
json_t const* json_create( char* str, json_t mem[], unsigned int qty );

Todos los elemtos JSON tienen un tipo de datos asociado. Se puede consultar el tipo de un elemento con la siguiente función getType.


/** Enumeration of codes of suported JSON properties types. */
typedef enum {
    JSON_OBJ, JSON_ARRAY, JSON_TEXT, JSON_BOOLEAN,
    JSON_INTEGER, JSON_REAL, JSON_SCIENTIFIC, JSON_NULL
} jsonType_t;

/** Get the type of a json property.
  * @param json A valid handler of a json property.
  * @return The code of type.*/
jsonType_t json_getType( json_t const* json );

Los elementos JSON de tipo object o array contienen a su vez otros elementos JSON. Se puede obtener el primer elemento con la función getChild. Una vez obtenido el primer elemento se pueden obtener los siguientes con la función getSibling.


/** Get the first property of a JSON object or array.
  * @param json A valid handler of a json property.
  *             Its type must be JSON_OBJ or JSON_ARRAY.
  * @retval The handler of the first property if there is.
  * @retval Null pointer if the json object has not porperties. */
json_t const* json_getChild( json_t const* json );

/** Get the next sibling of a JSON property that is 
  * within a JSON object or array.
  * @param json A valid handler of a json property.
  * @retval The handler of the next sbling if found.
  * @retval Null pointer if the json property is the last one. */
json_t const* json_getSibling( json_t const* json );

Todos los elementos JSON contenidos en un objeto tienen nombre. Se puede hacer búsquedas de elementos JSON por su nombre con la función getProperty. Y se puede obtener el nombre de un elemento con la función getName.


/** Search a property by its name in a JSON object.
  * @param obj A valid handler of a json object.
  *            Its type must be JSON_OBJ.
  * @param property The name of property to get.
  * @retval The handler of the json property if found.
  * @retval Null pointer if not found. */
json_t const* json_getProperty( json_t const* obj, char const* name);

/** Get the name of a json property.
  * @param json A valid handler of a json property.
  * @retval Pointer to null-terminated if property has name.
  * @retval Null pointer if the property is unnamed. */
char const* json_getName( json_t const* json );

Todos los elementos JSON que no sean del tipo object o array tienen un valor asociado.
Se puede obtener este valor en formato texto con la función getValue.


/** Get the value of a json property.
  * The type of property cannot be JSON_OBJ or JSON_ARRAY.
  * @param json A valid handler of a json property.
  * @return Pointer to null-terminated string with the value. */
char const* json_getValue( json_t const* property );

Para los elementos JSON del tipo integer, boolean o real hay funciones que convierte el valor del elemento en un tipo de C.


/** Get the value of a json boolean property.
  * @param property A valid handler of a json object. 
  *                Its type must be JSON_BOOLEAN.
  * @return The value stdbool. */
bool json_getBoolean( json_t const* property );

/** Get the value of a json integer property.
  * @param property A valid handler of a json object.
  *                 Its type must be JSON_INTEGER.
  * @return The value stdint. */
int64_t json_getInteger( json_t const* property );

/** Get the value of a json real property.
  * @param property A valid handler of a json object.
  *                 Its type must be JSON_REAL.
  * @return The value stdint. */
double json_getReal( json_t const* property );

 

Deja un comentario