sourcemod/extensions/json/JsonManager.h
ProjectSky 5af62b03b8 refactor: rename and update JSON extension components
- Renamed YYJSON to JSON throughout the codebase for consistency
- Updated file names and references, including YYJSONManager to JsonManager
- Added some new native functions
2025-11-08 20:25:29 +08:00

402 lines
20 KiB
C++
Executable File

#ifndef _INCLUDE_SM_JSON_JSONMANAGER_H_
#define _INCLUDE_SM_JSON_JSONMANAGER_H_
#include <IJsonManager.h>
#include <yyjson.h>
#include <random>
#include <memory>
#include <charconv>
/**
* @brief JSON value wrapper
*
* Wraps json mutable/immutable documents and values.
* Used as the primary data type for JSON operations.
*/
class JsonValue {
public:
JsonValue() = default;
~JsonValue() {
if (m_pDocument_mut.unique()) {
yyjson_mut_doc_free(m_pDocument_mut.get());
}
if (m_pDocument.unique()) {
yyjson_doc_free(m_pDocument.get());
}
}
JsonValue(const JsonValue&) = delete;
JsonValue& operator=(const JsonValue&) = delete;
void ResetObjectIterator() {
m_iterInitialized = false;
}
void ResetArrayIterator() {
m_iterInitialized = false;
m_arrayIndex = 0;
}
bool IsMutable() const {
return m_pDocument_mut != nullptr;
}
bool IsImmutable() const {
return m_pDocument != nullptr;
}
// Mutable document
std::shared_ptr<yyjson_mut_doc> m_pDocument_mut;
yyjson_mut_val* m_pVal_mut{ nullptr };
// Immutable document
std::shared_ptr<yyjson_doc> m_pDocument;
yyjson_val* m_pVal{ nullptr };
// Mutable document iterators
yyjson_mut_obj_iter m_iterObj;
yyjson_mut_arr_iter m_iterArr;
// Immutable document iterators
yyjson_obj_iter m_iterObjImm;
yyjson_arr_iter m_iterArrImm;
Handle_t m_handle{ BAD_HANDLE };
size_t m_arrayIndex{ 0 };
size_t m_readSize{ 0 };
bool m_iterInitialized{ false };
};
/**
* @brief Array iterator wrapper
*
* Wraps yyjson_arr_iter and yyjson_mut_arr_iter for array iteration.
*/
class JsonArrIter {
public:
JsonArrIter() = default;
~JsonArrIter() = default;
JsonArrIter(const JsonArrIter&) = delete;
JsonArrIter& operator=(const JsonArrIter&) = delete;
bool IsMutable() const {
return m_isMutable;
}
std::shared_ptr<yyjson_mut_doc> m_pDocument_mut;
std::shared_ptr<yyjson_doc> m_pDocument;
yyjson_mut_arr_iter m_iterMut;
yyjson_arr_iter m_iterImm;
Handle_t m_handle{ BAD_HANDLE };
bool m_isMutable{ false };
bool m_initialized{ false };
};
/**
* @brief Object iterator wrapper
*
* Wraps yyjson_obj_iter and yyjson_mut_obj_iter for object iteration.
*/
class JsonObjIter {
public:
JsonObjIter() = default;
~JsonObjIter() = default;
JsonObjIter(const JsonObjIter&) = delete;
JsonObjIter& operator=(const JsonObjIter&) = delete;
bool IsMutable() const {
return m_isMutable;
}
std::shared_ptr<yyjson_mut_doc> m_pDocument_mut;
std::shared_ptr<yyjson_doc> m_pDocument;
yyjson_mut_obj_iter m_iterMut;
yyjson_obj_iter m_iterImm;
void* m_currentKey{ nullptr };
Handle_t m_handle{ BAD_HANDLE };
bool m_isMutable{ false };
bool m_initialized{ false };
};
class JsonManager : public IJsonManager
{
public:
JsonManager();
~JsonManager();
public:
// ========== Document Operations ==========
virtual JsonValue* ParseJSON(const char* json_str, bool is_file, bool is_mutable,
yyjson_read_flag read_flg, char* error, size_t error_size) override;
virtual bool WriteToString(JsonValue* handle, char* buffer, size_t buffer_size,
yyjson_write_flag write_flg, size_t* out_size) override;
virtual bool WriteToFile(JsonValue* handle, const char* path, yyjson_write_flag write_flg,
char* error, size_t error_size) override;
virtual bool Equals(JsonValue* handle1, JsonValue* handle2) override;
virtual bool EqualsStr(JsonValue* handle, const char* str) override;
virtual JsonValue* DeepCopy(JsonValue* targetDoc, JsonValue* sourceValue) override;
virtual const char* GetTypeDesc(JsonValue* handle) override;
virtual size_t GetSerializedSize(JsonValue* handle, yyjson_write_flag write_flg) override;
virtual JsonValue* ToMutable(JsonValue* handle) override;
virtual JsonValue* ToImmutable(JsonValue* handle) override;
virtual yyjson_type GetType(JsonValue* handle) override;
virtual yyjson_subtype GetSubtype(JsonValue* handle) override;
virtual bool IsArray(JsonValue* handle) override;
virtual bool IsObject(JsonValue* handle) override;
virtual bool IsInt(JsonValue* handle) override;
virtual bool IsUint(JsonValue* handle) override;
virtual bool IsSint(JsonValue* handle) override;
virtual bool IsNum(JsonValue* handle) override;
virtual bool IsBool(JsonValue* handle) override;
virtual bool IsTrue(JsonValue* handle) override;
virtual bool IsFalse(JsonValue* handle) override;
virtual bool IsFloat(JsonValue* handle) override;
virtual bool IsStr(JsonValue* handle) override;
virtual bool IsNull(JsonValue* handle) override;
virtual bool IsCtn(JsonValue* handle) override;
virtual bool IsMutable(JsonValue* handle) override;
virtual bool IsImmutable(JsonValue* handle) override;
virtual size_t GetReadSize(JsonValue* handle) override;
// ========== Object Operations ==========
virtual JsonValue* ObjectInit() override;
virtual JsonValue* ObjectInitWithStrings(const char** pairs, size_t count) override;
virtual JsonValue* ObjectParseString(const char* str, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual JsonValue* ObjectParseFile(const char* path, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual size_t ObjectGetSize(JsonValue* handle) override;
virtual bool ObjectGetKey(JsonValue* handle, size_t index, const char** out_key) override;
virtual JsonValue* ObjectGetValueAt(JsonValue* handle, size_t index) override;
virtual JsonValue* ObjectGet(JsonValue* handle, const char* key) override;
virtual bool ObjectGetBool(JsonValue* handle, const char* key, bool* out_value) override;
virtual bool ObjectGetFloat(JsonValue* handle, const char* key, double* out_value) override;
virtual bool ObjectGetInt(JsonValue* handle, const char* key, int* out_value) override;
virtual bool ObjectGetInt64(JsonValue* handle, const char* key, std::variant<int64_t, uint64_t>* out_value) override;
virtual bool ObjectGetString(JsonValue* handle, const char* key, const char** out_str, size_t* out_len) override;
virtual bool ObjectIsNull(JsonValue* handle, const char* key, bool* out_is_null) override;
virtual bool ObjectHasKey(JsonValue* handle, const char* key, bool use_pointer) override;
virtual bool ObjectRenameKey(JsonValue* handle, const char* old_key, const char* new_key, bool allow_duplicate) override;
virtual bool ObjectSet(JsonValue* handle, const char* key, JsonValue* value) override;
virtual bool ObjectSetBool(JsonValue* handle, const char* key, bool value) override;
virtual bool ObjectSetFloat(JsonValue* handle, const char* key, double value) override;
virtual bool ObjectSetInt(JsonValue* handle, const char* key, int value) override;
virtual bool ObjectSetInt64(JsonValue* handle, const char* key, std::variant<int64_t, uint64_t> value) override;
virtual bool ObjectSetNull(JsonValue* handle, const char* key) override;
virtual bool ObjectSetString(JsonValue* handle, const char* key, const char* value) override;
virtual bool ObjectRemove(JsonValue* handle, const char* key) override;
virtual bool ObjectClear(JsonValue* handle) override;
virtual bool ObjectSort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override;
// ========== Array Operations ==========
virtual JsonValue* ArrayInit() override;
virtual JsonValue* ArrayInitWithStrings(const char** strings, size_t count) override;
virtual JsonValue* ArrayInitWithInt32(const int32_t* values, size_t count) override;
virtual JsonValue* ArrayInitWithInt64(const char** values, size_t count,
char* error, size_t error_size) override;
virtual JsonValue* ArrayInitWithBool(const bool* values, size_t count) override;
virtual JsonValue* ArrayInitWithFloat(const double* values, size_t count) override;
virtual JsonValue* ArrayParseString(const char* str, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual JsonValue* ArrayParseFile(const char* path, yyjson_read_flag read_flg,
char* error, size_t error_size) override;
virtual size_t ArrayGetSize(JsonValue* handle) override;
virtual JsonValue* ArrayGet(JsonValue* handle, size_t index) override;
virtual JsonValue* ArrayGetFirst(JsonValue* handle) override;
virtual JsonValue* ArrayGetLast(JsonValue* handle) override;
virtual bool ArrayGetBool(JsonValue* handle, size_t index, bool* out_value) override;
virtual bool ArrayGetFloat(JsonValue* handle, size_t index, double* out_value) override;
virtual bool ArrayGetInt(JsonValue* handle, size_t index, int* out_value) override;
virtual bool ArrayGetInt64(JsonValue* handle, size_t index, std::variant<int64_t, uint64_t>* out_value) override;
virtual bool ArrayGetString(JsonValue* handle, size_t index, const char** out_str, size_t* out_len) override;
virtual bool ArrayIsNull(JsonValue* handle, size_t index) override;
virtual bool ArrayReplace(JsonValue* handle, size_t index, JsonValue* value) override;
virtual bool ArrayReplaceBool(JsonValue* handle, size_t index, bool value) override;
virtual bool ArrayReplaceFloat(JsonValue* handle, size_t index, double value) override;
virtual bool ArrayReplaceInt(JsonValue* handle, size_t index, int value) override;
virtual bool ArrayReplaceInt64(JsonValue* handle, size_t index, std::variant<int64_t, uint64_t> value) override;
virtual bool ArrayReplaceNull(JsonValue* handle, size_t index) override;
virtual bool ArrayReplaceString(JsonValue* handle, size_t index, const char* value) override;
virtual bool ArrayAppend(JsonValue* handle, JsonValue* value) override;
virtual bool ArrayAppendBool(JsonValue* handle, bool value) override;
virtual bool ArrayAppendFloat(JsonValue* handle, double value) override;
virtual bool ArrayAppendInt(JsonValue* handle, int value) override;
virtual bool ArrayAppendInt64(JsonValue* handle, std::variant<int64_t, uint64_t> value) override;
virtual bool ArrayAppendNull(JsonValue* handle) override;
virtual bool ArrayAppendString(JsonValue* handle, const char* value) override;
virtual bool ArrayInsert(JsonValue* handle, size_t index, JsonValue* value) override;
virtual bool ArrayInsertBool(JsonValue* handle, size_t index, bool value) override;
virtual bool ArrayInsertInt(JsonValue* handle, size_t index, int value) override;
virtual bool ArrayInsertInt64(JsonValue* handle, size_t index, std::variant<int64_t, uint64_t> value) override;
virtual bool ArrayInsertFloat(JsonValue* handle, size_t index, double value) override;
virtual bool ArrayInsertString(JsonValue* handle, size_t index, const char* value) override;
virtual bool ArrayInsertNull(JsonValue* handle, size_t index) override;
virtual bool ArrayPrepend(JsonValue* handle, JsonValue* value) override;
virtual bool ArrayPrependBool(JsonValue* handle, bool value) override;
virtual bool ArrayPrependInt(JsonValue* handle, int value) override;
virtual bool ArrayPrependInt64(JsonValue* handle, std::variant<int64_t, uint64_t> value) override;
virtual bool ArrayPrependFloat(JsonValue* handle, double value) override;
virtual bool ArrayPrependString(JsonValue* handle, const char* value) override;
virtual bool ArrayPrependNull(JsonValue* handle) override;
virtual bool ArrayRemove(JsonValue* handle, size_t index) override;
virtual bool ArrayRemoveFirst(JsonValue* handle) override;
virtual bool ArrayRemoveLast(JsonValue* handle) override;
virtual bool ArrayRemoveRange(JsonValue* handle, size_t start_index, size_t end_index) override;
virtual bool ArrayClear(JsonValue* handle) override;
virtual int ArrayIndexOfBool(JsonValue* handle, bool search_value) override;
virtual int ArrayIndexOfString(JsonValue* handle, const char* search_value) override;
virtual int ArrayIndexOfInt(JsonValue* handle, int search_value) override;
virtual int ArrayIndexOfInt64(JsonValue* handle, int64_t search_value) override;
virtual int ArrayIndexOfUint64(JsonValue* handle, uint64_t search_value) override;
virtual int ArrayIndexOfFloat(JsonValue* handle, double search_value) override;
virtual bool ArraySort(JsonValue* handle, JSON_SORT_ORDER sort_mode) override;
// ========== Value Operations ==========
virtual JsonValue* Pack(const char* format, IPackParamProvider* param_provider, char* error, size_t error_size) override;
virtual JsonValue* CreateBool(bool value) override;
virtual JsonValue* CreateFloat(double value) override;
virtual JsonValue* CreateInt(int value) override;
virtual JsonValue* CreateInt64(std::variant<int64_t, uint64_t> value) override;
virtual JsonValue* CreateNull() override;
virtual JsonValue* CreateString(const char* value) override;
virtual bool GetBool(JsonValue* handle, bool* out_value) override;
virtual bool GetFloat(JsonValue* handle, double* out_value) override;
virtual bool GetInt(JsonValue* handle, int* out_value) override;
virtual bool GetInt64(JsonValue* handle, std::variant<int64_t, uint64_t>* out_value) override;
virtual bool GetString(JsonValue* handle, const char** out_str, size_t* out_len) override;
// ========== Pointer Operations ==========
virtual JsonValue* PtrGet(JsonValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrGetBool(JsonValue* handle, const char* path, bool* out_value, char* error, size_t error_size) override;
virtual bool PtrGetFloat(JsonValue* handle, const char* path, double* out_value, char* error, size_t error_size) override;
virtual bool PtrGetInt(JsonValue* handle, const char* path, int* out_value, char* error, size_t error_size) override;
virtual bool PtrGetInt64(JsonValue* handle, const char* path, std::variant<int64_t, uint64_t>* out_value, char* error, size_t error_size) override;
virtual bool PtrGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len, char* error, size_t error_size) override;
virtual bool PtrGetIsNull(JsonValue* handle, const char* path, bool* out_is_null, char* error, size_t error_size) override;
virtual bool PtrGetLength(JsonValue* handle, const char* path, size_t* out_len, char* error, size_t error_size) override;
virtual bool PtrSet(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override;
virtual bool PtrSetBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override;
virtual bool PtrSetFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override;
virtual bool PtrSetInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override;
virtual bool PtrSetInt64(JsonValue* handle, const char* path, std::variant<int64_t, uint64_t> value, char* error, size_t error_size) override;
virtual bool PtrSetString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override;
virtual bool PtrSetNull(JsonValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrAdd(JsonValue* handle, const char* path, JsonValue* value, char* error, size_t error_size) override;
virtual bool PtrAddBool(JsonValue* handle, const char* path, bool value, char* error, size_t error_size) override;
virtual bool PtrAddFloat(JsonValue* handle, const char* path, double value, char* error, size_t error_size) override;
virtual bool PtrAddInt(JsonValue* handle, const char* path, int value, char* error, size_t error_size) override;
virtual bool PtrAddInt64(JsonValue* handle, const char* path, std::variant<int64_t, uint64_t> value, char* error, size_t error_size) override;
virtual bool PtrAddString(JsonValue* handle, const char* path, const char* value, char* error, size_t error_size) override;
virtual bool PtrAddNull(JsonValue* handle, const char* path, char* error, size_t error_size) override;
virtual bool PtrRemove(JsonValue* handle, const char* path, char* error, size_t error_size) override;
virtual JsonValue* PtrTryGet(JsonValue* handle, const char* path) override;
virtual bool PtrTryGetBool(JsonValue* handle, const char* path, bool* out_value) override;
virtual bool PtrTryGetFloat(JsonValue* handle, const char* path, double* out_value) override;
virtual bool PtrTryGetInt(JsonValue* handle, const char* path, int* out_value) override;
virtual bool PtrTryGetInt64(JsonValue* handle, const char* path, std::variant<int64_t, uint64_t>* out_value) override;
virtual bool PtrTryGetString(JsonValue* handle, const char* path, const char** out_str, size_t* out_len) override;
// ========== Iterator Operations ==========
virtual bool ObjectForeachNext(JsonValue* handle, const char** out_key,
size_t* out_key_len, JsonValue** out_value) override;
virtual bool ArrayForeachNext(JsonValue* handle, size_t* out_index,
JsonValue** out_value) override;
virtual bool ObjectForeachKeyNext(JsonValue* handle, const char** out_key,
size_t* out_key_len) override;
virtual bool ArrayForeachIndexNext(JsonValue* handle, size_t* out_index) override;
// ========== Array Iterator Operations ==========
virtual JsonArrIter* ArrIterInit(JsonValue* handle) override;
virtual JsonArrIter* ArrIterWith(JsonValue* handle) override;
virtual JsonValue* ArrIterNext(JsonArrIter* iter) override;
virtual bool ArrIterHasNext(JsonArrIter* iter) override;
virtual size_t ArrIterGetIndex(JsonArrIter* iter) override;
virtual void* ArrIterRemove(JsonArrIter* iter) override;
// ========== Object Iterator Operations ==========
virtual JsonObjIter* ObjIterInit(JsonValue* handle) override;
virtual JsonObjIter* ObjIterWith(JsonValue* handle) override;
virtual void* ObjIterNext(JsonObjIter* iter) override;
virtual bool ObjIterHasNext(JsonObjIter* iter) override;
virtual JsonValue* ObjIterGetVal(JsonObjIter* iter, void* key) override;
virtual JsonValue* ObjIterGet(JsonObjIter* iter, const char* key) override;
virtual size_t ObjIterGetIndex(JsonObjIter* iter) override;
virtual void* ObjIterRemove(JsonObjIter* iter) override;
// ========== Iterator Release Operations ==========
virtual void ReleaseArrIter(JsonArrIter* iter) override;
virtual void ReleaseObjIter(JsonObjIter* iter) override;
// ========== Iterator Handle Type Operations ==========
virtual HandleType_t GetArrIterHandleType() override;
virtual HandleType_t GetObjIterHandleType() override;
virtual JsonArrIter* GetArrIterFromHandle(IPluginContext* pContext, Handle_t handle) override;
virtual JsonObjIter* GetObjIterFromHandle(IPluginContext* pContext, Handle_t handle) override;
// ========== Release Operations ==========
virtual void Release(JsonValue* value) override;
// ========== Handle Type Operations ==========
virtual HandleType_t GetHandleType() override;
// ========== Handle Operations ==========
virtual JsonValue* GetFromHandle(IPluginContext* pContext, Handle_t handle) override;
// ========== Number Read/Write Operations ==========
virtual JsonValue* ReadNumber(const char* dat, uint32_t read_flg = 0,
char* error = nullptr, size_t error_size = 0, size_t* out_consumed = nullptr) override;
virtual bool WriteNumber(JsonValue* handle, char* buffer, size_t buffer_size,
size_t* out_written = nullptr) override;
// ========== Floating-Point Format Operations ==========
virtual bool SetFpToFloat(JsonValue* handle, bool flt) override;
virtual bool SetFpToFixed(JsonValue* handle, int prec) override;
// ========== Direct Value Modification Operations ==========
virtual bool SetBool(JsonValue* handle, bool value) override;
virtual bool SetInt(JsonValue* handle, int value) override;
virtual bool SetInt64(JsonValue* handle, std::variant<int64_t, uint64_t> value) override;
virtual bool SetFloat(JsonValue* handle, double value) override;
virtual bool SetString(JsonValue* handle, const char* value) override;
virtual bool SetNull(JsonValue* handle) override;
virtual bool ParseInt64Variant(const char* value, std::variant<int64_t, uint64_t>* out_value,
char* error = nullptr, size_t error_size = 0) override;
private:
std::random_device m_randomDevice;
std::mt19937 m_randomGenerator;
// Helper methods
static std::unique_ptr<JsonValue> CreateWrapper();
static std::shared_ptr<yyjson_mut_doc> WrapDocument(yyjson_mut_doc* doc);
static std::shared_ptr<yyjson_mut_doc> CopyDocument(yyjson_doc* doc);
static std::shared_ptr<yyjson_mut_doc> CreateDocument();
static std::shared_ptr<yyjson_doc> WrapImmutableDocument(yyjson_doc* doc);
// Pack helper methods
static const char* SkipSeparators(const char* ptr);
static void SetPackError(char* error, size_t error_size, const char* fmt, ...);
static yyjson_mut_val* PackImpl(yyjson_mut_doc* doc, const char* format,
IPackParamProvider* provider, char* error,
size_t error_size, const char** out_end_ptr);
// PtrTryGet helper methods
struct PtrGetValueResult {
yyjson_mut_val* mut_val{ nullptr };
yyjson_val* imm_val{ nullptr };
bool success{ false };
};
static PtrGetValueResult PtrGetValueInternal(JsonValue* handle, const char* path);
};
#endif // _INCLUDE_SM_JSON_JSONMANAGER_H_