美文网首页
任意结构体转为json String

任意结构体转为json String

作者: Lif68 | 来源:发表于2019-10-26 11:17 被阅读0次

分类:UE4 c++ 

标签:蓝图函数库

新建了一个c++写的蓝图函数类

在项目的build.cs里面添加"JsonUtilities"模块,添加后如下(添加到了"HeadMountedDisplay"后面)

using UnrealBuildTool;

public class UDPProject : ModuleRules

{

public UDPProject(ReadOnlyTargetRules Target) : base(Target)

{

PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay" , "JsonUtilities" });

}

}

.h头文件(定义了StructToJsonObjectString函数,有两个子函数)

//LFY 2019/9/30 https://blog.csdn.net/qq_29019373/article/details/93474641

#pragma once

#include "CoreMinimal.h"

#include "Kismet/BlueprintFunctionLibrary.h"

#include "UStruct2JsonObjString.generated.h"

UCLASS()

class UDPPROJECT_API UUStruct2JsonObjString : public UBlueprintFunctionLibrary

{

GENERATED_BODY()

UFUNCTION(BlueprintCallable, Category = "LFYBPLibrary", CustomThunk, meta = (CustomStructureParam = "Struct"))

static bool StructToJsonObjectString(FString& OutJsonString, const UStruct* Struct);

static bool Generic_StructToJsonObjectString(FString& OutJsonString, const UStruct* StructDefinition, const void* Struct);

//ReWrite 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 "UStruct2JsonObjString.h"

#include "JsonObjectConverter.h"

bool UUStruct2JsonObjString::StructToJsonObjectString(FString& OutJsonString, const UStruct* Struct)

{

check(0);

return false;

}

bool UUStruct2JsonObjString::Generic_StructToJsonObjectString(FString& OutJsonString, const UStruct* StructDefinition, const void* Struct)

{

FJsonObjectConverter::UStructToJsonObjectString(StructDefinition, Struct, OutJsonString, 0, 0);

return !OutJsonString.IsEmpty();

}

相关文章

网友评论

      本文标题:任意结构体转为json String

      本文链接:https://www.haomeiwen.com/subject/nebkpctx.html