Files

413 lines
10 KiB
C++

/**
* @file include/ActivMiddleware/Misc/RapidJsonHelper.h
*
* @brief A few helpers to ease rapidjson library usage.
*/
#if (!defined ACTIV_MIDDLEWARE_MISC_RAPID_JSON_HELPER_H)
#define ACTIV_MIDDLEWARE_MISC_RAPID_JSON_HELPER_H
#include "ActivMiddleware/ActivMiddleware.h"
#include "ActivMiddleware/ActivBase/Exception.h"
#include "ActivMiddleware/Misc/Fmt.h"
#include "rapidjson/document.h"
//! Activ namespace.
namespace Activ
{
// From Exception.cpp.
extern ACTIV_BASE_API String &GetThreadSpecificExceptionString();
//! Json namespace
namespace Json
{
//! Value type.
using Value = rapidjson::Document::ValueType;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Extension to rapidjson's writer classes to enable Key() overloads for integers.
*
* @tparam WRTIER the rapidjson writer type (rapidjson::writer, e.g. rapidjson::prettywriter).
*
* Note keys have to be quoted for strict JSON conformance, and parsing will fail without.
*/
template<typename WRITER>
class Writer : public WRITER
{
public:
// Inherit constructors and don't hide existing Key() overloads.
using WRITER::WRITER;
using WRITER::Key;
bool Key(const int key)
{
this->Prefix(rapidjson::kStringType);
Quoter quoter(*this);
return this->WriteInt(key);
}
bool Key(const int64_t key)
{
this->Prefix(rapidjson::kStringType);
Quoter quoter(*this);
return this->WriteInt64(key);
}
bool Key(const unsigned key)
{
this->Prefix(rapidjson::kStringType);
Quoter quoter(*this);
return this->WriteUint(key);
}
bool Key(const uint64_t key)
{
this->Prefix(rapidjson::kStringType);
Quoter quoter(*this);
return this->WriteUint64(key);
}
private:
class Quoter
{
public:
Quoter(Writer &writer) :
m_writer(writer)
{
m_writer.os_->Put('\"');
}
~Quoter()
{
m_writer.os_->Put('\"');
}
private:
Writer &m_writer;
};
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! Simple exception. Not much reported by rapidjson on errors.
class Exception : public virtual Boost::exception, public virtual std::exception
{
public:
typedef Boost::error_info<class StatusCodeTag, StatusCode> StatusCodeInfo;
typedef Boost::error_info<class DescriptionTag, String> DescriptionInfo;
//! Default constructor.
Exception()
{
}
// std::exception overrides.
virtual const char *what() const throw() ACTIV_OVERRIDE
{
String &output(GetThreadSpecificExceptionString());
output.assign("rapidjson error");
const StatusCode * const pStatusCodeInfo = Boost::get_error_info<StatusCodeInfo>(*this);
const String * const pDescriptionInfo = Boost::get_error_info<DescriptionInfo>(*this);
if (ACTIV_NULLPTR != pDescriptionInfo)
output.append(Fmt::Format(": {}", *pDescriptionInfo));
if (ACTIV_NULLPTR != pStatusCodeInfo)
output.append(Fmt::Format(": {} ({})", StatusCodeToString(*pStatusCodeInfo), *pStatusCodeInfo));
const String locationInformation(GetExceptionThrowLocationInfo(*this));
if (!locationInformation.empty())
output.append(Fmt::Format(": {}", locationInformation));
return output.c_str();
}
private:
String m_errorString;
};
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Scoped helper for writing arrays.
*
* @tparam W a rapidjson writer type.
*/
template<typename W>
class ScopedArray
{
public:
/**
* @brief Constructor.
*
* Starts the array.
*
* @param writer a rapidjson writer object.
*/
ScopedArray(W &writer) :
m_writer(writer)
{
if (ACTIV_UNLIKELY(!m_writer.StartArray()))
ACTIV_BOOST_THROW_EXCEPTION(Exception());
}
//! Destructor. Ends the array.
~ScopedArray()
{
(void) m_writer.EndArray();
}
private:
W &m_writer;
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper function to create a ScopedArray for template writer type.
*
* @tparam W a rapidjson writer type.
*
* @param writer a rapidjson writer object.
*
* @return ScopedJsonArray.
*/
template<typename W>
ScopedArray<W> MakeScopedArray(W &writer)
{
return ScopedArray<W>(writer);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Scoped helper for writing JSON arrays.
*
* @tparam W a rapidjson writer type.
*/
template<typename W>
class ScopedObject
{
public:
/**
* @brief Constructor.
*
* Starts the object.
*
* @param writer a rapidjson writer object.
*/
ScopedObject(W &writer) :
m_writer(writer)
{
if (ACTIV_UNLIKELY(!m_writer.StartObject()))
ACTIV_BOOST_THROW_EXCEPTION(Exception());
}
//! Destructor. Ends the object.
~ScopedObject()
{
(void) m_writer.EndObject();
}
private:
W &m_writer;
};
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper function to create a ScopedObject for template writer type.
*
* @tparam W a rapidjson writer type.
*
* @param writer a rapidjson writer object.
*
* @return ScopedObject.
*/
template<typename W>
ScopedObject<W> MakeScopedObject(W &writer)
{
return ScopedObject<W>(writer);
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Append a key.
*
* @tparam W a rapidjson writer type.
* @tparam T type of the key. Only string and integrals are supported. Integrals will be quoted.
*
* @param writer
* @param key
*
* @throw std::exception
*/
template<typename W, typename T>
void AppendKey(W &writer, const T &key);
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Append a value.
*
* @tparam W a rapidjson writer type.
* @tparam T the type of the value.
*
* @param writer
* @param value
*
* @throw std::exception
*/
template<typename W, typename T>
void AppendValue(W &writer, const T &value);
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Append a value array.
*
* @tparam W a rapidjson writer type.
* @tparam Ts the types of the value.
*
* @param writer
* @param values
*
* @throw std::exception
*/
template<typename W, typename... Ts>
void AppendValueArray(W &writer, const Ts &... values);
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Append a key-value.
*
* @tparam W a rapidjson writer type.
* @tparam S the String-like type of the key name.
* @tparam T the type of the value.
*
* @param writer
* @param key
* @param value
*
* @throw std::exception
*/
template<typename W, typename S, typename T>
void AppendKeyValue(W &writer, const S &key, const T &value)
{
AppendKey(writer, key);
AppendValue(writer, value);
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Append a key-value array.
*
* @tparam W a rapidjson writer type.
* @tparam S the String-like type of the key name.
* @tparam Ts the types of the value.
*
* @param writer
* @param key
* @param values
*
* @throw std::exception
*/
template<typename W, typename S, typename... Ts>
void AppendKeyValueArray(W &writer, const S &key, const Ts &... values)
{
AppendKey(writer, key);
AppendValueArray(writer, values...);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Validate a value.
*
* @tparam T the target type.
*
* @param value JSON value.
* @param target
*/
template<typename T>
void ValidateValue(const Value &value, T &target);
} // namespace Json
} // namespace Activ
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
#include "ActivMiddleware/Misc/Detail/RapidJsonHelperDetail.h"
//! Activ namespace.
namespace Activ
{
// Json namespace.
namespace Json
{
template<typename W, typename T>
void AppendKey(W &writer, const T &key)
{
using namespace Detail;
AppendKey(writer, key, typename CategoryTag<T>::type());
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename W, typename T>
void AppendValue(W &writer, const T &value)
{
using namespace Detail;
AppendValue(writer, value, typename CategoryTag<T>::type());
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename W, typename... Ts>
void AppendValueArray(W &writer, const Ts &... values)
{
using namespace Detail;
ScopedArray<W> scopedArray(writer);
AppendArrayValues(writer, values...);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename T>
void ValidateValue(const Value &value, T &target)
{
using namespace Detail;
ValidateValue(value, target, typename CategoryTag<T>::type());
}
} // namespace Json
} // namespace Activ
#endif // (!defined ACTIV_MIDDLEWARE_MISC_RAPID_JSON_HELPER_H)