Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonConvert Exists in both Newtonsoft and System.Net.Http.Formatting Visual Studio 2017 for Mac

I have a project I have been working on using Xamarin's MonoDevelop.

I have been using Newtonsoft's Json nuget package.

I just downloaded Visual Studio 2017 for Mac.

I try to build my project in VS2017Mac and get the following error:

error CS0433: The type 'JsonConvert' exists in both 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' and 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I thought I should be able to fix that by just adding Newtonsoft.Json. to the front of JsonConvert, but that didn't work.

I don't want to remove Newtonsoft's implementation if possible because I think their library still has more functionality. Is there another way to resolve this? Why didn't adding the full assembly reference work?

like image 685
user856232 Avatar asked May 19 '17 17:05

user856232


2 Answers

I had the following error message but with another library(Ranet) in C#:

Error CS0433 The type 'JsonConvert' exists in both 'Microsoft.AnalysisServices.Tabular.Json, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' and 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

I solved it using aliases, but I would like to provide a bit more detail as I struggled to implement using the instructions from these answers and other answers. This is how I did it:

  1. In the solution explorer, right click on the "Newtonsoft.json", add ", Newton" to the alias field, it should look something like this:

enter image description here

  1. Add the following code to the first line of you file(before all using statements):
extern alias Newton;
  1. Add the following reference to the end of your using statements:
using NewtonReference = Newton::Newtonsoft.Json;
  1. Now you can call Newtonsoft method using the following code:
NewtonReference.JsonConvert.DeserializeObject<string>("");
  1. A final example would look something like this:
extern alias Newton;

using System;
using NewtonReference = Newton::Newtonsoft.Json;

public class Test {     
    public static List<string> TestMethod() {
        NewtonReference.JsonConvert.DeserializeObject<string>("");  
    }  
}

Hopefully this will be helpful to someone else :)

like image 146
David Rogers Avatar answered Oct 20 '22 22:10

David Rogers


  1. In the Properties window for the project's Newtonsoft.Json reference, change the value of Aliases from global to global, foo.

  2. Insert extern alias foo; as the first line of any class that consumes Newtonsoft.Json.

  3. Qualify members with foo.. Example: foo.Newtonsoft.Json.JsonConvert.SerializeObject(someObject)}.

like image 22
weir Avatar answered Oct 20 '22 22:10

weir