generated from freezernick/unreal-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJsonData.h
159 lines (135 loc) · 3.76 KB
/
JsonData.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright by Nick Lamprecht (2020-2023)
#pragma once
#include "CoreMinimal.h"
#if __has_include ("Launch/Resources/Version.h")
#include "Launch/Resources/Version.h"
#else
#include "Runtime/Launch/Resources/Version.h"
#endif
#include "HttpModule.h"
#include "Interfaces/IHttpResponse.h"
#include "Interfaces/IHttpRequest.h"
#include "Dom/JsonObject.h"
#include "Serialization/JsonSerializer.h"
#include "Serialization/JsonReader.h"
#include "JsonData.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnGetResult, const bool, bSuccess, class UJsonData*, JSON);
UCLASS()
class UJsonData final : public UObject
{
GENERATED_BODY()
private:
void OnReady(FHttpRequestPtr Request, const FHttpResponsePtr Response, const bool bWasSuccessful)
{
RemoveFromRoot();
if (!bWasSuccessful)
{
OnGetResult.Broadcast(false, this);
return;
}
if (!FromString(Response->GetContentAsString()))
OnGetResult.Broadcast(false, this);
OnGetResult.Broadcast(true, this);
}
void Reset()
{
if (Data.IsValid())
Data.Reset();
Data = MakeShareable(new FJsonObject());
}
public:
TSharedPtr<FJsonObject> Data;
UPROPERTY(BlueprintAssignable, Category = "JSON")
FOnGetResult OnGetResult;
UJsonData()
{
Reset();
};
FString GetString(const FString& key) const
{
FString string;
if (!Data->TryGetStringField(*key, string))
return "";
return string;
}
bool GetBool(const FString& key) const
{
bool boolean;
if (!Data->TryGetBoolField(*key, boolean))
return false;
return boolean;
}
int32 GetInt(const FString& key) const
{
int32 integer;
if (!Data->TryGetNumberField(*key, integer))
return 0;
return integer;
}
TArray<FString> GetStringArray(const FString& key)
{
TArray<FString> stringArray;
const TArray<TSharedPtr<FJsonValue>> *arrayPtr;
if (Data->TryGetArrayField(*key, arrayPtr))
for (int32 i=0; i < arrayPtr->Num(); i++)
stringArray.Add((*arrayPtr)[i]->AsString());
return stringArray;
}
TArray<int32> GetIntArray(const FString& key)
{
TArray<int32> array;
const TArray<TSharedPtr<FJsonValue>> *arrayPtr;
if (Data->TryGetArrayField(*key, arrayPtr))
for (int32 i = 0; i < arrayPtr->Num(); i++)
array.Add(static_cast<int32>((*arrayPtr)[i]->AsNumber()));
return array;
}
UJsonData* GetObject(const FString& key)
{
UJsonData* fieldObj = NULL;
const TSharedPtr<FJsonObject> *outPtr;
if (!Data->TryGetObjectField(*key, outPtr))
return NULL;
fieldObj = NewObject<UJsonData>();
fieldObj->Data = *outPtr;
return fieldObj;
}
TArray<UJsonData*> GetObjectArray(const FString& key)
{
TArray<UJsonData*> objectArray;
const TArray<TSharedPtr<FJsonValue>> *arrayPtr;
if (Data->TryGetArrayField(*key, arrayPtr))
{
for (int32 i = 0; i < arrayPtr->Num(); i++)
{
UJsonData* pageData = NewObject<UJsonData>();
pageData->Data = (*arrayPtr)[i]->AsObject();
objectArray.Add(pageData);
}
}
return objectArray;
}
bool FromString(const FString& dataString)
{
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(dataString);
bool isDeserialized = FJsonSerializer::Deserialize(JsonReader, Data);
if (!isDeserialized || !Data.IsValid())
return false;
return true;
}
static UJsonData* GetRequest(const FString& url)
{
UJsonData* dataObj = NewObject<UJsonData>();
#if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION >= 26 || ENGINE_MAJOR_VERSION >= 5
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
#else
TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest();
#endif
HttpRequest->SetVerb("GET");
HttpRequest->SetURL(url);
HttpRequest->OnProcessRequestComplete().BindUObject(dataObj, &UJsonData::OnReady);
dataObj->AddToRoot();
HttpRequest->ProcessRequest();
return dataObj;
}
};