Json: Add support for float, deduplicate code
This commit is contained in:
+6
-26
@@ -18,6 +18,7 @@
|
|||||||
#include "ruc/json/array.h"
|
#include "ruc/json/array.h"
|
||||||
#include "ruc/json/object.h"
|
#include "ruc/json/object.h"
|
||||||
#include "ruc/meta/assert.h"
|
#include "ruc/meta/assert.h"
|
||||||
|
#include "ruc/meta/concepts.h"
|
||||||
#include "ruc/meta/odr.h"
|
#include "ruc/meta/odr.h"
|
||||||
|
|
||||||
namespace ruc::json {
|
namespace ruc::json {
|
||||||
@@ -45,36 +46,15 @@ void fromJson(const Json& json, bool& boolean)
|
|||||||
boolean = json.asBool();
|
boolean = json.asBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Json>
|
template<typename Json, Integral T>
|
||||||
void fromJson(const Json& json, int32_t& number)
|
void fromJson(const Json& json, T& number)
|
||||||
{
|
{
|
||||||
VERIFY(json.type() == Json::Type::Number);
|
VERIFY(json.type() == Json::Type::Number);
|
||||||
number = static_cast<int32_t>(json.asDouble());
|
number = static_cast<T>(json.asDouble());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Json>
|
template<typename Json, FloatingPoint T>
|
||||||
void fromJson(const Json& json, uint32_t& number)
|
void fromJson(const Json& json, T& number)
|
||||||
{
|
|
||||||
VERIFY(json.type() == Json::Type::Number);
|
|
||||||
number = static_cast<uint32_t>(json.asDouble());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Json>
|
|
||||||
void fromJson(const Json& json, int64_t& number)
|
|
||||||
{
|
|
||||||
VERIFY(json.type() == Json::Type::Number);
|
|
||||||
number = static_cast<int64_t>(json.asDouble());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Json>
|
|
||||||
void fromJson(const Json& json, size_t& number) // uint64_t
|
|
||||||
{
|
|
||||||
VERIFY(json.type() == Json::Type::Number);
|
|
||||||
number = static_cast<size_t>(json.asDouble());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Json>
|
|
||||||
void fromJson(const Json& json, double& number)
|
|
||||||
{
|
{
|
||||||
VERIFY(json.type() == Json::Type::Number);
|
VERIFY(json.type() == Json::Type::Number);
|
||||||
number = json.asDouble();
|
number = json.asDouble();
|
||||||
|
|||||||
+5
-28
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include "ruc/json/array.h"
|
#include "ruc/json/array.h"
|
||||||
#include "ruc/json/object.h"
|
#include "ruc/json/object.h"
|
||||||
|
#include "ruc/meta/concepts.h"
|
||||||
#include "ruc/meta/odr.h"
|
#include "ruc/meta/odr.h"
|
||||||
|
|
||||||
namespace ruc::json {
|
namespace ruc::json {
|
||||||
@@ -30,46 +31,22 @@ struct jsonConstructor {
|
|||||||
json.m_value.boolean = boolean;
|
json.m_value.boolean = boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Json>
|
template<typename Json, Integral T>
|
||||||
static void construct(Json& json, int32_t number)
|
static void construct(Json& json, T number)
|
||||||
{
|
{
|
||||||
json.destroy();
|
json.destroy();
|
||||||
json.m_type = Json::Type::Number;
|
json.m_type = Json::Type::Number;
|
||||||
json.m_value.number = static_cast<double>(number);
|
json.m_value.number = static_cast<double>(number);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Json>
|
template<typename Json, FloatingPoint T>
|
||||||
static void construct(Json& json, int64_t number)
|
static void construct(Json& json, T number)
|
||||||
{
|
{
|
||||||
json.destroy();
|
json.destroy();
|
||||||
json.m_type = Json::Type::Number;
|
json.m_type = Json::Type::Number;
|
||||||
json.m_value.number = static_cast<double>(number);
|
json.m_value.number = static_cast<double>(number);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Json>
|
|
||||||
static void construct(Json& json, uint32_t number)
|
|
||||||
{
|
|
||||||
json.destroy();
|
|
||||||
json.m_type = Json::Type::Number;
|
|
||||||
json.m_value.number = static_cast<double>(number);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Json>
|
|
||||||
static void construct(Json& json, size_t number)
|
|
||||||
{
|
|
||||||
json.destroy();
|
|
||||||
json.m_type = Json::Type::Number;
|
|
||||||
json.m_value.number = static_cast<double>(number);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Json>
|
|
||||||
static void construct(Json& json, double number)
|
|
||||||
{
|
|
||||||
json.destroy();
|
|
||||||
json.m_type = Json::Type::Number;
|
|
||||||
json.m_value.number = number;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Json>
|
template<typename Json>
|
||||||
static void construct(Json& json, const char* string)
|
static void construct(Json& json, const char* string)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user