Skip to content

Commit

Permalink
Add support for top-level arrays. Close #77. Close #116.
Browse files Browse the repository at this point in the history
  • Loading branch information
ufna committed Jan 15, 2021
1 parent 67331cf commit aae0e2c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Source/VaRest/Private/VaRestJsonValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ UVaRestJsonValue::UVaRestJsonValue(const FObjectInitializer& ObjectInitializer)
{
}

void UVaRestJsonValue::Reset()
{
JsonVal = nullptr;
}

TSharedPtr<FJsonValue>& UVaRestJsonValue::GetRootValue()
{
return JsonVal;
Expand Down
27 changes: 24 additions & 3 deletions Source/VaRest/Private/VaRestRequestJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "VaRestDefines.h"
#include "VaRestJsonObject.h"
#include "VaRestJsonValue.h"
#include "VaRestLibrary.h"
#include "VaRestSettings.h"

Expand Down Expand Up @@ -112,6 +113,15 @@ void UVaRestRequestJSON::ResetResponseData()
ResponseJsonObj = NewObject<UVaRestJsonObject>();
}

if (ResponseJsonValue != nullptr)
{
ResponseJsonValue->Reset();
}
else
{
ResponseJsonValue = NewObject<UVaRestJsonValue>();
}

ResponseHeaders.Empty();
ResponseCode = -1;
ResponseSize = 0;
Expand Down Expand Up @@ -166,6 +176,12 @@ void UVaRestRequestJSON::SetResponseObject(UVaRestJsonObject* JsonObject)
ResponseJsonObj = JsonObject;
}

UVaRestJsonValue* UVaRestRequestJSON::GetResponseValue() const
{
check(ResponseJsonValue);
return ResponseJsonValue;
}

///////////////////////////////////////////////////////////////////////////
// Response data access

Expand Down Expand Up @@ -500,11 +516,16 @@ void UVaRestRequestJSON::OnProcessRequestComplete(FHttpRequestPtr Request, FHttp
{
// Use default unreal one
const TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(*Response->GetContentAsString());
TSharedPtr<FJsonObject> OutJsonObj;
if (FJsonSerializer::Deserialize(Reader, OutJsonObj))
TSharedPtr<FJsonValue> 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());
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions Source/VaRest/Private/VaRestSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -164,6 +166,18 @@ UVaRestJsonValue* UVaRestSubsystem::ConstructJsonValue(const TSharedPtr<FJsonVal
return NewValue;
}

UVaRestJsonValue* UVaRestSubsystem::DecodeJson(const FString& JsonString)
{
const TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(*JsonString);
TSharedPtr<FJsonValue> OutJsonValue;
if (FJsonSerializer::Deserialize(Reader, OutJsonValue))
{
return ConstructJsonValue(OutJsonValue);
}

return nullptr;
}

class UVaRestJsonObject* UVaRestSubsystem::LoadJsonFromFile(const FString& Path, const bool bIsRelativeToContentDir)
{
auto* Json = ConstructVaRestJsonObject();
Expand Down
5 changes: 5 additions & 0 deletions Source/VaRest/Public/VaRestJsonValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<FJsonValue>& GetRootValue();

Expand Down
9 changes: 9 additions & 0 deletions Source/VaRest/Public/VaRestRequestJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "VaRestRequestJSON.generated.h"

class UVaRestJsonValue;
class UVaRestJsonObject;
class UVaRestSettings;

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions Source/VaRest/Public/VaRestSubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<FJsonValue>& InValue);

//////////////////////////////////////////////////////////////////////////
// Serialization

public:
/** Construct Json value from string */
UFUNCTION(BlueprintCallable, Category = "VaRest|Subsystem")
UVaRestJsonValue* DecodeJson(const FString& JsonString);

//////////////////////////////////////////////////////////////////////////
// File system integration

Expand Down

0 comments on commit aae0e2c

Please sign in to comment.