A roslyn analyzer, which allows to detect and fail builds, which use Newtonsoft.Json
library.
Newtonsoft.Json
is a great library, and there are no problems with using it.
However, in some cases, people might decide to use System.Text.Json instead.
In these cases, it is necessary to have a way to detect the usage of Newtonsoft.Json
and prevent it during migration.
In a nutshell, you have 3 ways, how to use it:
- You can use a nuget package with this analyzer:
dotnet add package NewtonsoftJsonUsageAnalyzer --version 0.2.0
- You can attach this analyzer per specific project(s) (e.g., NewtonsoftJsonUsageAnalyzer.Sample.csproj):
<ItemGroup>
<ProjectReference Include="..\NewtonsoftJsonUsageAnalyzer\NewtonsoftJsonUsageAnalyzer.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
- You can attach this analyzer globally via
Directory.Build.props
:
<Project>
<ItemGroup>
<!-- Use MSBuildThisFileDirectory to make the path relative to the current file location -->
<ProjectReference Include="$(MSBuildThisFileDirectory)\NewtonsoftJsonUsageAnalyzer\NewtonsoftJsonUsageAnalyzer.csproj"
PrivateAssets="all"
ReferenceOutputAssembly="false"
OutputItemType="Analyzer" />
</ItemGroup>
</Project>
$ dotnet build
1>Examples.cs(4,1): Error NS0001 : Detected Newtonsoft usage: 'Newtonsoft.Json'
1>------- Finished building project: NewtonsoftJsonUsageAnalyzer.Sample. Succeeded: False. Errors: 1. Warnings: 0
Build completed in 00:00:01.838
- https://github.com/jonathanpeppers/memory-analyzers - example, how to build Roslyn analyzers.
- JetBrains Rider - really awesome predifined template for Roslyn analyzers, including samples and tests.