使用通配符实现转换任意结构体成json格式string的蓝图函数
.h
UFUNCTION(BlueprintCallable, Category = "VictoryBPLibrary", CustomThunk, meta = (CustomStructureParam = "Struct"))
static bool StructToJsonObjectString(FString& OutJsonString,const UStruct* Struct);
static bool Generic_StructToJsonObjectString(FString& OutJsonString, const UStruct* StructDefinition,const void* Struct);
//重写exec
DECLARE_FUNCTION(execStructToJsonObjectString) {
P_GET_PROPERTY_REF(UStrProperty, OutJsonString);
Stack.StepCompiledIn<UStructProperty>(NULL);
void* InStruct = Stack.MostRecentPropertyAddress;
P_FINISH;
bool bSuccess = false;
UStructProperty* StructProp = Cast<UStructProperty>(Stack.MostRecentProperty);
if (StructProp && InStruct)
{
UScriptStruct* StructType = StructProp->Struct;
P_NATIVE_BEGIN;
bSuccess = Generic_StructToJsonObjectString(OutJsonString, StructType,InStruct);
P_NATIVE_END;
}
*(bool*)RESULT_PARAM = bSuccess;
}
.cpp
#include "JsonObjectConverter.h"
bool U::StructToJsonObjectString(FString& OutJsonString, const UStruct* Struct)
{
// We should never hit this! stubs to avoid NoExport on the class.
check(0);
return false;
}
bool U::Generic_StructToJsonObjectString(FString& OutJsonString, const UStruct* StructDefinition, const void* Struct)
{
FJsonObjectConverter::UStructToJsonObjectString(StructDefinition, Struct, OutJsonString,0,0);
return !OutJsonString.IsEmpty();
}
build.cs中加入 "JsonUtilities"模块