Skip to content

Instantly share code, notes, and snippets.

@PhroZenOne
Last active December 28, 2020 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhroZenOne/e860e6dbbca458285acacec65cc61f84 to your computer and use it in GitHub Desktop.
Save PhroZenOne/e860e6dbbca458285acacec65cc61f84 to your computer and use it in GitHub Desktop.
UFUNCTION(BlueprintCallable, Category = "Valhalla|Server", CustomThunk, meta = (CustomStructureParam = "structToFill"))
static void JsonToStruct(const UProperty* structToFill, const FString& json);
DECLARE_FUNCTION(execJsonToStruct) {
Stack.MostRecentPropertyAddress = nullptr;
Stack.MostRecentProperty = nullptr;
Stack.StepCompiledIn<UStructProperty>(NULL);
void* structPtr = Stack.MostRecentPropertyAddress;
auto structProperty = Cast<UStructProperty>(Stack.MostRecentProperty);
Stack.StepCompiledIn<UStrProperty>(NULL);
void* strPtr = Stack.MostRecentPropertyAddress;
auto strProperty = Cast<UStrProperty>(Stack.MostRecentProperty);
P_FINISH;
TSharedPtr<FJsonObject> data = MakeShareable(new FJsonObject());
TSharedRef<TJsonReader<>> jsonReader = TJsonReaderFactory<>::Create(strProperty->GetPropertyValue(strPtr));
if (FJsonSerializer::Deserialize(jsonReader, data) && data.IsValid()) {
UScriptStruct* Struct = structProperty->Struct;
for (TFieldIterator<UProperty> It(Struct); It; ++It)
{
UProperty* Property = *It;
// This is the variable name if you need it
FString variableName = Property->GetName();
// Never assume ArrayDim is always 1
for (int32 ArrayIndex = 0; ArrayIndex < Property->ArrayDim; ArrayIndex++)
{
// This grabs the pointer to where the property value is stored
void* ValuePtr = Property->ContainerPtrToValuePtr<void>(structPtr, ArrayIndex);
FString vrtxSpaces = "_";
FString vrtxAString = "";
FString vrtxBString = "";
if (variableName.Split(vrtxSpaces, &vrtxAString, &vrtxBString, ESearchCase::CaseSensitive, ESearchDir::FromStart)) {
SetProperty(Property, ValuePtr, data, vrtxAString);
}
else
{
SetProperty(Property, ValuePtr, data, variableName);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment