How to transform .json files in Rider

773 views Asked by At

In order to have different configurations available for testing of different mobile OS's, I'm trying to create transform files in Rider on (config/appsettings).json files.

On the rider website there's a blog showing how to do exactly that for .config files: XDT configuration transformations in Rider

Visual Studio has an extension which allows .json transformations called SlowCheetah: SlowCheetah - Visual Studio Marketplace

So far I haven't been able to do this in Rider on .json files.

2

There are 2 answers

0
Erijk On BEST ANSWER

Ok, a heads up on my own question ;)

I figured I can alter the .csproj project file to create (but not generate) transform files:

<ItemGroup>
    <None Update="Config\Config.Android.json">
      <IsTransformFile>true</IsTransformFile>
      <DependentUpon>Config.json</DependentUpon>
    </None>
    <None Update="Config\Config.IOS.json">
      <IsTransformFile>true</IsTransformFile>
      <DependentUpon>Config.json</DependentUpon>
    </None>
    <None Update="Config\Config.json">
        <TransformOnBuild>true</TransformOnBuild>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
</ItemGroup>
<ItemGroup>
    <None Update="appsettings.json">
        <TransformOnBuild>true</TransformOnBuild>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.IOS.json">
        <IsTransformFile>true</IsTransformFile>
        <DependentUpon>appsettings.json</DependentUpon>
    </None>
    <None Update="appsettings.Android.json">
        <IsTransformFile>true</IsTransformFile>
        <DependentUpon>appsettings.json</DependentUpon>
    </None>
</ItemGroup>

2
mu88 On

XDT stands for XML Data Transform, therefore JSON is not supported.

But you can use different JSON files per environment, as stated in the official docs:

// appsettings.json
{
  "MyKey": "My default Value",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

// appsettings.Development.json
{
  "MyKey": "My Development Value"
}

// appsettings.Production.json
{
  "MyKey": "My Production Value"
}

Please note that this is bound to the new .NET (aka .NET Core, .NET ≥ 5).