Cannot get to deserialise a zip using FJsonSerializer

Hi everyone! I’ve been stuck trying to do this for some hours to no avail. Please bear in mind that I am a complete beginner to Unreal and C++ and have been doing this with the help of AI.
I have a .gltf file that I want to send from a server. To do this, I created a class that contains some information like the object ID, the action and the data to be sent.

The server takes an object of this class, serialises it to a JSON string, converts it into bytes and sends it using a WebSocket.

On the client side, I get to connect to the server and receive the data but I have been uncapable of deserialising it. This should happen in this line:

if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())

But that if always returns false. It is worth mentioning that I implemented this in Unity and it worked perfectly. The GLTF file nor the zip are corrupted. I tried sending the data from the server in both binary and text formats, using UTF8 and base64 string formats but nothing changed. I am really lost and don’t know what else to do. I would greatly appreciate your help or any advice!

Here’s the the code snippet of the function:

//We decompress the message and print it to the screen
WebSocket->OnRawMessage().AddLambda([this](const void* Data, SIZE_T Size, SIZE_T BytesRemaining)
{
	//Convert the received data to a readable string
	FString ReceivedJsonString = FString(UTF8_TO_TCHAR(reinterpret_cast<const char*>(Data)));

	//Parse the received JSON string
	TSharedPtr<FJsonObject> JsonObject;
	TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(ReceivedJsonString);

	if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())
	{
		//Extract the base64-encoded zip file data
		FString Base64ZipData;

		if (JsonObject->TryGetStringField(TEXT("zip_file"), Base64ZipData))
		{
			UE_LOG(LogTemp, Error, TEXT("SUCCESFULLY PARSED JSON"));

			// Convert the Base64 string to binary data
			TArray<uint8> ZipBinaryData;

			if (FBase64::Decode(Base64ZipData, ZipBinaryData))
			{
				// Save the file and extract it
				FString SavePath = FPaths::ProjectSavedDir() + TEXT("ReceivedModel.zip");

				// Save the binary data as a ZIP file
				FFileHelper::SaveArrayToFile(ZipBinaryData, *SavePath);

				GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("ZIP file saved! Extracting..."));

				// Extract the ZIP file
				ExtractZipFromMemory(ZipBinaryData, SavePath);
			}
		}
		else
		{
			GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("Failed to extract ZIP data from JSON"));
			UE_LOG(LogTemp, Error, TEXT("Failed to extract ZIP data from JSON"));
		}
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("Failed to parse JSON!"));
		UE_LOG(LogTemp, Error, TEXT("Failed to parse JSON!"));
	}

});

First step, print the value of ReceivedJsonString before deserialize.

I suspect the message might not contain the full json object, as the BytesRemaining parameter suggests, it might be split into several packets.

1 Like

Effectively, I had to take a look at the message and notice that it was not complete. I had to send it from the server in one go and even then, Unreal would only capture the message in chunks. So I had to create an FString and append the different messages until I got the full message. Only then I could deserialise it properly.

Thank you for your reply!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.