11 #ifndef __NET_ConvertToType_H__
12 #define __NET_ConvertToType_H__
28 #include <type_traits>
30 #define NET_REQUIRE(_cond_) std::enable_if_t<_cond_>
39 return obj.serialize(writer);
45 return obj.serialize(data);
52 #define NET_DEFINE_INT_SERIALIZER(_T_) \
54 inline bool NETserialize<_T_>(UT_JSONWriter& writer, const _T_& obj) \
56 writer.jsonValue(obj); \
60 inline bool NETserialize<_T_>(const UT_JSONValue& data, _T_& item) \
63 bool res = data.import(v); \
64 item = static_cast<_T_>(v); \
70 #undef NET_DEFINE_INT_SERIALIZER
88 writer.jsonValue(static_cast<int32>(
obj));
115 return data.import(item);
181 template <
typename T,
template <
typename>
class ARRAY>
185 for (
auto&& item : items)
186 NETserialize<T>(
writer, item);
187 writer.jsonEndArray();
191 template <
typename T,
template <
typename>
class ARRAY>
197 if (values ==
nullptr)
204 if (value ==
nullptr)
206 NETserialize<T>(*
value, items[i]);
211 #define NET_DEFINE_ARRAY_SERIALIZER(_ARRAY_) \
212 template <typename T> \
213 inline bool NETserialize(UT_JSONWriter& writer, const _ARRAY_<typename T::value_type>& items) \
215 return NETserializeArray<typename T::value_type, _ARRAY_>(writer, items); \
217 template <typename T> \
218 inline bool NETserialize(const UT_JSONValue& data, _ARRAY_<typename T::value_type>& items) \
220 return NETserializeArray<typename T::value_type, _ARRAY_>(data, items); \
224 #undef NET_DEFINE_ARRAY_SERIALIZER
229 return writer.jsonStringArray(items);
235 return data.import(items);
245 writer.jsonValue(
value);
275 writer.jsonValue(
v.get<
int64>());
277 writer.jsonValue(
v.get<
float>());
281 writer.jsonValue(
v.get<
bool>());
310 v = data.getStringHolder();
342 writer.jsonBeginArray();
344 for (
auto&& k : variants)
350 writer.jsonEndArray();
362 for (
int i = 0; i < arr->
entries(); i++)
372 variants.append(value);
385 writer.jsonBeginMap();
387 for (
auto&&
v : variants)
392 writer.jsonKeyToken(
v.first);
408 for (
auto&&
v : *json)
410 if (
v.second ==
nullptr)
417 variants[
v.first] =
value;
459 namespace NET_ConvertToType
465 typedef std::remove_const_t<std::remove_reference_t<T>>
type;
473 template <
typename Tto,
typename Enable =
void>
479 UT_ASSERT(!
"This code should never be entered. You have not setup a proper cast for this type.");
484 template <
typename Tto>
486 std::enable_if_t<std::is_same<UT_StringHolder,
487 NET_remove_cref_t<Tto>>::value>>
493 template <
typename Tto>
495 std::enable_if_t<std::is_same<UT_StringArray,
496 NET_remove_cref_t<Tto>>::value>>
515 for (
auto &&value : values)
524 template <
typename Tto>
527 std::enable_if_t<std::is_same<UT_IntArray, NET_remove_cref_t<Tto>>::value>>
536 for (
auto &&
value : values)
546 template <
typename Tto>
548 std::enable_if_t<std::is_integral<Tto>::value &&
549 !std::is_same<Tto, bool>::value>>
556 return source.
toInt();
560 template <
typename Tto>
561 class convertImpl<Tto, std::enable_if_t<std::is_floating_point<Tto>::value>>
572 template <
typename Tto>
573 class convertImpl<Tto, std::enable_if_t<std::is_same<bool, Tto>::value>>
580 return source.
toInt();
583 return low_source ==
"true";
589 template <
typename Tto,
typename Tfrom>
596 #endif // __NET_ConvertToType_H__
static Tto cast(const UT_StringHolder &source)
bool NETserialize< UT_String >(UT_JSONWriter &writer, const UT_String &obj)
bool NETserialize< UT_WorkBuffer >(UT_JSONWriter &writer, const UT_WorkBuffer &obj)
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
SYS_NO_DISCARD_RESULT UT_JSONValueArray * getArray() const
Get the array value (may return a NULL pointer)
bool parseValue(UT_JSONParser &parser, UT_IStream *is=0, bool record_source_offsets=false)
UT_JSONValueArray stores a list of UT_JSONValue objects.
GLint GLsizei const GLuint64 * values
typename NET_remove_cref< T >::type NET_remove_cref_t
GLsizei GLsizei GLchar * source
bool NETserialize< NET_EmptySerializer >(UT_JSONWriter &writer, const NET_EmptySerializer &)
bool NETserialize< UT_VariantMap >(UT_JSONWriter &writer, const UT_VariantMap &value)
bool NETserialize< bool >(UT_JSONWriter &writer, const bool &obj)
Tto convert(const Tfrom &source)
Class which writes ASCII or binary JSON streams.
bool NETserialize< UT_StringArray >(UT_JSONWriter &writer, const UT_StringArray &items)
bool NETserialize< UT_StringLit >(UT_JSONWriter &writer, const UT_StringLit &obj)
A utility class to do read-only operations on a subset of an existing string.
void setCapacity(exint newcapacity)
bool NETserialize< UT_Variant >(UT_JSONWriter &writer, const UT_Variant &v)
exint emplace_back(S &&...s)
SYS_FORCE_INLINE const char * buffer() const
GLint GLenum GLsizei GLint GLsizei const void * data
bool NETserialize< int64 >(UT_JSONWriter &writer, const int64 &obj)
bool NETserialize(UT_JSONWriter &writer, const T &obj)
#define NET_DEFINE_INT_SERIALIZER(_T_)
A null variant. A null variant holds no value.
SYS_FORCE_INLINE int64 entries() const
Return the number of entries in the array.
static Tto cast(const UT_StringHolder &source)
static Tto cast(const UT_StringHolder &source)
static Tto cast(const UT_StringHolder &source)
bool NETserialize< UT_JSONValue >(UT_JSONWriter &writer, const UT_JSONValue &value)
SYS_NO_DISCARD_RESULT UT_StringRef toLower() const
static Tto cast(const UT_StringHolder &source)
#define NET_DEFINE_ARRAY_SERIALIZER(_ARRAY_)
bool jsonBeginArray()
Begin a generic array object.
SYS_NO_DISCARD_RESULT Type getType() const
Get the type of data stored in the object.
Class to store JSON objects as C++ objects.
static Tto cast(const UT_StringHolder &source)
unsigned isInteger(int skip_spaces=0) const
Determine if string can be seen as a single integer number.
GLsizei const GLfloat * value
static Tto cast(const UT_StringHolder &source)
unsigned isFloat(int skip_spaces=0, int loose=0) const
Determine if string can be seen as a single floating point number.
std::remove_const_t< std::remove_reference_t< T > > type
SYS_FORCE_INLINE const UT_JSONValue * get(int64 i) const
Access a const entry by index.
bool NETserialize< UT_StringHolder >(UT_JSONWriter &writer, const UT_StringHolder &obj)
bool NETserialize< UT_VariantArray >(UT_JSONWriter &writer, const UT_VariantArray &value)
bool NETserialize< uint32 >(UT_JSONWriter &writer, const uint32 &obj)
bool NETserializeArray(UT_JSONWriter &writer, const ARRAY< T > &items)