libjapi  0.3.2
Universal JSON API Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
japi_utils.c File Reference

Universal JAPI helper functions library. More...

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <json-c/json.h>
#include "japi_utils.h"
Include dependency graph for japi_utils.c:

Functions

int japi_get_value_as_str (json_object *jobj, const char *key, const char **val)
 Get string value from json key. More...
 
int japi_get_value_as_bool (json_object *jobj, const char *key, bool *val)
 Get boolean value from json key. More...
 
int japi_get_value_as_int (json_object *jobj, const char *key, int *val)
 Get int value from json key. More...
 
int japi_get_value_as_int64 (json_object *jobj, const char *key, long long int *val)
 Get int64 value from json key. More...
 
int japi_get_value_as_double (json_object *jobj, const char *key, double *val)
 Get double value from json key. More...
 
char * japi_get_jobj_as_ndstr (json_object *jobj)
 Returns a string with added newline. More...
 

Detailed Description

Universal JAPI helper functions library.

Author
Deniz Armagan
Date
2019-04-10
Version
0.1

japi_utils is a universal JSON API helper library.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Function Documentation

char* japi_get_jobj_as_ndstr ( json_object *  jobj)

Returns a string with added newline.

Stringify the JSON object, copy it to a new allocated buffer and append a newline. This is necessary to ensure that a call to write() will send the message at once, meaning the message and the newline are not separated in two strings. The caller is responsible for freeing the returned string.

Parameters
jobjThe JSON object
Returns
On success, string is returned. On error, NULL is returned.

Referenced by japi_process_message(), and japi_pushsrv_sendmsg().

int japi_get_value_as_bool ( json_object *  jobj,
const char *  key,
bool *  val 
)

Get boolean value from json key.

Return boolean for the given json key.

Parameters
jobjThe json-object to parse
keyThe corresponding key to requested value
valThe requested value
Returns
On success, 0 is returned. On error, -1 if given JSON object is NULL, -2 if given key is NULL, -3 if given key doesn't exist, -4 if the key value is not a boolean type, is returned.
int japi_get_value_as_double ( json_object *  jobj,
const char *  key,
double *  val 
)

Get double value from json key.

Return double for the given json key.

Parameters
jobjThe json-object to parse
keyThe corresponding key to requested value
valThe requested value
Returns
On success, 0 is returned. On error, -1 if given JSON object is NULL, -2 if given key is NULL, -3 if given key doesn't exist, -4 if the key value is not a double type, is returned.
int japi_get_value_as_int ( json_object *  jobj,
const char *  key,
int *  val 
)

Get int value from json key.

Return int for the given json key.

Parameters
jobjThe json-object to parse
keyThe corresponding key to requested value
valThe requested value
Returns
On success, 0 is returned. On error, -1 if given JSON object is NULL, -2 if given key is NULL, -3 if given key doesn't exist, -4 if the key value is not a int type, is returned.
int japi_get_value_as_int64 ( json_object *  jobj,
const char *  key,
long long int *  val 
)

Get int64 value from json key.

Return int64 for the given json key.

Parameters
jobjThe json-object to parse
keyThe corresponding key to requested value
valThe requested value
Returns
On success, 0 is returned. On error, -1 if given JSON object is NULL, -2 if given key is NULL, -3 if given key doesn't exist, -4 if the key value is not a int64 type, is returned.
int japi_get_value_as_str ( json_object *  jobj,
const char *  key,
const char **  val 
)

Get string value from json key.

Return string for the given json key.

Parameters
jobjThe json-object to parse
keyThe corresponding key to requested value
valThe requested value
Returns
On success, 0 is returned. On error, -1 if given JSON object is NULL, -2 if given key is NULL, -3 if given key doesn't exist, -4 if the key value is not a string type, is returned.

Referenced by japi_process_message().