jsonserializationunreal-engine5json-serialization

UE5 UPROPERTY variable name being serialized with camel case by FJsonObjectConverter::UStructToJsonObject


I'm using UE 5.3. I have a UPROPERTY variable named 'issuetype'. When serializing using FJsonObjectConverter::UStructToJsonObject it produces 'issueType'. It only does this in packaged builds, not when running in PIE.

Looking into FJsonObjectConverter::StandardizeCase, it only makes the first character, or and the "d" in "ID", lowercase so I can't figure out why it's capitalizing the T in issuetype, not to mention why it's only doing it on packaged builds and not PIE. If it's somehow identifying compound words there's other compound words that i'm using that it's not doing this to, such as "customfield"

Here's my code:

struct:

USTRUCT()
struct FMyIssue
{
   GENERATED_BODY()

public:
   FMyIssue() = default;

   UPROPERTY()
   TMap<FString, FString> issuetype = {{TEXT("name"), TEXT(" ") }};
}

.cpp

TSharedPtr<FMyStruct> myStruct = MakeShared<FMyStruct>();
TSharedPtr<FJsonObject> jsonObject = FJsonObjectConverter::UStructToJsonObject(*myStruct);

FString serializedSelf;
const TSharedRef<TJsonWriter<>> jsonWriter = TJsonWriterFactory<>::Create(&serializedSelf);
FJsonSerializer::Serialize(jsonObject .ToSharedRef(), jsonWriter)

const FString fileLocation = TEXT("path/to/file");
FFileHelper::SaveStringToFile(serializedSelf, *fileLocation)

output:

{
    "issueType":
        {
            "name": " "
        },
}

Solution

  • Unreal's FJsonObjectConverter uses names (FName) for JSON keys, and those have a known problem with case preservation:

    https://medium.com/@pbrooks_uk/unreal-fname-non-preserved-case-sensitivitiy-b0ecf84b99d3

    Basically, if you have two FName variables in your project:

    FName TitleCaseName(L"MyActivity");
    FName camelCaseName(L"myactivity");
    

    then TitleCaseName.ToString() and camelCaseName.ToString() will return the same string, which is either "myactivity" or "MyActivity" (and it's difficult or impossible to predict which one).

    I consider this a fundamental flaw in the FJsonObjectConverter implementation. You may be better off using FJsonObject directly, or embedding a third-party JSON parser like sajson