Hixon10 / NewtonsoftJsonUsageAnalyzer

Detect and block Newtonsoft.Json library usage

Home Page:https://www.nuget.org/packages/NewtonsoftJsonUsageAnalyzer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Newtonsoft.Json Usage Analyzer

NuGet

A roslyn analyzer, which allows to detect and fail builds, which use Newtonsoft.Json library.

Motivation

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.

How to use

In a nutshell, you have 3 ways, how to use it:

  1. You can use a nuget package with this analyzer:
dotnet add package NewtonsoftJsonUsageAnalyzer --version 0.2.0
  1. 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>
  1. 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>

Example of NS0001

Example of NS0001

$ 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

Credits

  1. https://github.com/jonathanpeppers/memory-analyzers - example, how to build Roslyn analyzers.
  2. JetBrains Rider - really awesome predifined template for Roslyn analyzers, including samples and tests.

About

Detect and block Newtonsoft.Json library usage

https://www.nuget.org/packages/NewtonsoftJsonUsageAnalyzer/

License:MIT License


Languages

Language:PowerShell 86.7%Language:C# 13.3%