-
-
Save adnoh/48a3f8615e9f499b21a2ef20e3882277 to your computer and use it in GitHub Desktop.
[UE4(Unreal Engine 4)] Example for parsing json and creating struct instance from parsed json.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"AwesomeStructs": | |
[ | |
{ | |
"AwesomeVector": [10,0,0], | |
"AwesomeBoolean": true, | |
"AwesomeFloat": 10.5, | |
"AwesomeInteger": 20, | |
"AwesomeRotator": [10,10,5] | |
}, | |
{ | |
"AwesomeVector": [10,0,0], | |
"AwesomeBoolean": true, | |
"AwesomeFloat": 10.5, | |
"AwesomeInteger": 20, | |
"AwesomeRotator": [10,10,5] | |
}, | |
{ | |
"AwesomeVector": [10,0,0], | |
"AwesomeBoolean": true, | |
"AwesomeFloat": 10.5, | |
"AwesomeInteger": 20, | |
"AwesomeRotator": [10,10,5] | |
}, | |
{ | |
"AwesomeVector": [10,0,0], | |
"AwesomeBoolean": true, | |
"AwesomeFloat": 10.5, | |
"AwesomeInteger": 20, | |
"AwesomeRotator": [10,10,5] | |
}, | |
{ | |
"AwesomeVector": [10,0,0], | |
"AwesomeBoolean": true, | |
"AwesomeFloat": 10.5, | |
"AwesomeInteger": 20, | |
"AwesomeRotator": [10,10,5] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "AwesomeStruct.h" | |
//#include "ProcedualSpline.h" | |
FAwesomeStruct FAwesomeStruct::BuildAwesomeStruct( | |
FVector AwesomeVector, | |
bool AwesomeBoolean, | |
float AwesomeFloat, | |
int32 AwesomeInteger, | |
FRotator AwesomeRotator | |
) | |
{ | |
FAwesomeStruct AwesomeStruct; | |
AwesomeStruct.AwesomeVector = AwesomeVector; | |
AwesomeStruct.AwesomeBoolean = AwesomeBoolean; | |
AwesomeStruct.AwesomeFloat = AwesomeFloat; | |
AwesomeStruct.AwesomeInteger = AwesomeInteger; | |
AwesomeStruct.AwesomeRotator = AwesomeRotator; | |
return AwesomeStruct; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#pragma once | |
#include "AwesomeStruct.generated.h" | |
USTRUCT(BlueprintType) | |
struct FAwesomeStruct | |
{ | |
GENERATED_BODY() | |
public: | |
UPROPERTY(EditAnyWhere, BluePrintReadWrite, Category = "Struct") | |
FVector AwesomeVector; | |
UPROPERTY(EditAnyWhere, BluePrintReadWrite, Category = "Struct") | |
bool AwesomeBoolean; | |
UPROPERTY(EditAnyWhere, BluePrintReadWrite, Category = "Struct") | |
float AwesomeFloat; | |
UPROPERTY(EditAnyWhere, BluePrintReadWrite, Category = "Struct") | |
int32 AwesomeInteger; | |
UPROPERTY(EditAnyWhere, BluePrintReadWrite, Category = "Struct") | |
FRotator AwesomeRotator; | |
public: | |
static FAwesomeStruct BuildAwesomeStruct( | |
FVector AwesomeVector, | |
bool AwesomeBoolean, | |
float AwesomeFloat, | |
int32 AwesomeInteger, | |
FRotator AwesomeRotator | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#include "AwesomeStructGeneratorComponent.h" | |
#include "../Public/AwesomeStructGeneratorComponent.h" | |
// Sets default values for this component's properties | |
UAwesomeStructGeneratorComponent::UAwesomeStructGeneratorComponent() | |
{ | |
} | |
TArray<FAwesomeStruct> UAwesomeStructGeneratorComponent::GenerateStructsFromJson(FString FullPathToFile) | |
{ | |
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonFullPath(FullPathToFile)); | |
//TSharedRef<TJsonReader<TCHAR>> JsonReader = FJsonStringReader::Create(JsonFullPath(FullPathToFile)); | |
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject); | |
TArray<FAwesomeStruct> AwesomeStruct; | |
if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid()) | |
{ | |
if (GEngine) { | |
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Deserialize")); | |
} | |
GenerateStructsFromJson(AwesomeStruct, JsonObject); | |
} | |
else { | |
if (GEngine) { | |
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Not Deserialize")); | |
} | |
} | |
return AwesomeStruct; | |
} | |
/* | |
** private | |
*/ | |
void UAwesomeStructGeneratorComponent::GenerateStructsFromJson( | |
TArray<FAwesomeStruct> &AwesomeStructs, | |
TSharedPtr<FJsonObject> JsonObject | |
) | |
{ | |
FVector PrevEndPoint = FVector{ 0,0,0 }; | |
TArray<TSharedPtr<FJsonValue>> objArray = JsonObject->GetArrayField(TEXT("AwesomeStructs")); | |
for (int32 i = 0; i < objArray.Num(); i++) | |
{ | |
TSharedPtr<FJsonValue> value = objArray[i]; | |
TSharedPtr<FJsonObject> json = value->AsObject(); | |
FVector AwesomeVector = ParseAsVector(json, FString("AwesomeVector")); | |
bool AwesomeBoolean = json->GetBoolField(TEXT("AwesomeBoolean")); | |
float AwesomeFloat = json->GetNumberField(TEXT("AwesomeFloat")); | |
int32 AwesomeInteger = json->GetNumberField(TEXT("AwesomeInteger")); | |
FRotator AwesomeRotator = ParseAsRotator(json, FString("AwesomeRotator")); | |
FAwesomeStruct AwesomeStruct = FAwesomeStruct::BuildAwesomeStruct( | |
AwesomeVector, AwesomeBoolean, AwesomeFloat, AwesomeInteger, AwesomeRotator | |
); | |
AwesomeStructs.Push(AwesomeStruct); | |
} | |
} | |
FString UAwesomeStructGeneratorComponent::JsonFullPath(FString Path) | |
{ | |
// Games/Content/****.json | |
FString FullPath = FPaths::ProjectDir(); | |
FullPath += Path; | |
FString JsonStr; | |
FFileHelper::LoadFileToString(JsonStr, *FullPath); | |
return JsonStr; | |
} | |
FVector UAwesomeStructGeneratorComponent::ParseAsVector(TSharedPtr<FJsonObject> json, FString KeyName) | |
{ | |
TArray<FString> ArrayJson; | |
json->TryGetStringArrayField(*KeyName, ArrayJson); | |
FVector Vector = FVector{ FCString::Atof(*ArrayJson[0]), | |
FCString::Atof(*ArrayJson[1]), | |
FCString::Atof(*ArrayJson[2]) }; | |
return Vector; | |
} | |
FRotator UAwesomeStructGeneratorComponent::ParseAsRotator(TSharedPtr<FJsonObject> json, FString KeyName) | |
{ | |
TArray<FString> ArrayJson; | |
json->TryGetStringArrayField(*KeyName, ArrayJson); | |
FRotator Rotator = FRotator{ FCString::Atof(*ArrayJson[0]), | |
FCString::Atof(*ArrayJson[1]), | |
FCString::Atof(*ArrayJson[2]) }; | |
return Rotator; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#pragma once | |
#include "Components/ActorComponent.h" | |
#include "AwesomeStruct.h" | |
#include "AwesomeStructGeneratorComponent.generated.h" | |
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent)) | |
class SOKOBAN_API UAwesomeStructGeneratorComponent : public UActorComponent | |
{ | |
GENERATED_BODY() | |
public: | |
UAwesomeStructGeneratorComponent(); | |
//Read in a JSON File below ProjectDir relativ path and return an array of awesomeStructs | |
UFUNCTION(BlueprintCallable, Category = "AwesomeStruct") | |
TArray<FAwesomeStruct> GenerateStructsFromJson(FString FullPathToFile); | |
//Read in a File below ProjectDir relativ path and return a string of it | |
UFUNCTION(BlueprintCallable, Category = "AwesomeStruct") | |
FString JsonFullPath(FString Path); | |
//Thanks epic for making my life hard - the BP getProjectDirectory does return ABSOLUT which does not work with JsonReader - so thanks for this ugly workaround needed | |
UFUNCTION(BlueprintPure, Category = "AwesomeStruct") | |
FString getProjectDirectoryCpp() { return FPaths::ProjectDir(); }; | |
private: | |
void GenerateStructsFromJson(TArray<FAwesomeStruct> &AwesomeStructs, TSharedPtr<FJsonObject> JsonObject); | |
FVector ParseAsVector(TSharedPtr<FJsonObject> json, FString KeyName); | |
FRotator ParseAsRotator(TSharedPtr<FJsonObject> json, FString KeyName); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment