1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 16:57:16 +01:00
SqMod/modules/json/Common.hpp

127 lines
4.1 KiB
C++
Raw Normal View History

#ifndef _SQJSON_COMMON_HPP_
#define _SQJSON_COMMON_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
// ------------------------------------------------------------------------------------------------
#include <jansson.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* SOFTWARE INFORMATION
*/
#define SQJSON_NAME "Squirrel JSON Module"
#define SQJSON_AUTHOR "Sandu Liviu Catalin (S.L.C)"
#define SQJSON_COPYRIGHT "Copyright (C) 2016 Sandu Liviu Catalin"
#define SQJSON_HOST_NAME "SqModJSONHost"
#define SQJSON_VERSION 001
#define SQJSON_VERSION_STR "0.0.1"
#define SQJSON_VERSION_MAJOR 0
#define SQJSON_VERSION_MINOR 0
#define SQJSON_VERSION_PATCH 1
2016-06-20 17:53:09 +02:00
/* ------------------------------------------------------------------------------------------------
* Forward declarations.
*/
class JValue;
class JArray;
class JObject;
/* ------------------------------------------------------------------------------------------------
* Implements RAII to free the strings obtained from dumps even after exceptions.
*/
struct CStrGuard
{
// --------------------------------------------------------------------------------------------
CStr mPtr; // The managed pointer
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
CStrGuard(CStr p)
: mPtr(p)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~CStrGuard()
{
if (mPtr)
{
std::free(mPtr);
}
}
};
/* ------------------------------------------------------------------------------------------------
2016-06-20 17:53:09 +02:00
* Class that represents an error occurred while parsing JSON data.
*/
2016-06-20 17:53:09 +02:00
struct JError
{
public:
// --------------------------------------------------------------------------------------------
json_error_t mErr; // The managed error instance.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
JError();
};
/* ------------------------------------------------------------------------------------------------
* Retrieve the string representation of JSON value type.
*/
CSStr JSONTypeToStr(json_type type);
/* ------------------------------------------------------------------------------------------------
* Retrieve the string representation of JSON value type.
*/
inline CSStr JSONTypeStr(json_t * ptr)
{
return (ptr == nullptr) ? _SC("unknown") : JSONTypeToStr(json_typeof(ptr));
}
2016-06-20 17:53:09 +02:00
/* ------------------------------------------------------------------------------------------------
* Convert a script value from the stack to a JSON object.
*/
Object SqFromJSON(HSQUIRRELVM vm, json_t * jval);
/* ------------------------------------------------------------------------------------------------
* Convert a script value from the stack to a JSON object.
*/
json_t * SqToJSON(HSQUIRRELVM vm, SQInteger idx);
/* ------------------------------------------------------------------------------------------------
* Convert a script table to a JSON object.
*/
JObject SqTableToJSONObject(Table & obj);
/* ------------------------------------------------------------------------------------------------
* Convert a script array to a JSON array.
*/
JArray SqArrayToJSONArray(Array & obj);
/* ------------------------------------------------------------------------------------------------
* Convert a script object to a JSON object.
*/
JObject SqObjectToJSONObject(Object & obj);
/* ------------------------------------------------------------------------------------------------
* Convert a script value to a JSON value.
*/
JValue SqValueToJSONValue(Object & obj);
} // Namespace:: SqMod
#endif // _SQJSON_COMMON_HPP_