TSharedPtr FSpawnVehiclePathConfig::SerializeToJson()

in Ambit/Source/Ambit/Actors/SpawnerConfigs/SpawnVehiclePathConfig.cpp [27:117]


TSharedPtr<FJsonObject> FSpawnVehiclePathConfig::SerializeToJson() const
{
    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));

    if (IsValid(VehicleToSpawn))
    {
        Json->SetStringField(JsonVehiclePathKeys::KVehicleToSpawnKey, VehicleToSpawn->GetPathName());
    }
    else
    {
        UE_LOG(LogAmbit, Warning, TEXT("No vehicle specified to serialize."));
    }

    Json->SetNumberField(JsonVehiclePathKeys::KSpeedLimit, SpeedLimit);

    Json->SetNumberField(JsonVehiclePathKeys::KDistanceBetweenWaypoints, DistanceBetweenWaypoints);

    Json->SetBoolField(JsonVehiclePathKeys::KShowDebugWaypoints, ShowDebugWaypoint);

    Json->SetBoolField(JsonKeys::KSplineCloseLoopKey, CloseLoop);
    // Make sure there are spline points to serialize, and that spline point information
    // was configured correctly (create Null JSON field if otherwise)
    if (SplinePoints.Num() > 0)
    {
        // Serialize spline point transforms and types as array of JSON objects
        TArray<TSharedPtr<FJsonValue>> SplinePointsJson;
        int32 i = 0;
        for (const FSplinePoint& Point : SplinePoints)
        {
            const TSharedPtr<FJsonObject> PointJson = MakeShareable(new FJsonObject);

            PointJson->SetNumberField(JsonKeys::KSplinePointInputKey, Point.InputKey);

            const FVector& SplinePointLocation = Point.Position;
            PointJson->SetArrayField(JsonKeys::KSplinePointRelativeLocationKey,
                                     FJsonHelpers::SerializeVector3(SplinePointLocation));

            const FVector& SplinePointArriveTangent = Point.ArriveTangent;
            PointJson->SetArrayField(JsonKeys::KSplineArriveKey,
                                     FJsonHelpers::SerializeVector3(SplinePointArriveTangent));

            const FVector& SplinePointLeaveTangent = Point.LeaveTangent;
            PointJson->SetArrayField(JsonKeys::KSplineLeaveKey,
                                     FJsonHelpers::SerializeVector3(SplinePointLeaveTangent));

            const FRotator& SplinePointRotation = Point.Rotation;
            PointJson->SetArrayField(JsonKeys::KSplinePointRelativeRotationKey,
                                     FJsonHelpers::SerializeRotation(SplinePointRotation));

            const FVector& SplinePointScale = Point.Scale;
            PointJson->SetArrayField(JsonKeys::KSplinePointScaleKey, FJsonHelpers::SerializeVector3(SplinePointScale));

            // Serialize spline point type as JSON string
            FString SplinePointTypeString;
            const TEnumAsByte<ESplinePointType::Type> SplinePointType = Point.Type;
            if (SplinePointType == ESplinePointType::Linear)
            {
                SplinePointTypeString = JsonKeys::KSplineTypeLinear;
            }
            else if (SplinePointType == ESplinePointType::Curve)
            {
                SplinePointTypeString = JsonKeys::KSplineTypeCurve;
            }
            else if (SplinePointType == ESplinePointType::Constant)
            {
                SplinePointTypeString = JsonKeys::KSplineTypeConstant;
            }
            else if (SplinePointType == ESplinePointType::CurveClamped)
            {
                SplinePointTypeString = JsonKeys::KSplineTypeCurveClamped;
            }
            else if (SplinePointType == ESplinePointType::CurveCustomTangent)
            {
                SplinePointTypeString = JsonKeys::KSplineTypeCustom;
            }
            PointJson->SetStringField(JsonKeys::KSplinePointTypeKey, SplinePointTypeString);
            SplinePointsJson.Add(MakeShareable(new FJsonValueObject(PointJson)));
        }
        Json->SetArrayField(JsonKeys::KSplinePointsKey, SplinePointsJson);
    }
    else
    {
        UE_LOG(LogAmbit, Warning, TEXT("No spline point data to serialize."));
        Json->SetField(JsonKeys::KSplinePointsKey, MakeShareable(new FJsonValueNull));
    }
    return Json;
}