如何在 MS 构建任务中使用 NewtonSoft.json?

Far*_*nif 2 .net c# msbuild json visual-studio

我有一个构建任务,我想在其中使用 newtonsoft.json 执行一些 json 序列化/反序列化,如下所示:

<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <TargetFilename ParameterType="System.String" Required="true" />
      <SourceFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.Core" />

      <Using Namespace="System" />
      <Using Namespace="System.IO" />

      <Code Type="Fragment" Language="cs">
        <![CDATA[

            string sourceJsonString = File.ReadAllText(SourceFilename);
            string targetJsonString = File.ReadAllText(TargetFilename);

            dynamic sourceJson = JsonConvert.DeserializeObject(sourceJsonString);
            dynamic targetJson = JsonConvert.DeserializeObject(targetJsonString);

            targetJson.firstProperty = sourceJson.firstProperty;
            targetJson.secondProperty = sourceJson.secondProperty;


            targetJsonString = JsonConvert.SerializeObject(targetJson, Formatting.Indented);

            File.WriteAllText(targetFilename, targetJsonString);

          ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="TransformsWithStaging" BeforeTargets="Build">
    <ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Staging.json" />
  </Target>
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用,因为我在上面的任务中使用了 NewtonSoft.Json,但在这个构建任务中没有引用。

如何在构建任务中导入或引用 NewtonSoft.Json。我使用的是 Visual Studio 2017 Professional、.NET Core 2.0 并且该项目是一个 WebApi 应用程序?

Far*_*nif 5

得到它与以下工作:

<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <TargetFilename ParameterType="System.String" Required="true" />
      <SourceFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.Core" />
      <Reference Include="System.Runtime" />
      <Reference Include="System.Dynamic.Runtime" />
      <Reference Include="$(NugetPackageRoot)\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0\System.ObjectModel.dll" />
      <Reference Include="$(NugetPackageRoot)\newtonsoft.json\10.0.3\lib\netstandard1.3\Newtonsoft.Json.dll" />
      <Reference Include="$(NugetPackageRoot)\system.runtime.serialization.primitives\4.1.1\lib\netstandard1.3\System.Runtime.Serialization.Primitives.dll" />

      <Using Namespace="System" />
      <Using Namespace="System.IO" />
      <Using Namespace="System.Collections.Specialized" />
      <Using Namespace="Newtonsoft.Json" />
      <Using Namespace="Newtonsoft.Json.Linq" />

      <Code Type="Fragment" Language="cs">
        <![CDATA[

            string sourceJsonString = File.ReadAllText(SourceFilename);
            string targetJsonString = File.ReadAllText(TargetFilename);

            JObject sourceJsonObject = JObject.Parse(sourceJsonString);
            JObject targetJsonObject = JObject.Parse(targetJsonString);

            targetJsonObject["someProperty"] = sourceJsonObject["someProperty"];

            File.WriteAllText(TargetFilename, targetJsonObject.ToString());

          ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="TransformsWithDevelopment" Condition="'$(Configuration)'=='Development'" BeforeTargets="Build">
    <ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Development.json" />
  </Target>

  <Target Name="TransformsWithStaging" Condition="'$(Configuration)'=='Staging'" BeforeTargets="Build">
    <ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Staging.json" />
  </Target>

  <Target Name="TransformsWithProduction" Condition="'$(Configuration)'=='Production'" BeforeTargets="Build">
    <ReplaceFileText TargetFilename="$(ProjectDir)appsettings.json" SourceFilename="$(ProjectDir)appsettings.Production.json" />
  </Target>
Run Code Online (Sandbox Code Playgroud)

  • 您也可以尝试使用 `$(NuGetPackageRoot)newtonsoft.json\10.0.3\...` 这样您就不需要对 nuget 路径进行硬编码并使其跨平台工作(CodeTaskFactory 也适用于 Mono 的 msbuild)。 (2认同)