1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Compare commits

..

2 Commits

Author SHA1 Message Date
Sandu Liviu Catalin
52cfa235be Update JSON.hpp
Some checks reported errors
continuous-integration/drone/push Build was killed
2022-10-02 21:47:20 +03:00
Sandu Liviu Catalin
ec7f1183d8 Basic hooking into json parsing.
Also allow customization of the fallback meta-method.
2022-10-02 21:24:12 +03:00
2 changed files with 96 additions and 5 deletions

View File

@ -2,6 +2,7 @@
#include "Library/JSON.hpp"
// ------------------------------------------------------------------------------------------------
#include <sajson.h>
#include <sqratConst.h>
// ------------------------------------------------------------------------------------------------
@ -180,6 +181,23 @@ CtxJSON & CtxJSON::CloseArray()
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::ReopenArray()
{
// If the last character is a comma then remove it
if (mOutput.back() == ',')
{
mOutput.pop_back();
}
// If the last character is the array-end character then replace it with a comma
if (mOutput.back() == ']')
{
mOutput.back() = ',';
}
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::OpenObject()
{
@ -212,6 +230,23 @@ CtxJSON & CtxJSON::CloseObject()
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::ReopenObject()
{
// If the last character is a comma then remove it
if (mOutput.back() == ',')
{
mOutput.pop_back();
}
// If the last character is the object-end character then replace it with a comma
if (mOutput.back() == '}')
{
mOutput.back() = ',';
}
// Allow chaining
return *this;
}
// ------------------------------------------------------------------------------------------------
CtxJSON & CtxJSON::MakeKey()
{
@ -225,6 +260,11 @@ CtxJSON & CtxJSON::MakeKey()
{
mOutput.push_back(':');
}
// Allow the hook to react
if (mKeyHook)
{
mKeyHook(*this);
}
// Allow chaining
return *this;
}
@ -467,7 +507,7 @@ SQRESULT CtxJSON::SerializeTable(HSQUIRRELVM vm, SQInteger idx) // NOLINT(misc-n
// ------------------------------------------------------------------------------------------------
SQRESULT CtxJSON::SerializeInstance(HSQUIRRELVM vm, SQInteger idx)
{
sq_pushstring(vm, _SC("_tojson"), 7);
sq_pushstring(vm, mMetaMethod.c_str(), static_cast< SQInteger >(mMetaMethod.size()));
// Attempt to retrieve the meta-method from the instance
if(SQRESULT r = sq_get(vm, idx); SQ_FAILED(r))
{
@ -629,6 +669,8 @@ void CtxJSON::PushString(const SQChar * str)
mOutput.append(str);
mOutput.push_back('"');
mOutput.push_back(',');
// Allow the hook to know
mString.assign(str);
}
// ------------------------------------------------------------------------------------------------
@ -638,6 +680,8 @@ void CtxJSON::PushString(const SQChar * str, size_t length)
mOutput.append(str, length);
mOutput.push_back('"');
mOutput.push_back(',');
// Allow the hook to know
mString.assign(str, length);
}
// ------------------------------------------------------------------------------------------------
@ -678,6 +722,7 @@ void Register_JSON(HSQUIRRELVM vm)
// Constructors
.Ctor()
.Ctor< bool >()
.Ctor< bool, StackStrF & >()
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqCtxJSON::Fn)
// Properties

View File

@ -5,12 +5,13 @@
#include "Library/IO/Buffer.hpp"
// ------------------------------------------------------------------------------------------------
#include <sajson.h>
#include <functional>
// ------------------------------------------------------------------------------------------------
#include <fmt/args.h>
#include <fmt/format.h>
#include <fmt/xchar.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -35,20 +36,55 @@ struct CtxJSON
*/
uint32_t mDepth{0};
/* --------------------------------------------------------------------------------------------
* The meta-method name to use on objects.
*/
String mMetaMethod{"_tojson"};
/* --------------------------------------------------------------------------------------------
* Last pushed string value. Can be used to heck for key name in the hook.
*/
String mString{};
/* --------------------------------------------------------------------------------------------
* Internal utility used to monitor the existence of certain keys to allow overloading.
*/
std::function< void(CtxJSON&) > mKeyHook{};
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
CtxJSON() noexcept = default;
CtxJSON() = default;
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
explicit CtxJSON(bool ooa) noexcept
explicit CtxJSON(bool ooa)
: CtxJSON()
{
mObjectOverArray = ooa;
}
/* --------------------------------------------------------------------------------------------
* Explicit constructor.
*/
CtxJSON(bool ooa, StackStrF & mmname)
: CtxJSON()
{
mObjectOverArray = ooa;
// Allow custom metamethod names
mMetaMethod.assign(mmname.mPtr, static_cast< size_t >(mmname.mLen));
}
/* --------------------------------------------------------------------------------------------
* Internal constructor.
*/
explicit CtxJSON(std::function< void(CtxJSON&) > && kh)
: CtxJSON()
{
mKeyHook = std::move(kh);
}
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
@ -135,6 +171,11 @@ struct CtxJSON
*/
CtxJSON & CloseArray();
/* --------------------------------------------------------------------------------------------
* Resume writing an array.
*/
CtxJSON & ReopenArray();
/* --------------------------------------------------------------------------------------------
* Begin writing an object.
*/
@ -145,6 +186,11 @@ struct CtxJSON
*/
CtxJSON & CloseObject();
/* --------------------------------------------------------------------------------------------
* Resume writing an object.
*/
CtxJSON & ReopenObject();
/* --------------------------------------------------------------------------------------------
* Begin writing a key value.
*/