Skip to main content

sleepy_discord/json_wrapper.cpp

Namespaces​

Name
SleepyDiscord
SleepyDiscord::json

Functions​

Name
const std::stringcreateJSON(std::initializer_list< std::pair< std::string, std::string >> json)
const std::stringstring(const std::string & s)
const std::stringUInteger(const uint64_t num)
const std::stringoptionalUInteger(const uint64_t num)
const std::stringinteger(const int64_t num)
const std::stringoptionalInteger(const int64_t num)
const std::stringboolean(const bool boolean)

Functions Documentation​

function createJSON​

const std::string createJSON(
std::initializer_list< std::pair< std::string, std::string >> json
)

function string​

const std::string string(
const std::string & s
)

function UInteger​

const std::string UInteger(
const uint64_t num
)

function optionalUInteger​

const std::string optionalUInteger(
const uint64_t num
)

function integer​

const std::string integer(
const int64_t num
)

function optionalInteger​

const std::string optionalInteger(
const int64_t num
)

function boolean​

const std::string boolean(
const bool boolean
)

Source code​

#include "json_wrapper.h"
#include <stdexcept>
#include <string>

namespace SleepyDiscord { namespace json {
const std::string createJSON(std::initializer_list<std::pair<std::string, std::string>> json) {
std::string target;
target.reserve(2); //revents crash
for (std::pair<std::string, std::string> pair : json) {
if (pair.second != "") {
target += ",\"" + pair.first + "\":" + pair.second;
}
}
target[0] = '{';
target.push_back('}');
return target;
}

const std::string string(const std::string& s) {
if (s.empty())
return "";
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.String(s.data(), s.length());
return std::string(buffer.GetString(), buffer.GetSize());
}

const std::string UInteger(const uint64_t num) {
return std::to_string(num & 0x3FFFFFFFFFFFFF); //just in case numbers are larger then 53 bits
}

const std::string optionalUInteger(const uint64_t num) {
return num ? UInteger(num) : "";
}

const std::string integer(const int64_t num) {
return std::to_string(num & 0x803FFFFFFFFFFFFF); //just in case numbers are larger then 53 bits
}

const std::string optionalInteger(const int64_t num) {
return num ? integer(num) : "";
}

const std::string boolean(const bool boolean) {
return boolean ? "true" : "false";
}
}}

Updated on 13 April 2022 at 18:39:59 UTC