-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create wrappers for ListChannelsFromGroup and ListUserSubscribedChannels
- Loading branch information
1 parent
804ec98
commit c42ccfa
Showing
7 changed files
with
231 additions
and
64 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
Source/PubnubLibrary/Private/FunctionLibraries/PubnubJsonUtilities.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2024 PubNub Inc. All Rights Reserved. | ||
|
||
|
||
#include "FunctionLibraries/PubnubJsonUtilities.h" | ||
#include "Json.h" | ||
|
||
FString UPubnubJsonUtilities::JsonObjectToString(TSharedPtr<FJsonObject> JsonObject) | ||
{ | ||
FString JsonString; | ||
TSharedRef< TJsonWriter<> > JsonWriter = TJsonWriterFactory<>::Create(&JsonString); | ||
FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter); | ||
return JsonString; | ||
} | ||
|
||
bool UPubnubJsonUtilities::StringToJsonObject(FString JsonString, TSharedPtr<FJsonObject>& JsonObject) | ||
{ | ||
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonString); | ||
bool Result = FJsonSerializer::Deserialize(JsonReader, JsonObject); | ||
return Result; | ||
} | ||
|
||
void UPubnubJsonUtilities::ListChannelsFromGroupJsonToData(FString ResponseJson, bool& Error, int& Status, TArray<FString>& Channels) | ||
{ | ||
Channels.Empty(); | ||
|
||
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject); | ||
|
||
if(!StringToJsonObject(ResponseJson, JsonObject)) | ||
{ | ||
Error = true; | ||
return; | ||
} | ||
|
||
if(JsonObject->HasField(ANSI_TO_TCHAR("error"))) | ||
{ | ||
Error = JsonObject->GetBoolField(ANSI_TO_TCHAR("error")); | ||
} | ||
|
||
if(JsonObject->HasField(ANSI_TO_TCHAR("status"))) | ||
{ | ||
Status = JsonObject->GetIntegerField(ANSI_TO_TCHAR("status")); | ||
} | ||
|
||
if(!JsonObject->HasField(ANSI_TO_TCHAR("payload"))) | ||
{ | ||
return; | ||
} | ||
|
||
TSharedPtr<FJsonObject> PayloadJsonObject = JsonObject->GetObjectField(ANSI_TO_TCHAR("payload")); | ||
|
||
if(PayloadJsonObject->HasField(ANSI_TO_TCHAR("channels"))) | ||
{ | ||
TArray<TSharedPtr<FJsonValue>> ChannelsJsonValue = PayloadJsonObject->GetArrayField(ANSI_TO_TCHAR("channels")); | ||
|
||
for(auto ChannelJsonValue : ChannelsJsonValue) | ||
{ | ||
Channels.Add(ChannelJsonValue->AsString()); | ||
} | ||
} | ||
} | ||
|
||
void UPubnubJsonUtilities::ListUserSubscribedChannelsJsonToData(FString ResponseJson, int& Status, FString& Message, TArray<FString>& Channels) | ||
{ | ||
Channels.Empty(); | ||
|
||
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject); | ||
|
||
if(!StringToJsonObject(ResponseJson, JsonObject)) | ||
{ | ||
return; | ||
} | ||
|
||
if(JsonObject->HasField(ANSI_TO_TCHAR("status"))) | ||
{ | ||
Status = JsonObject->GetIntegerField(ANSI_TO_TCHAR("status")); | ||
} | ||
|
||
if(JsonObject->HasField(ANSI_TO_TCHAR("message"))) | ||
{ | ||
Message = JsonObject->GetStringField(ANSI_TO_TCHAR("message")); | ||
} | ||
|
||
if(!JsonObject->HasField(ANSI_TO_TCHAR("payload"))) | ||
{ | ||
return; | ||
} | ||
|
||
TSharedPtr<FJsonObject> PayloadJsonObject = JsonObject->GetObjectField(ANSI_TO_TCHAR("payload")); | ||
|
||
if(PayloadJsonObject->HasField(ANSI_TO_TCHAR("channels"))) | ||
{ | ||
TArray<TSharedPtr<FJsonValue>> ChannelsJsonValue = PayloadJsonObject->GetArrayField(ANSI_TO_TCHAR("channels")); | ||
|
||
for(auto ChannelJsonValue : ChannelsJsonValue) | ||
{ | ||
Channels.Add(ChannelJsonValue->AsString()); | ||
} | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
Source/PubnubLibrary/Private/FunctionLibraries/PubnubUtilities.cpp
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Source/PubnubLibrary/Public/FunctionLibraries/PubnubJsonUtilities.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 PubNub Inc. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include "CoreMinimal.h" | ||
#include "Kismet/BlueprintFunctionLibrary.h" | ||
#include "PubnubJsonUtilities.generated.h" | ||
|
||
class FJsonObject; | ||
|
||
/** | ||
* | ||
*/ | ||
UCLASS() | ||
class PUBNUBLIBRARY_API UPubnubJsonUtilities : public UBlueprintFunctionLibrary | ||
{ | ||
GENERATED_BODY() | ||
public: | ||
|
||
static FString JsonObjectToString(TSharedPtr<FJsonObject> JsonObject); | ||
//Convert FString to JsonObject. Returns true if conversion was successful | ||
static bool StringToJsonObject(FString JsonString, TSharedPtr<FJsonObject> &JsonObject); | ||
|
||
/* JSON CONVERTERS*/ | ||
|
||
UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities") | ||
static void ListChannelsFromGroupJsonToData(FString ResponseJson, bool &Error, int &Status, TArray<FString>& Channels); | ||
|
||
UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities") | ||
static void ListUserSubscribedChannelsJsonToData(FString ResponseJson, int &Status, FString &Message, TArray<FString>& Channels); | ||
}; |
23 changes: 0 additions & 23 deletions
23
Source/PubnubLibrary/Public/FunctionLibraries/PubnubUtilities.h
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.