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

On supporting top level response object as lists or objects #116

Closed
solfar opened this issue Sep 13, 2017 · 2 comments
Closed

On supporting top level response object as lists or objects #116

solfar opened this issue Sep 13, 2017 · 2 comments
Labels
enhancement New feature or request feature request
Projects

Comments

@solfar
Copy link

solfar commented Sep 13, 2017

There is an old problem with this library is that it doesn't support returning top level list JSON objects, even though that is a valid JSON response. There was a hack proposed on how to circumvent this restriction by changing it into a dict in blueprint, but that doesn't seem like a good long-term solution.

I noticed that the Deserialize function from Unreal supports parsing both list and objects at top level, and there is actually one version of it that automatically return the right top level, but as a FJsonValue rather than a FJsonObject:

JsonSerializer.h (58) static bool Deserialize(const TSharedRef< TJsonReader<CharType> >& Reader, TSharedPtr<FJsonValue>& OutValue)

I added a blueprint function to VaRequestJSON.cpp called GetResponseValue() that returns to me the Json value that results calling the function above. With that value I can then either call the 'AsObject' or 'AsArray' at the blueprint level to access the response as a top level object or as a top level list respectively. Seems to work fine, so this is maybe something you should consider.

@solfar
Copy link
Author

solfar commented Sep 13, 2017

Just for your interest, this is how I am doing this now: I created an extra property called ResponseJsonVal, along with a GetResponseValue() method. In VaRequestJSON.ccp, I then replace the call to Deserialize with the following:

TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(ResponseContent);
FJsonSerializer::Deserialize(JsonReader, ResponseJsonVal->GetRootValue());

if (ResponseJsonVal->GetType() == EVaJson::Object) {
	ResponseJsonObj->SetRootObject(ResponseJsonVal->AsObject()->GetRootObject());
}

This keeps the old GetResponseObject method working as before, but allows access to the more generic value through the GetResponseValue method.

@ufna ufna added enhancement New feature or request feature request todo Internal development tasks labels Oct 4, 2017
@ufna ufna added this to Backlog in Development Oct 5, 2019
@ufna ufna removed the todo Internal development tasks label Oct 5, 2019
@ufna ufna closed this as completed in aae0e2c Jan 15, 2021
Development automation moved this from Backlog to Done Jan 15, 2021
@ufna
Copy link
Owner

ufna commented Jan 18, 2021

image

Top-level arrays are readable now

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

2 participants