MetaSoul

Loading...
Searching...
No Matches
ESJsonLibrary.h
Go to the documentation of this file.
1// Modifications Copyright 2022-current EmoShape. All Rights Reserved
2
3
4// Copyright 2016 Vladimir Alyamkin. All Rights Reserved.
5
6#pragma once
7#include "ESIOJConvert.h"
8#include "ESIOTypes.h"
9#include "ESJsonObject.h"
10#include "ESJsonValue.h"
11#include "ESJsonLibrary.generated.h"
12
13 class UESRequestJSON;
14 class UESIOJConvert;
15 class UESJsonObject;
16 class UESJsonValue;
17 DECLARE_DYNAMIC_DELEGATE_OneParam(FEmoShapeJsonCallDelegate, UESRequestJSON*, Request);
18
19USTRUCT()
20struct FEmoShapeCallResponse
21{
22 GENERATED_USTRUCT_BODY()
23
24 UPROPERTY()
25 UESRequestJSON* Request;
26
27 UPROPERTY()
28 UObject* WorldContextObject;
29
30 UPROPERTY()
31 FEmoShapeJsonCallDelegate Callback;
32
33 FDelegateHandle CompleteDelegateHandle;
34 FDelegateHandle FailDelegateHandle;
35
36 FEmoShapeCallResponse()
37 : Request(nullptr)
38 , WorldContextObject(nullptr)
39 {
40 }
41
42};
43
44UCLASS()
45class EMOSHAPESUBSYSTEM_API UESJsonLibrary : public UBlueprintFunctionLibrary
46{
47 GENERATED_BODY()
48
49 public:
51 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Utility")
52 static FString PercentEncode(const FString& Source);
53
60 UFUNCTION(BlueprintPure, Category = "EmoShapeJson|Utility", meta = (DisplayName = "Base64 Encode (String)"))
61 static FString Base64Encode(const FString& Source);
62
69 UFUNCTION(BlueprintPure, Category = "EmoShapeJson|Utility", meta = (DisplayName = "Base64 Encode (Bytes)"))
70 static FString Base64EncodeBytes(const TArray<uint8>& Source);
71
79 UFUNCTION(BlueprintPure, Category = "EmoShapeJson|Utility", meta = (DisplayName = "Base64 Decode (To String)"))
80 static bool Base64Decode(const FString& Source, FString& Dest);
81
82
90 UFUNCTION(BlueprintPure, Category = "EmoShapeJson|Utility", meta = (DisplayName = "Base64 Decode (To Bytes)"))
91 static bool Base64DecodeBytes(const FString& Source, TArray<uint8>& Dest);
92
94 // Easy URL processing
95
102 UFUNCTION(BlueprintPure, Category = "EmoShapeJson|Utility")
103 static bool StringToJsonValueArray(const FString& JsonString, TArray<UESJsonValue*>& OutJsonValueArray);
104
111 UFUNCTION(BlueprintPure, Category = "EmoShapeSocketIOFunctions", CustomThunk, meta = (CustomStructureParam = "AnyStruct"))
112 static UESJsonObject* StructToJsonObject(TFieldPath<FProperty> AnyStruct);
113
114 //Convert property into c++ accessible form
115 DECLARE_FUNCTION(execStructToJsonObject)
116 {
117 //Get properties and pointers from stack
118 Stack.Step(Stack.Object, NULL);
119 FStructProperty* StructProperty = CastField<FStructProperty>(Stack.MostRecentProperty);
120 void* StructPtr = Stack.MostRecentPropertyAddress;
121
122 // We need this to wrap up the stack
123 P_FINISH;
124
125 auto BPJsonObject = NewObject<UESJsonObject>();
126
127 auto JsonObject = UESIOJConvert::ToJsonObject(StructProperty->Struct, StructPtr, true);
128 BPJsonObject->SetRootObject(JsonObject);
129
130 *(UESJsonObject**)RESULT_PARAM = BPJsonObject;
131 }
132
133 UFUNCTION(BlueprintPure, CustomThunk, meta = (DisplayName = "To JsonValue (Struct)", BlueprintAutocast, CustomStructureParam = "AnyStruct"), Category = "Utilities|EmoShapeSocketIO")
134 static UESJsonValue* StructToJsonValue(TFieldPath<FProperty> AnyStruct);
135
136 DECLARE_FUNCTION(execStructToJsonValue)
137 {
138 //Get properties and pointers from stack
139 Stack.Step(Stack.Object, NULL);
140 FStructProperty* StructProperty = CastField<FStructProperty>(Stack.MostRecentProperty);
141 void* StructPtr = Stack.MostRecentPropertyAddress;
142
143 // We need this to wrap up the stack
144 P_FINISH;
145
146 auto JsonObject = UESIOJConvert::ToJsonObject(StructProperty->Struct, StructPtr, true);
147
148 TSharedPtr<FJsonValue> JsonValue = MakeShareable(new FJsonValueObject(JsonObject));
149 UESJsonValue* BPJsonValue = NewObject<UESJsonValue>();
150 BPJsonValue->SetRootValue(JsonValue);
151
152 *(UESJsonValue**)RESULT_PARAM = BPJsonValue;
153 }
154
155
163 UFUNCTION(BlueprintCallable, Category = "EmoShapeSocketIOFunctions", CustomThunk, meta = (CustomStructureParam = "AnyStruct"))
164 static bool JsonObjectToStruct(UESJsonObject* JsonObject, TFieldPath<FProperty> AnyStruct);
165
166 DECLARE_FUNCTION(execJsonObjectToStruct)
167 {
168 //Get properties and pointers from stack
169 P_GET_OBJECT(UESJsonObject, JsonObject);
170
171 Stack.Step(Stack.Object, NULL);
172 FStructProperty* StructProperty = CastField<FStructProperty>(Stack.MostRecentProperty);
173 void* StructPtr = Stack.MostRecentPropertyAddress;
174
175 P_FINISH;
176
177 //Pass in the reference to the json object
178 bool Success = UESIOJConvert::JsonObjectToUStruct(JsonObject->GetRootObject(), StructProperty->Struct, StructPtr, true);
179
180 *(bool*)RESULT_PARAM = Success;
181 }
182
183 //Convenience - Saving/Loading structs from files
184 UFUNCTION(BlueprintCallable, Category = "EmoShapeSocketIOFunctions", CustomThunk, meta = (CustomStructureParam = "AnyStruct"))
185 static bool SaveStructToJsonFile(TFieldPath<FProperty> AnyStruct, const FString& FilePath);
186
187 UFUNCTION(BlueprintCallable, Category = "EmoShapeSocketIOFunctions", CustomThunk, meta = (CustomStructureParam = "AnyStruct"))
188 static bool LoadJsonFileToStruct(const FString& FilePath, TFieldPath<FProperty> AnyStruct);
189
190 //custom thunk needed to handle wildcard structs
191 DECLARE_FUNCTION(execSaveStructToJsonFile)
192 {
193 Stack.StepCompiledIn<FStructProperty>(NULL);
194 FStructProperty* StructProp = CastField<FStructProperty>(Stack.MostRecentProperty);
195 void* StructPtr = Stack.MostRecentPropertyAddress;
196
197 FString FilePath;
198 Stack.StepCompiledIn<FStrProperty>(&FilePath);
199 P_FINISH;
200
201 P_NATIVE_BEGIN;
202 *(bool*)RESULT_PARAM = UESIOJConvert::ToJsonFile(FilePath, StructProp->Struct, StructPtr);
203 P_NATIVE_END;
204 }
205
206 //custom thunk needed to handle wildcard structs
207 DECLARE_FUNCTION(execLoadJsonFileToStruct)
208 {
209 FString FilePath;
210 Stack.StepCompiledIn<FStrProperty>(&FilePath);
211 Stack.StepCompiledIn<FStructProperty>(NULL);
212 FStructProperty* StructProp = CastField<FStructProperty>(Stack.MostRecentProperty);
213 void* StructPtr = Stack.MostRecentPropertyAddress;
214 P_FINISH;
215
216 P_NATIVE_BEGIN;
217 *(bool*)RESULT_PARAM = UESIOJConvert::JsonFileToUStruct(FilePath, StructProp->Struct, StructPtr, true);
218 P_NATIVE_END;
219 }
220
221 //Conversion Nodes - comments added for blueprint hover with compact nodes
222
223 //To JsonValue (Array)
224 UFUNCTION(BlueprintPure, meta = (DisplayName = "To JsonValue (Array)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
225 static UESJsonValue* Conv_ArrayToJsonValue(const TArray<UESJsonValue*>& InArray);
226
227 //To JsonValue (JsonObject)
228 UFUNCTION(BlueprintPure, meta = (DisplayName = "To JsonValue (JsonObject)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
229 static UESJsonValue* Conv_JsonObjectToJsonValue(UESJsonObject* InObject);
230
231 //To JsonValue (Bytes)
232 UFUNCTION(BlueprintPure, meta = (DisplayName = "To JsonValue (Bytes)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
233 static UESJsonValue* Conv_BytesToJsonValue(const TArray<uint8>& InBytes);
234
235 //To JsonValue (String)
236 UFUNCTION(BlueprintPure, meta = (DisplayName = "To JsonValue (String)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
237 static UESJsonValue* Conv_StringToJsonValue(const FString& InString);
238
239 //To JsonValue (Integer)
240 UFUNCTION(BlueprintPure, meta = (DisplayName = "To JsonValue (Integer)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
241 static UESJsonValue* Conv_IntToJsonValue(int32 InInt);
242
243 //To JsonValue (Float)
244 UFUNCTION(BlueprintPure, meta = (DisplayName = "To JsonValue (Float)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
245 static UESJsonValue* Conv_FloatToJsonValue(float InFloat);
246
247 //To JsonValue (Bool)
248 UFUNCTION(BlueprintPure, meta = (DisplayName = "To JsonValue (Bool)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
249 static UESJsonValue* Conv_BoolToJsonValue(bool InBool);
250
251 //To String (JsonValue) - doesn't autocast due to get display name
252 UFUNCTION(BlueprintPure, meta = (DisplayName = "To String (JsonValue)", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
253 static FString Conv_EmoShapeJsonValueToString(class UESJsonValue* InValue);
254
255 //To Integer (JsonValue)
256 UFUNCTION(BlueprintPure, meta = (DisplayName = "To Integer (JsonValue)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
257 static int32 Conv_JsonValueToInt(class UESJsonValue* InValue);
258
259 //To Float (JsonValue)
260 UFUNCTION(BlueprintPure, meta = (DisplayName = "To Float (JsonValue)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
261 static float Conv_JsonValueToFloat(class UESJsonValue* InValue);
262
263 //To Bool (JsonValue)
264 UFUNCTION(BlueprintPure, meta = (DisplayName = "To Bool (JsonValue)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
265 static bool Conv_JsonValueToBool(class UESJsonValue* InValue);
266
267 //To Bytes (JsonValue)
268 UFUNCTION(BlueprintPure, meta = (DisplayName = "To Bytes (JsonValue)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
269 static TArray<uint8> Conv_JsonValueToBytes(class UESJsonValue* InValue);
270
271 //To String (JsonObject) - doesn't autocast due to get display name
272 UFUNCTION(BlueprintPure, meta = (DisplayName = "To String (JsonObject)", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
273 static FString Conv_EmoShapeJsonObjectToString(class UESJsonObject* InObject);
274
275 //To Object (JsonValue)
276 UFUNCTION(BlueprintPure, meta = (DisplayName = "To Object (JsonValue)", CompactNodeTitle = "->", BlueprintAutocast), Category = "Utilities|EmoShapeSocketIO")
277 static UESJsonObject* Conv_JsonValueToJsonObject(class UESJsonValue* InValue);
278
279public:
281 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Utility", meta = (WorldContext = "WorldContextObject"))
282 static void CallURL(UObject* WorldContextObject, const FString& URL, EEmoShapeIORequestVerb Verb, EEmoShapeIORequestContentType ContentType, UESJsonObject* EmoShapeJJson, const FEmoShapeJsonCallDelegate& Callback);
283
285 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Utility", meta = (Latent, LatentInfo = "LatentInfo", WorldContext = "WorldContextObject"))
286 static void GetURLBinary(UObject* WorldContextObject, const FString& URL, EEmoShapeIORequestVerb Verb, EEmoShapeIORequestContentType ContentType, TArray<uint8>& OutResultData, struct FLatentActionInfo LatentInfo);
287
289 static void OnCallComplete(UESRequestJSON* Request);
290
291private:
292 static TMap<UESRequestJSON*, FEmoShapeCallResponse> RequestMap;
293
294};
ESIOJConvert.h
ESIOTypes.h
EEmoShapeIORequestVerb
EEmoShapeIORequestVerb
Definition ESIOTypes.h:11
EEmoShapeIORequestContentType
EEmoShapeIORequestContentType
Definition ESIOTypes.h:23
DECLARE_DYNAMIC_DELEGATE_OneParam
DECLARE_DYNAMIC_DELEGATE_OneParam(FEmoShapeJsonCallDelegate, UESRequestJSON *, Request)
ESJsonObject.h
ESJsonValue.h
UESIOJConvert
Definition ESIOJConvert.h:20
UESIOJConvert::JsonFileToUStruct
static bool JsonFileToUStruct(const FString &FilePath, UStruct *Struct, void *StructPtr, bool IsBlueprintStruct=false)
Definition ESIOJConvert.cpp:714
UESIOJConvert::ToJsonObject
static TSharedPtr< FJsonObject > ToJsonObject(const FString &JsonString)
Definition ESIOJConvert.cpp:603
UESIOJConvert::ToJsonFile
static bool ToJsonFile(const FString &FilePath, UStruct *Struct, void *StructPtr, bool IsBlueprintStruct=false)
Definition ESIOJConvert.cpp:731
UESIOJConvert::JsonObjectToUStruct
static bool JsonObjectToUStruct(TSharedPtr< FJsonObject > JsonObject, UStruct *Struct, void *StructPtr, bool IsBlueprintStruct=false, bool BinaryStructCppSupport=false)
Definition ESIOJConvert.cpp:681
UESJsonLibrary
Definition ESJsonLibrary.h:46
UESJsonLibrary::UFUNCTION
UFUNCTION(BlueprintPure, meta=(DisplayName="To JsonValue (Array)", CompactNodeTitle="->", BlueprintAutocast), Category="Utilities|EmoShapeSocketIO") static UESJsonValue *Conv_ArrayToJsonValue(const TArray< UESJsonValue * > &InArray)
UESJsonLibrary::UFUNCTION
UFUNCTION(BlueprintPure, CustomThunk, meta=(DisplayName="To JsonValue (Struct)", BlueprintAutocast, CustomStructureParam="AnyStruct"), Category="Utilities|EmoShapeSocketIO") static UESJsonValue *StructToJsonValue(TFieldPath< FProperty > AnyStruct)
UESJsonLibrary::DECLARE_FUNCTION
DECLARE_FUNCTION(execLoadJsonFileToStruct)
Definition ESJsonLibrary.h:207
UESJsonLibrary::UFUNCTION
UFUNCTION(BlueprintPure, Category="EmoShapeJson|Utility", meta=(DisplayName="Base64 Encode (String)")) static FString Base64Encode(const FString &Source)
UESJsonObject
Definition ESJsonObject.h:10
UESJsonValue
Definition ESJsonValue.h:63
UESJsonValue::SetRootValue
void SetRootValue(TSharedPtr< FJsonValue > &JsonValue)
Definition ESJsonValue.cpp:147
UESRequestJSON
Definition ESRequestJSON.h:78
FEmoShapeCallResponse
Definition ESJsonLibrary.h:21