Skip to content

Instantly share code, notes, and snippets.

@drawcode
Forked from hanzochang/AwesomeStruct.cpp
Created April 25, 2020 07:04
Show Gist options
  • Save drawcode/1b6014cda14c945b8a9d186eb026fcc8 to your computer and use it in GitHub Desktop.
Save drawcode/1b6014cda14c945b8a9d186eb026fcc8 to your computer and use it in GitHub Desktop.
[UE4(Unreal Engine 4)] Example for parsing json and creating struct instance from parsed json.
{
"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],
},
]
}
#include "ProcedualSpline.h"
#include "AwesomeStruct.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;
}
// 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
);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "YourProject.h"
#include "AwesomeStructGeneratorComponent.h"
// Sets default values for this component's properties
UAwesomeStructGeneratorComponent::UAwesomeStructGeneratorComponent()
{
}
TArray<FAwesomeStruct> UAwesomeStructGeneratorComponent::GenerateStructsFromJson(FString Path)
{
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonFullPath(Path));
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(AwesomeStructs, 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::GameContentDir();
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;
}
// 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 YOURPROJECT_API UAwesomeStructGeneratorComponent : public UActorComponent
{
GENERATED_BODY()
public:
UAwesomeStructGeneratorComponent();
private:
void GenerateStructsFromJson(TArray<FAwesomeStruct> &AwesomeStructs, TSharedPtr<FJsonObject> JsonObject);
FString JsonFullPath(FString Path);
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