241 lines
6.5 KiB
C++
241 lines
6.5 KiB
C++
/**
|
|
* @file include/di/Json.h
|
|
* @brief JSON library wrapper
|
|
* @date June 1, 2015
|
|
* @author R.W.A. van der Heijden
|
|
* @copyright 2015 Dual Inventive Technology Centre B.V.
|
|
*
|
|
* This class wraps a JSON library (currently Jansson) in order to swap
|
|
* out JSON library later in the project because of the known performance
|
|
* issues with Jansson.
|
|
*/
|
|
#ifndef INCLUDE_DI_JSON_H_
|
|
#define INCLUDE_DI_JSON_H_
|
|
|
|
#include <memory>
|
|
|
|
struct json_t;
|
|
typedef struct json_t json_t;
|
|
|
|
namespace Di {
|
|
/**
|
|
* @class Json
|
|
* JSON library wrapper
|
|
*/
|
|
class Json {
|
|
public:
|
|
/**
|
|
* Json type enumeration
|
|
*/
|
|
enum Type : unsigned int {
|
|
J_OBJECT, /**< JSON Object */
|
|
J_ARRAY, /**< JSON Array */
|
|
J_STRING, /**< JSON String */
|
|
J_INTEGER,/**< JSON Integer (note, in json INT and DOUBLE are the same */
|
|
J_REAL, /**< JSON Real/Double */
|
|
J_BOOL, /**< JSON Boolean */
|
|
J_NULL /**< JSON NULL value */
|
|
};
|
|
typedef void * ObjectIterator; /**< Object iterator handle for object iteration */
|
|
/**
|
|
* Constructor which creates an empty JSON object with the supplied type
|
|
* @param t (currently) Only J_OBJECT and J_ARRAY are valid.
|
|
*/
|
|
explicit Json(Type t);
|
|
/**
|
|
* Constructor which creates a new JSON type with the internal type.
|
|
* only used internally in Json class
|
|
* @param obj New raw type
|
|
* @param decref Whether to delete the object or to discard it
|
|
*/
|
|
Json(json_t *obj, bool decref);
|
|
|
|
/**
|
|
* Constructor which create a new Json object based on a C++ STL string type
|
|
* @param message C++ STL String
|
|
*/
|
|
explicit Json(const std::string &message);
|
|
|
|
/**
|
|
* Constructor which create a new Json object based on a C++ STL string type
|
|
* @param message C++ STL String
|
|
*/
|
|
explicit Json(const std::shared_ptr<std::string> &message);
|
|
|
|
/**
|
|
* Constructor which create a new Json object based on a C++ STL string type
|
|
* @param message C++ STL String
|
|
* @param flags Internal flags, always 0
|
|
* @todo remove ..
|
|
*/
|
|
Json(const std::string &message, size_t flags);
|
|
|
|
/**
|
|
* Constructor which creates a new Json object based on the C-style char * and
|
|
* a defined size.
|
|
* @param string C-style array
|
|
* @param size Size of array
|
|
* @param flags internal flags, always 0
|
|
* @todo can we remove flags?
|
|
*/
|
|
Json(char *string, size_t size, size_t flags);
|
|
/**
|
|
* Destructor which cleans the Json object
|
|
*/
|
|
~Json();
|
|
|
|
/**
|
|
* Check whether there was an error in the Json class during the load of JSON data
|
|
* in the constructor
|
|
* @return True or false whether there was an error or not
|
|
*/
|
|
bool hasError();
|
|
|
|
/**
|
|
* Checks if the current element is an object
|
|
* @return true or false
|
|
*/
|
|
bool isObject();
|
|
/**
|
|
* Checks if the current element is a boolean
|
|
* @return true or false
|
|
*/
|
|
bool isBool();
|
|
/**
|
|
* Checks if the current element is a string
|
|
* @return true or false
|
|
*/
|
|
bool isString();
|
|
/**
|
|
* Checks if the current element is a number
|
|
* @return true or false
|
|
*/
|
|
bool isNumber();
|
|
/**
|
|
* Checks if the current element is an array
|
|
* @return true or false
|
|
*/
|
|
bool isArray();
|
|
|
|
/**
|
|
* Retrieves a key from the current object and return it in a new Json instance
|
|
* @param key Key of the requested object
|
|
* @return Shared pointer to a new Json object
|
|
*/
|
|
std::shared_ptr<Json> getObject(const std::string &key);
|
|
|
|
/**
|
|
* When the Json object is an array, return the size of the array in N elements
|
|
* @return Number of elements
|
|
*/
|
|
size_t getArraySize();
|
|
/**
|
|
* Get element from array for index N
|
|
* @param n Index to retrieve the element
|
|
* @return Shared pointer to a new Json object
|
|
*/
|
|
std::shared_ptr<Json> getArrayIndex(size_t n);
|
|
|
|
/**
|
|
* When the current element is a boolean, return its value
|
|
* @return true of false
|
|
*/
|
|
bool getBool();
|
|
bool getBool(const std::string &key);
|
|
/**
|
|
* When the current element is a string, return its value
|
|
* @return C++ style string
|
|
*/
|
|
std::string getString();
|
|
std::string getString(const std::string &key);
|
|
/**
|
|
* When the current element is a number, return its value
|
|
* @return int64_t number (note, double and int are the same in JSON)
|
|
*/
|
|
int64_t getNumber();
|
|
int64_t getNumber(const std::string &key);
|
|
/**
|
|
* When the current element is a real/double, return its value
|
|
* @return double precision number (note, double and int are the same in JSON)
|
|
*/
|
|
double getReal();
|
|
double getReal(const std::string &key);
|
|
/**
|
|
* Return the \ref Type of the current element
|
|
* @return returns the type
|
|
*/
|
|
Type getType();
|
|
|
|
/**
|
|
* When the Json object is an object, return the size of the object in N key-value-combinations
|
|
* @return Number of elements
|
|
*/
|
|
size_t numIter();
|
|
int beginIter(std::string *key, std::shared_ptr<Json> *value);
|
|
int nextIter(std::string *key, std::shared_ptr<Json> *value);
|
|
/**
|
|
* Performs a full (including children) copy on the current object
|
|
* @return Shared pointer to a new element
|
|
*/
|
|
std::shared_ptr<Json> copy();
|
|
|
|
/**
|
|
* Write a JSON null into an array
|
|
* @return 0 on success, -1 on error
|
|
*/
|
|
int setNull();
|
|
/**
|
|
* Write a JSON null into an object on position key, or append it
|
|
* to array when the current element is an array
|
|
* @param key Key position of the element
|
|
* @return 0 on success, -1 on error
|
|
*/
|
|
int setNull(const std::string &key);
|
|
int setString(const std::string &value);
|
|
int setString(const std::string &key, const std::string &value);
|
|
int setString(const char *value, size_t len);
|
|
int setString(const std::string &key, const char *value, size_t len);
|
|
|
|
int setNumber(int64_t value);
|
|
int setNumber(uint64_t value);
|
|
int setNumber(int32_t value) {
|
|
return setNumber(static_cast<int64_t>(value));
|
|
}
|
|
int setNumber(uint32_t value) {
|
|
return setNumber(static_cast<uint64_t>(value));
|
|
}
|
|
|
|
int setNumber(const std::string &key, int64_t value);
|
|
int setNumber(const std::string &key, uint64_t value);
|
|
|
|
int setNumber(const std::string &key, int32_t value) {
|
|
return setNumber(key, static_cast<int64_t>(value));
|
|
}
|
|
int setNumber(const std::string &key, uint32_t value) {
|
|
return setNumber(key, static_cast<uint64_t>(value));
|
|
}
|
|
|
|
int setReal(double value);
|
|
int setReal(const std::string &key, double value);
|
|
int setBool(bool value);
|
|
int setBool(const std::string &key, bool value);
|
|
int setObject(const std::shared_ptr<Json> &value);
|
|
int setObject(const std::string &key, const std::shared_ptr<Json> &value);
|
|
int setArray(const std::shared_ptr<Json> &value);
|
|
int setArray(const std::string &key, const std::shared_ptr<Json> &value);
|
|
|
|
std::string dumpString();
|
|
bool dumpFile(const std::string &filename);
|
|
json_t *getRawObject();
|
|
|
|
private:
|
|
json_t *obj;
|
|
bool decref;
|
|
ObjectIterator iter;
|
|
bool __hasError;
|
|
};
|
|
|
|
} // namespace Di
|
|
|
|
#endif // INCLUDE_DI_JSON_H_
|