From a788e059a59fd86306c759d76135934013972c94 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 18 Sep 2022 15:02:39 +0300 Subject: [PATCH] Implement `_tojson` meta-method for base types. --- module/Base/AABB.cpp | 17 +++++++++++++++++ module/Base/AABB.hpp | 5 +++++ module/Base/Circle.cpp | 15 +++++++++++++++ module/Base/Circle.hpp | 5 +++++ module/Base/Color3.cpp | 15 +++++++++++++++ module/Base/Color3.hpp | 5 +++++ module/Base/Color4.cpp | 15 +++++++++++++++ module/Base/Color4.hpp | 5 +++++ module/Base/Quaternion.cpp | 15 +++++++++++++++ module/Base/Quaternion.hpp | 5 +++++ module/Base/Shared.hpp | 3 +++ module/Base/Sphere.cpp | 15 +++++++++++++++ module/Base/Sphere.hpp | 5 +++++ module/Base/Vector2.cpp | 15 +++++++++++++++ module/Base/Vector2.hpp | 5 +++++ module/Base/Vector2i.cpp | 15 +++++++++++++++ module/Base/Vector2i.hpp | 5 +++++ module/Base/Vector3.cpp | 15 +++++++++++++++ module/Base/Vector3.hpp | 5 +++++ module/Base/Vector4.cpp | 15 +++++++++++++++ module/Base/Vector4.hpp | 5 +++++ 21 files changed, 205 insertions(+) diff --git a/module/Base/AABB.cpp b/module/Base/AABB.cpp index bf0259f6..5bdf1278 100644 --- a/module/Base/AABB.cpp +++ b/module/Base/AABB.cpp @@ -4,6 +4,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" // ------------------------------------------------------------------------------------------------ namespace SqMod { @@ -300,6 +301,21 @@ String AABB::ToString() const return fmt::format("{},{},{},{},{},{}", min.x, min.y, min.z, max.x, max.y, max.z); } +// ------------------------------------------------------------------------------------------------ +void AABB::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{min:{{x:{},y:{},z:{}}},max:{{x:{},y:{},z:{}}},", + min.x, min.y, min.z, max.x, max.y, max.z); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{min:[{},{},{}],max:[{},{},{}]}},", + min.x, min.y, min.z, max.x, max.y, max.z); + } +} + // ------------------------------------------------------------------------------------------------ void AABB::SetStr(SQChar delim, StackStrF & values) { @@ -793,6 +809,7 @@ void Register_AABB(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &AABB::ToString) + .Func(_SC("_tojson"), &AABB::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) diff --git a/module/Base/AABB.hpp b/module/Base/AABB.hpp index 32dfc0e6..59137c60 100644 --- a/module/Base/AABB.hpp +++ b/module/Base/AABB.hpp @@ -304,6 +304,11 @@ struct AABB */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set the values extracted from the specified string using the specified delimiter. */ diff --git a/module/Base/Circle.cpp b/module/Base/Circle.cpp index 88be8d5d..197826f4 100644 --- a/module/Base/Circle.cpp +++ b/module/Base/Circle.cpp @@ -3,6 +3,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -360,6 +361,19 @@ String Circle::ToString() const return fmt::format("{},{},{}", pos.x, pos.y, rad); } +// ------------------------------------------------------------------------------------------------ +void Circle::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},r:{}}},", pos.x, pos.y, rad); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", pos.x, pos.y, rad); + } +} + // ------------------------------------------------------------------------------------------------ void Circle::SetRadius(Value nr) { @@ -552,6 +566,7 @@ void Register_Circle(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Circle::ToString) + .Func(_SC("_toJSON"), &Circle::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >) diff --git a/module/Base/Circle.hpp b/module/Base/Circle.hpp index 35048048..b4558092 100644 --- a/module/Base/Circle.hpp +++ b/module/Base/Circle.hpp @@ -341,6 +341,11 @@ struct Circle */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set the specified radius. */ diff --git a/module/Base/Color3.cpp b/module/Base/Color3.cpp index f6ca2903..59a3394d 100644 --- a/module/Base/Color3.cpp +++ b/module/Base/Color3.cpp @@ -4,6 +4,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -481,6 +482,19 @@ String Color3::ToString() const return fmt::format("{},{},{}", r, g, b); } +// ------------------------------------------------------------------------------------------------ +void Color3::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{r:{},g:{},b:{}}},", r, g, b); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", r, g, b); + } +} + // ------------------------------------------------------------------------------------------------ void Color3::SetScalar(Value ns) { @@ -741,6 +755,7 @@ void Register_Color3(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Color3::ToString) + .Func(_SC("_tojson"), &Color3::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >) diff --git a/module/Base/Color3.hpp b/module/Base/Color3.hpp index b63db518..4d942b9e 100644 --- a/module/Base/Color3.hpp +++ b/module/Base/Color3.hpp @@ -405,6 +405,11 @@ struct Color3 */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set all components to the specified scalar value. */ diff --git a/module/Base/Color4.cpp b/module/Base/Color4.cpp index 542eaa9e..b4c84240 100644 --- a/module/Base/Color4.cpp +++ b/module/Base/Color4.cpp @@ -4,6 +4,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -506,6 +507,19 @@ String Color4::ToString() const return fmt::format("{},{},{},{}", r, g, b, a); } +// ------------------------------------------------------------------------------------------------ +void Color4::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{r:{},g:{},b:{},a:{}}},", r, g, b, a); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", r, g, b, a); + } +} + // ------------------------------------------------------------------------------------------------ void Color4::SetScalar(Value ns) { @@ -781,6 +795,7 @@ void Register_Color4(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Color4::ToString) + .Func(_SC("_tojson"), &Color4::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >) diff --git a/module/Base/Color4.hpp b/module/Base/Color4.hpp index df48dfff..7827f6d2 100644 --- a/module/Base/Color4.hpp +++ b/module/Base/Color4.hpp @@ -405,6 +405,11 @@ struct Color4 */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set all components to the specified scalar value. */ diff --git a/module/Base/Quaternion.cpp b/module/Base/Quaternion.cpp index 968b0fe3..c41a55a3 100644 --- a/module/Base/Quaternion.cpp +++ b/module/Base/Quaternion.cpp @@ -5,6 +5,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -349,6 +350,19 @@ String Quaternion::ToString() const return fmt::format("{},{},{},{}", x, y, z, w); } +// ------------------------------------------------------------------------------------------------ +void Quaternion::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},w:{}}},", x, y, z, w); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", x, y, z, w); + } +} + // ------------------------------------------------------------------------------------------------ void Quaternion::SetScalar(Value ns) { @@ -775,6 +789,7 @@ void Register_Quaternion(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Quaternion::ToString) + .Func(_SC("_tojson"), &Quaternion::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >) diff --git a/module/Base/Quaternion.hpp b/module/Base/Quaternion.hpp index bbccd5de..f29c72be 100644 --- a/module/Base/Quaternion.hpp +++ b/module/Base/Quaternion.hpp @@ -296,6 +296,11 @@ struct Quaternion */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set all components to the specified scalar value. */ diff --git a/module/Base/Shared.hpp b/module/Base/Shared.hpp index 84d13567..8d64f1ac 100644 --- a/module/Base/Shared.hpp +++ b/module/Base/Shared.hpp @@ -6,6 +6,9 @@ // ------------------------------------------------------------------------------------------------ namespace SqMod { +// ------------------------------------------------------------------------------------------------ +struct CtxJSON; + /* ------------------------------------------------------------------------------------------------ * Helper constants used by the bas types. */ diff --git a/module/Base/Sphere.cpp b/module/Base/Sphere.cpp index f03a3fb3..48632462 100644 --- a/module/Base/Sphere.cpp +++ b/module/Base/Sphere.cpp @@ -3,6 +3,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -360,6 +361,19 @@ String Sphere::ToString() const return fmt::format("{},{},{},{}", pos.x, pos.y, pos.z, rad); } +// ------------------------------------------------------------------------------------------------ +void Sphere::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},r:{}}},", pos.x, pos.y, pos.z, rad); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", pos.x, pos.y, pos.z, rad); + } +} + // ------------------------------------------------------------------------------------------------ void Sphere::SetRadius(Value nr) { @@ -527,6 +541,7 @@ void Register_Sphere(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Sphere::ToString) + .Func(_SC("_tojson"), &Sphere::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >) diff --git a/module/Base/Sphere.hpp b/module/Base/Sphere.hpp index 992a6c3f..1c291344 100644 --- a/module/Base/Sphere.hpp +++ b/module/Base/Sphere.hpp @@ -341,6 +341,11 @@ struct Sphere */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set the specified radius. */ diff --git a/module/Base/Vector2.cpp b/module/Base/Vector2.cpp index 5b27fb8d..1ce66648 100644 --- a/module/Base/Vector2.cpp +++ b/module/Base/Vector2.cpp @@ -4,6 +4,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -295,6 +296,19 @@ String Vector2::ToString() const return fmt::format("{},{}", x, y); } +// ------------------------------------------------------------------------------------------------ +void Vector2::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{}}},", x, y); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{}],", x, y); + } +} + // ------------------------------------------------------------------------------------------------ void Vector2::SetScalar(Value ns) { @@ -428,6 +442,7 @@ void Register_Vector2(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Vector2::ToString) + .Func(_SC("_tojson"), &Vector2::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >) diff --git a/module/Base/Vector2.hpp b/module/Base/Vector2.hpp index f55f1502..8346853f 100644 --- a/module/Base/Vector2.hpp +++ b/module/Base/Vector2.hpp @@ -285,6 +285,11 @@ struct Vector2 */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set all components to the specified scalar value. */ diff --git a/module/Base/Vector2i.cpp b/module/Base/Vector2i.cpp index e173389d..de2550f1 100644 --- a/module/Base/Vector2i.cpp +++ b/module/Base/Vector2i.cpp @@ -4,6 +4,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -441,6 +442,19 @@ String Vector2i::ToString() const return fmt::format("{},{}", x, y); } +// ------------------------------------------------------------------------------------------------ +void Vector2i::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{}}},", x, y); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{}],", x, y); + } +} + // ------------------------------------------------------------------------------------------------ void Vector2i::SetScalar(Value ns) { @@ -574,6 +588,7 @@ void Register_Vector2i(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Vector2i::ToString) + .Func(_SC("_tojson"), &Vector2i::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >) diff --git a/module/Base/Vector2i.hpp b/module/Base/Vector2i.hpp index a5377b0b..0249f8a9 100644 --- a/module/Base/Vector2i.hpp +++ b/module/Base/Vector2i.hpp @@ -390,6 +390,11 @@ struct Vector2i */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set all components to the specified scalar value. */ diff --git a/module/Base/Vector3.cpp b/module/Base/Vector3.cpp index a58f9c7e..0e1a3642 100644 --- a/module/Base/Vector3.cpp +++ b/module/Base/Vector3.cpp @@ -5,6 +5,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -331,6 +332,19 @@ String Vector3::ToString() const return fmt::format("{},{},{}", x, y, z); } +// ------------------------------------------------------------------------------------------------ +void Vector3::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{}}},", x, y, z); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{}],", x, y, z); + } +} + // ------------------------------------------------------------------------------------------------ void Vector3::SetScalar(Value ns) { @@ -696,6 +710,7 @@ void Register_Vector3(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Vector3::ToString) + .Func(_SC("_tojson"), &Vector3::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >) diff --git a/module/Base/Vector3.hpp b/module/Base/Vector3.hpp index 9894380a..df27444d 100644 --- a/module/Base/Vector3.hpp +++ b/module/Base/Vector3.hpp @@ -297,6 +297,11 @@ struct Vector3 */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set all components to the specified scalar value. */ diff --git a/module/Base/Vector4.cpp b/module/Base/Vector4.cpp index a95fbb07..33cee22a 100644 --- a/module/Base/Vector4.cpp +++ b/module/Base/Vector4.cpp @@ -5,6 +5,7 @@ #include "Base/DynArg.hpp" #include "Core/Buffer.hpp" #include "Core/Utility.hpp" +#include "Library/JSON.hpp" #include "Library/Numeric/Random.hpp" // ------------------------------------------------------------------------------------------------ @@ -344,6 +345,19 @@ String Vector4::ToString() const return fmt::format("{},{},{},{}", x, y, z, w); } +// ------------------------------------------------------------------------------------------------ +void Vector4::ToJSON(CtxJSON & ctx) const +{ + if (ctx.mObjectOverArray) + { + fmt::format_to(std::back_inserter(ctx.mOutput), "{{x:{},y:{},z:{},w:{}}},", x, y, z, w); + } + else + { + fmt::format_to(std::back_inserter(ctx.mOutput), "[{},{},{},{}],", x, y, z, w); + } +} + // ------------------------------------------------------------------------------------------------ void Vector4::SetScalar(Value ns) { @@ -526,6 +540,7 @@ void Register_Vector4(HSQUIRRELVM vm) .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >) .SquirrelFunc(_SC("_typename"), &Typename::Fn) .Func(_SC("_tostring"), &Vector4::ToString) + .Func(_SC("_tojson"), &Vector4::ToJSON) // Meta-methods .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >) .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >) diff --git a/module/Base/Vector4.hpp b/module/Base/Vector4.hpp index f82ef3b5..fe74975a 100644 --- a/module/Base/Vector4.hpp +++ b/module/Base/Vector4.hpp @@ -295,6 +295,11 @@ struct Vector4 */ SQMOD_NODISCARD String ToString() const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to convert an instance of this type to a JSON string. + */ + void ToJSON(CtxJSON & ctx) const; + /* -------------------------------------------------------------------------------------------- * Set all components to the specified scalar value. */