Damien Dennehy
Damien Dennehy

Reputation: 4064

Using JsonConvert.DeserializeObject in a Class Library Package targeting .NET Core

I'm having trouble resolving a missing reference when trying to use Json.NET's JsonConvert.DeserializeObject method in a Class Library Package targeting .NET Core.

To reproduce in VS2015:

  1. Create a new Class Library Package using RC1.

Class Library Package

  1. Install Newtonsoft.Json v8.0.2.
  2. Target .NET Core. See the exact project.json file I'm using below.
  3. In the default Class1 that's created, add this simple method:

    public void DoSomething()
    {
        var x = JsonConvert.DeserializeObject("");
    }
    
  4. You'll get the following error:

The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=2.0.5.0'

Any idea what reference I'm missing? I assume it's some kind of System.Serialization or System.IO package but I can't figure out which one.

project.json:

{
"version": "1.0.0-*",
"description": "ClassLibrary1 Class Library",
"authors": [ "DD" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"frameworks": {
    "net451": { },
    "netcore50": {
        "dependencies": {
            "Microsoft.CSharp": "4.0.0",
            "System.Collections": "4.0.0",
            "System.Linq": "4.0.0",
            "System.Runtime": "4.0.0",
            "System.Threading": "4.0.0"
        }
    }
},
"dependencies": {
    "Newtonsoft.Json": "8.0.2"
}
}

Upvotes: 4

Views: 4500

Answers (2)

Imran Javed
Imran Javed

Reputation: 12677

You can use latest Newtonsoft.Json nuget package (at the moment it's 11.0.2) working fine without any problem with .NET Core 2.1.

Upvotes: 2

Jamie Dunstan
Jamie Dunstan

Reputation: 3765

You can fix this issue by adding a dependency of Microsoft.NETCore.Portable.Compatibility. [NuGet page]

{
  "version": "1.0.0-*",
  "description": "ClassLibrary1 Class Library",
  "authors": [ "DD" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "frameworks": {
    "net451": { },
    "netcore50": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.0",
        "System.Collections": "4.0.0",
        "System.Linq": "4.0.0",
        "System.Runtime": "4.0.0",
        "System.Threading": "4.0.0"
      }
    }
  },
  "dependencies": {
    "Newtonsoft.Json": "8.0.2",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1-beta-23516"
  }
}

Upvotes: 3

Related Questions