TSharedPtr FSpawnerBaseConfig::SerializeToJson()

in Ambit/Source/Ambit/Actors/SpawnerConfigs/SpawnerBaseConfig.cpp [27:103]


TSharedPtr<FJsonObject> FSpawnerBaseConfig::SerializeToJson() const
{
    // Returns Json with SpawnerLocation and SpawnerRotation serialized
    // as well as BoxExtent (if SpawnerType is Bounding)
    // or SplinePoints (if SpawnerType is Spline)
    TSharedPtr<FJsonObject> Json = MakeShareable(new FJsonObject);

    // Serialize location and rotation of spawner as JSON arrays
    Json->SetArrayField(JsonKeys::KSpawnerLocationKey, FJsonHelpers::SerializeVector3(SpawnerLocation));
    Json->SetArrayField(JsonKeys::KSpawnerRotationKey, FJsonHelpers::SerializeRotation(SpawnerRotation));

    // Serialize MatchByValue as JSON string
    const FString& MatchByValue = MatchBy == EMatchBy::NameOrTags ? "NameOrTags" : "NameAndTags";
    Json->SetStringField(JsonKeys::KMatchByKey, MatchByValue);

    // Serialize SurfaceNamePattern as JSON string
    Json->SetStringField(JsonKeys::KSurfaceNamePatternKey, SurfaceNamePattern);

    // Serialize SurfaceTags as JSON array of strings
    TArray<TSharedPtr<FJsonValue>> TagsJson;
    for (const FName& SurfaceTag : SurfaceTags)
    {
        const FString& TagAsString = SurfaceTag.ToString();
        TagsJson.Add(MakeShareable(new FJsonValueString(TagAsString)));
    }
    Json->SetArrayField(JsonKeys::KSurfaceTagsKey, TagsJson);

    // Serialize DensityMin as JSON number
    Json->SetNumberField(JsonKeys::KDensityMinKey, DensityMin);

    // Serialize DensityMax as JSON number
    Json->SetNumberField(JsonKeys::KDensityMaxKey, DensityMax);

    // Serialize RotationMin as JSON number
    Json->SetNumberField(JsonKeys::KRotationMinKey, RotationMin);

    // Serialize RotationMax as JSON number
    Json->SetNumberField(JsonKeys::KRotationMaxKey, RotationMax);

    // Serialize bAddPhysics as JSON bool
    Json->SetBoolField(JsonKeys::KAddPhysicsKey, bAddPhysics);

    // Serialize ActorsToSpawn as JSON string array
    if (ActorsToSpawn.Num() > 0 && !ActorsToSpawn.Contains(nullptr))
    {
        TArray<TSharedPtr<FJsonValue>> ActorsJson;
        for (const TSubclassOf<AActor>& Actor : ActorsToSpawn)
        {
            const FString& PathName = Actor->GetPathName();
            ActorsJson.Add(MakeShareable(new FJsonValueString(PathName)));
        }
        if (ActorsJson.Num() > 0)
        {
            Json->SetArrayField(JsonKeys::KActorsToSpawnKey, ActorsJson);
        }
        else
        {
            Json->SetField(JsonKeys::KActorsToSpawnKey, MakeShareable(new FJsonValueNull));
        }
    }
    else
    {
        if (ActorsToSpawn.Contains(nullptr))
        {
            UE_LOG(LogAmbit, Warning, TEXT("An element of the ActorsToSpawn set is not specified."));
        }
        Json->SetField(JsonKeys::KActorsToSpawnKey, MakeShareable(new FJsonValueNull));
    }

    // Serialize RemoveOverlaps as a JSON bool
    Json->SetBoolField(JsonKeys::KRemoveOverlapsKey, bRemoveOverlaps);

    // Serialize RandomSeed as JSON number
    Json->SetNumberField(JsonKeys::KRandomSeedKey, RandomSeed);

    return Json;
}