UStructToJsonObjectString轉藍圖函數

使用通配符實現轉換任意結構體成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"模塊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章