Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON array at top level of response #77

Closed
unktomi opened this issue Aug 21, 2016 · 7 comments
Closed

JSON array at top level of response #77

unktomi opened this issue Aug 21, 2016 · 7 comments
Labels
enhancement New feature or request feature request
Projects

Comments

@unktomi
Copy link
Contributor

unktomi commented Aug 21, 2016

Hello,
I was trying to use VaRest to talk to a Philips Hue bridge. That device sends json responses with an Array not Object at the top level. It seems the response in VaRest is hardcoded to VaResttJsonObject. Apparently having an Array is legal Json:
RFC 4627:
"A JSON text is a serialized object or array."
Not sure what the best way to fix this is.

@ufna ufna added the help wanted Extra attention is needed label Aug 21, 2016
@ufna
Copy link
Owner

ufna commented Aug 21, 2016

Hi @unktomi ,

Unfortunately top-level arrays are not supported by Epic's deserializer, but you can try to frame top level array as variable manually:

@ufna ufna closed this as completed Aug 21, 2016
@ufna
Copy link
Owner

ufna commented Aug 21, 2016

@unktomi
Copy link
Contributor Author

unktomi commented Aug 21, 2016

Thanks for the workaround - however the Epic serializer as of at least 4.12 does seem to support deserializing a JSON Array:
template
static bool Deserialize( const TSharedRef< TJsonReader >& Reader, TArray< TSharedPtr >& OutArray )

@ufna ufna reopened this Aug 21, 2016
@ufna
Copy link
Owner

ufna commented Aug 21, 2016

Thanks, I'll check it tomorrow

@gwythaint
Copy link

One has to double quote the "myarray" field name.

@ufna ufna added enhancement New feature or request and removed help wanted Extra attention is needed labels Jan 23, 2018
@saidmoya12
Copy link

@ufna Responsecontent No work with lastest VaRest for UE4.19

@ufna ufna added this to Backlog in Development Oct 5, 2019
@hgsujay
Copy link

hgsujay commented Jan 9, 2021

For those who are looking for the solution here is what I did.
I added a new class in my game module and got this to work.
Added a new class named VaRestBpLibrary.cpp that inherits UBlueprintFunctionLibrary.
Added the Json and VaRest as dependencies
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Http", "Json", "VaRest" });
And then added the following function in the Library.
VaRestBpLibrary.h file

#pragma once

#include "CoreMinimal.h"
#include "VaRestJsonObject.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "VaRestBpLibrary.generated.h"

UCLASS()
class MYGAME_API UVaRestBpLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	UFUNCTION(BlueprintCallable, Category = "VaRestBpLibrary", meta = (DisplayName = "Decode Top Level Array"))
	static TArray<UVaRestJsonObject*> DecodeTopLevelArray(FString Response);
};

VaRestBpLibrary.cpp

#include "VaRestBpLibrary.h"

TArray<UVaRestJsonObject*> UVaRestBpLibrary::DecodeTopLevelArray(FString Response)
{	
	TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(*Response);
	TArray<TSharedPtr<FJsonValue>> OutArray;
	TArray<UVaRestJsonObject*> FinalArray;
	if (FJsonSerializer::Deserialize(Reader, OutArray))
	{
		for (TSharedPtr<FJsonValue> JsonValue : OutArray)
		{
			const TSharedPtr<FJsonObject>* JsonObject;
			JsonValue->TryGetObject(JsonObject);
			UVaRestJsonObject* VaRestJsonObject = NewObject<UVaRestJsonObject>();
			VaRestJsonObject->SetRootObject(JsonObject->ToSharedRef());
			FinalArray.Add(VaRestJsonObject);
		}
	}
	return FinalArray;
}

This creates a Blueprint node that can decode the top-level array and it returns an Array of VaRestJsonObjects, which you can use in the blueprints.
For the input node just plug in the response string(use Get ResponseContent as String node)

@ufna ufna closed this as completed in aae0e2c Jan 15, 2021
Development automation moved this from Backlog to Done Jan 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request
Projects
Development
  
Done
Development

No branches or pull requests

5 participants