diff --git a/Source/VaRest/Private/VaRestJsonValue.cpp b/Source/VaRest/Private/VaRestJsonValue.cpp index f68b1754..5d1ad085 100644 --- a/Source/VaRest/Private/VaRestJsonValue.cpp +++ b/Source/VaRest/Private/VaRestJsonValue.cpp @@ -10,6 +10,11 @@ UVaRestJsonValue::UVaRestJsonValue(const FObjectInitializer& ObjectInitializer) { } +void UVaRestJsonValue::Reset() +{ + JsonVal = nullptr; +} + TSharedPtr& UVaRestJsonValue::GetRootValue() { return JsonVal; diff --git a/Source/VaRest/Private/VaRestRequestJSON.cpp b/Source/VaRest/Private/VaRestRequestJSON.cpp index bf0e303d..f6a2f124 100644 --- a/Source/VaRest/Private/VaRestRequestJSON.cpp +++ b/Source/VaRest/Private/VaRestRequestJSON.cpp @@ -4,6 +4,7 @@ #include "VaRestDefines.h" #include "VaRestJsonObject.h" +#include "VaRestJsonValue.h" #include "VaRestLibrary.h" #include "VaRestSettings.h" @@ -112,6 +113,15 @@ void UVaRestRequestJSON::ResetResponseData() ResponseJsonObj = NewObject(); } + if (ResponseJsonValue != nullptr) + { + ResponseJsonValue->Reset(); + } + else + { + ResponseJsonValue = NewObject(); + } + ResponseHeaders.Empty(); ResponseCode = -1; ResponseSize = 0; @@ -166,6 +176,12 @@ void UVaRestRequestJSON::SetResponseObject(UVaRestJsonObject* JsonObject) ResponseJsonObj = JsonObject; } +UVaRestJsonValue* UVaRestRequestJSON::GetResponseValue() const +{ + check(ResponseJsonValue); + return ResponseJsonValue; +} + /////////////////////////////////////////////////////////////////////////// // Response data access @@ -500,11 +516,16 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp { // Use default unreal one const TSharedRef> Reader = TJsonReaderFactory<>::Create(*Response->GetContentAsString()); - TSharedPtr OutJsonObj; - if (FJsonSerializer::Deserialize(Reader, OutJsonObj)) + TSharedPtr OutJsonValue; + if (FJsonSerializer::Deserialize(Reader, OutJsonValue)) { - ResponseJsonObj->SetRootObject(OutJsonObj.ToSharedRef()); + ResponseJsonValue->SetRootValue(OutJsonValue); ResponseSize = Response->GetContentLength(); + + if (ResponseJsonValue->GetType() == EVaJson::Object) + { + ResponseJsonObj->SetRootObject(ResponseJsonValue->GetRootValue()->AsObject()); + } } } diff --git a/Source/VaRest/Private/VaRestSubsystem.cpp b/Source/VaRest/Private/VaRestSubsystem.cpp index 8527b8dd..e79653cc 100644 --- a/Source/VaRest/Private/VaRestSubsystem.cpp +++ b/Source/VaRest/Private/VaRestSubsystem.cpp @@ -8,6 +8,8 @@ #include "Misc/FileHelper.h" #include "Misc/Paths.h" +#include "Serialization/JsonReader.h" +#include "Serialization/JsonSerializer.h" #include "Subsystems/SubsystemBlueprintLibrary.h" UVaRestSubsystem::UVaRestSubsystem() @@ -164,6 +166,18 @@ UVaRestJsonValue* UVaRestSubsystem::ConstructJsonValue(const TSharedPtr> Reader = TJsonReaderFactory<>::Create(*JsonString); + TSharedPtr OutJsonValue; + if (FJsonSerializer::Deserialize(Reader, OutJsonValue)) + { + return ConstructJsonValue(OutJsonValue); + } + + return nullptr; +} + class UVaRestJsonObject* UVaRestSubsystem::LoadJsonFromFile(const FString& Path, const bool bIsRelativeToContentDir) { auto* Json = ConstructVaRestJsonObject(); diff --git a/Source/VaRest/Public/VaRestJsonValue.h b/Source/VaRest/Public/VaRestJsonValue.h index ad2bb0e3..c5e5305c 100644 --- a/Source/VaRest/Public/VaRestJsonValue.h +++ b/Source/VaRest/Public/VaRestJsonValue.h @@ -30,6 +30,11 @@ class VAREST_API UVaRestJsonValue : public UObject { GENERATED_UCLASS_BODY() +public: + /** Reset all internal data */ + UFUNCTION(BlueprintCallable, Category = "VaRest|Json") + void Reset(); + /** Get the root Json value */ TSharedPtr& GetRootValue(); diff --git a/Source/VaRest/Public/VaRestRequestJSON.h b/Source/VaRest/Public/VaRestRequestJSON.h index d74da2b3..8f39c06e 100644 --- a/Source/VaRest/Public/VaRestRequestJSON.h +++ b/Source/VaRest/Public/VaRestRequestJSON.h @@ -12,6 +12,7 @@ #include "VaRestRequestJSON.generated.h" +class UVaRestJsonValue; class UVaRestJsonObject; class UVaRestSettings; @@ -157,6 +158,10 @@ class VAREST_API UVaRestRequestJSON : public UObject UFUNCTION(BlueprintCallable, Category = "VaRest|Response") void SetResponseObject(UVaRestJsonObject* JsonObject); + /** Get the Response Json value */ + UFUNCTION(BlueprintCallable, Category = "VaRest|Response") + UVaRestJsonValue* GetResponseValue() const; + /////////////////////////////////////////////////////////////////////////// // Request/response data access @@ -297,6 +302,10 @@ class VAREST_API UVaRestRequestJSON : public UObject UPROPERTY() UVaRestJsonObject* ResponseJsonObj; + /** Response data stored as JSON value */ + UPROPERTY() + UVaRestJsonValue* ResponseJsonValue; + /** Verb for making request (GET,POST,etc) */ EVaRestRequestVerb RequestVerb; diff --git a/Source/VaRest/Public/VaRestSubsystem.h b/Source/VaRest/Public/VaRestSubsystem.h index f4547975..9773e34f 100644 --- a/Source/VaRest/Public/VaRestSubsystem.h +++ b/Source/VaRest/Public/VaRestSubsystem.h @@ -103,6 +103,14 @@ class VAREST_API UVaRestSubsystem : public UEngineSubsystem /** Create new Json value from FJsonValue (to be used from VaRestJsonObject) */ UVaRestJsonValue* ConstructJsonValue(const TSharedPtr& InValue); + ////////////////////////////////////////////////////////////////////////// + // Serialization + +public: + /** Construct Json value from string */ + UFUNCTION(BlueprintCallable, Category = "VaRest|Subsystem") + UVaRestJsonValue* DecodeJson(const FString& JsonString); + ////////////////////////////////////////////////////////////////////////// // File system integration