Flexible JSON Serialization in Unreal Engine Using DataConfig

2023-08-27

Intro

We've just pushed DataConfig 1.4.3. It's a small release with some samples showing off flexibility of DataConfig's JSON reader and writer.

NDJSON

NDJSON is popular among datasets and DataConfig can handle it just fine.

FString Str = TEXT(R"(

    { "Name" : "Foo", "Id" : 1, "Type" : "Alpha" }
    { "Name" : "Bar", "Id" : 2, "Type" : "Beta" }
    { "Name" : "Baz", "Id" : 3, "Type" : "Gamma" }

)");

UTEST_OK("Extra NDJSON", LoadNDJSON(*Str, Dest));

FString SavedStr;
UTEST_OK("Extra NDJSON", SaveNDJSON(Dest, SavedStr));

Root Object/Array

Omitting root level JSON {} and [] is a tiny itch among people writes JSON manually. Turns out DataConfig can support this too:

FString Str = TEXT(R"(

    "Name" : "Foo",
    "Id" : 253,
    "Type" : "Beta"

)");
// ...
FString Str = TEXT(R"(

    "Alpha",
    "Beta",
    "Gamma"

)");

Nested Arrays

People familar with UE know that it doesn't allow multidimensional array property like TArray<TArray<int>>. We can do nothing about this but DataConfig allows you to directly loads nested JSON array into whatever you want!

USTRUCT()
struct FDcGrid2D
{
    GENERATED_BODY()
    UPROPERTY() TArray<int> Data;
};

USTRUCT()
struct FDcExtraTestNested_Grid
{
    GENERATED_BODY()
    UPROPERTY(meta=(DcWidth = 2, DcHeight = 2)) FDcGrid2D GridField1;
    UPROPERTY(meta=(DcWidth = 3, DcHeight = 4)) FDcGrid2D GridField2;
};

// ...
FString Str = TEXT(R"(
    {
        "GridField1" :
        [
            [1,2],
            [3,4],
        ],
        "GridField2" :
        [
            [ 1, 2, 3],
            [ 4, 5, 6],
            [ 7, 8, 9],
            [10,11,12],
        ],
    }
)");

Closing

These featuers are there in DataConfig from the start and now we've managed to show them off as proper samples. DataConfig is all about flexible data serialization and if you're dealing with JSON and UnrealEngine, give it a try!

Download the latest release here. There's also DataConfig JSON Asset on UE market place.