UStructToJsonObject not working as expected since 4.13

I’m using FJsonObjectConverter::UStructToJsonObject to convert a struct to json object. And the struct has a UProperty of FJsonObjectWrapper. Something like this:

UStruct()
struct FMyStruct
{
  GENERATED_USTRUCT_BODY()

  UProperty() FJsonObjectWrapper SomeJsonObject;
}

Everything works fine at 4.12. However, since 4.13, it returns FJsonObjectWrapper as Json Value String instead of Json Value Object. Then everything break.

After reading the source code, I find the reason is that UE 4.13 added a function ExportTextItem to FJsonObjectWrapper. I don’t know why this need to be added. Is this a bug or something?

Most important, is there some way to keep it return Object instead of String?

Any help will be appreciate!

Hey Adcentury,

I am not sure what you are seeing but my FJsonObjectWrapper gives both a FString as “JsonString” and a TSharedPtr as “JsonObject”. Here is a screenshot:

Can you please provide more information related to your issue?

Thanks.

I had the same problem and have tracked down the source. In JsonObjectConverter.cpp in the ConvertScalarUPropertyToJsonValue function, in the UStructProperty section, the Struct is first checked for HasExportTextItem(). If it doesn’t then it is exported with UStructToJsonObject where the code that checks for FJsonObjectWrapper is located. The problem is that the FJsonObjectWrapper returns true on HasExportTextItem() so the UStructToJsonObject() function is never called for it.

A possible fix would be to disable the HasExportTextItem() check for FJsonObjectWrapper structs.

In my code I fixed this with a patch. I created a CustomExportCallback that checks to see if the property is a FJsonObjectWrapper and calls UStructToJsonObject if it is.

Here is the function that I used with the CustomExportCallback:

        TSharedPtr<FJsonValue> USampleClass::FJsonObjectWrapperFixer(
                UProperty* property, 
                const void* value)
        {
        	FString strPropertyType = property->GetCPPType();
        	if (strPropertyType == "FJsonObjectWrapper")
        	{
        		TSharedRef<FJsonObject> outObject(new FJsonObject());
        		FJsonObjectConverter::UStructToJsonObject(
        			FJsonObjectWrapper::StaticStruct(),
        			value,
        			outObject,
        			0,
        			0);
        		return TSharedPtr<FJsonValue>(new FJsonValueObject(outObject));
        	}
        	return TSharedPtr<FJsonValue>(NULL);
        }