Unreal OpenAI API 1.0.0
JsonFuncLib.h
1// OpenAI Sample, Copyright LifeEXE. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Kismet/BlueprintFunctionLibrary.h"
7#include "JsonObjectConverter.h"
8#include "JsonFuncLib.generated.h"
9
10UCLASS()
11class OPENAI_API UJsonFuncLib : public UBlueprintFunctionLibrary
12{
13 GENERATED_BODY()
14
15public:
16 static bool StringToJson(const FString& JsonString, TSharedPtr<FJsonObject>& JsonObject);
17 static bool JsonToString(const TSharedPtr<FJsonObject>& JsonObject, FString& JsonString);
18
19 template <typename OutStructType>
20 static bool ParseJSONToStruct(const FString& Data, OutStructType* OutStruct)
21 {
22 TSharedPtr<FJsonObject> JsonObject;
23 if (!UJsonFuncLib::StringToJson(Data, JsonObject)) return false;
24
25 FJsonObjectConverter::JsonObjectToUStruct(JsonObject.ToSharedRef(), OutStruct, 0, 0);
26 return true;
27 }
28 static FString RemoveOptionalValuesThatNotSet(const FString& JsonString);
29 static void RemoveEmptyArrays(const TSharedPtr<FJsonObject>& JsonObject);
30
31 // helpers for OpeanAI 'functions'
32 static FString MakeFunctionsString(const TSharedPtr<FJsonObject>& Json);
33 static FString CleanUpFunctionsObject(const FString& Input);
34
35 // errors
36 static bool OpenAIResponseContainsError(const TSharedPtr<FJsonObject>& JsonObject);
37
38private:
39 static const FString START_FUNCTION_OBJECT_MARKER;
40 static const FString END_FUNCTION_OBJECT_MARKER;
41
42 static void RemoveOptionalValuesInJsonObject(const TSharedPtr<FJsonObject>& JsonObject);
43 static void ProcessJsonArrayRemovingEmptyArrays(TArray<TSharedPtr<FJsonValue>>& JsonArray);
44};
Definition: JsonFuncLib.h:12