I have the following object:
public class ProjectInfo { public string ConnectionStringName { get; set; } public string DefaultEntityNamespace { get; set; } public string DefaultSharedNamespace { get; set; } public string DefaultTestNamespace { get; set; } public string SqlProviderName { get; set; } }
Which I try to do a simple serialization of (in a VSIX project):
var settings = new ProjectInfo { ConnectionStringName = "SomeName" }; var json = JsonConvert.SerializeObject(settings);
which gives me:
An exception of type 'System.IO.FileNotFoundException' occurred in Newtonsoft.Json.dll but was not handled in user code Additional information: Could not load file or assembly 'System.Xml.Linq, Version=5.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
I've spent the last hour trying to figure out where the dependency comes from or why Json.NET tries to use that namespace. System.Xml.Linq is not referenced in any of my projects.
From the stack trace I can see:
at Newtonsoft.Json.Converters.XmlNodeConverter.CanConvert(Type valueType) at Newtonsoft.Json.JsonSerializer.GetMatchingConverter(IList`1 converters, Type objectType) at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract) at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType) at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType) at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer) at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
..but why does it take that route?
Update
A simple test case also fails:
[Fact] public void should_be_Able_to_Serialize_settings() { JsonConvert.SerializeObject(new ProjectInfo {ConnectionStringName = "Arne"}); }
Update 2
This project has worked before. It also works on a colleague's computer. The only difference I can see is that I've upgraded to VStudio 2015 Update 1. (or that I've made a silly mistake somewhere). But I've also done a hard reset to the latest revision that my colleague uses.
Why does it try to reference v5.0.5 of System.Linq.Xml? Isn't v4.0.0 the standard one for .NET 4.5? Which version of .NET does v5.0.5 belong to?
(I've never had a similar problem with Json.NET before. Is it something with VStudio 2015 / .NET 4.5.2 / VSIX project?)
Update3
Here are the dependencies. They show that Json.NET tries to reference that exact version:
Update:
Json.NET reference in the project file:
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=c70b2336aed9f731, processorArchitecture=MSIL"> <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath> <Private>True</Private> </Reference>
Edit 4:
My problem is that the extension fails to work as it tries to load an assembly that does not exist. From my understanding, v5.0.5 is a silverlight assembly. And I do not use silverlight.
I've tried to add an assembly redirect, but it doesn't seem to work.
<dependentAssembly> <assemblyIdentity name="System.Xml.Linq" publicKeyToken="31bf3856ad364e35" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-5.0.5.0" newVersion="4.0.0.0"/> </dependentAssembly>
Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects. In this article and code examples, first we will learn how to serialize JSON in C# and then we will learn how to deserialize JSON in C#.
Correct, JsonSerializer is threadsafe. No state is shared while serializing but if you change a setting on the JsonSerializer while in the middle of serializing an object then those will automatically be used.
Text. Json library is included in the runtime for . NET Core 3.1 and later versions. For other target frameworks, install the System.
Json.NET uses System.Xml.Linq for converting json to xml.
You're going to be able to compile without a library's dependencies, as long as you don't reference any of the types in the dependencies. This is normal. I've had the same issue with NHibernate's Iesi.Collections dependency.
I looked in the Json.Net source code and the using statements for System.Xml.Linq are conditionalized for the .Net version. Are you and your colleague running the same .Net version? Did you recently change .Net on your machine?
What I would suggest is to completely remove NHibernate and any dependencies. Then install Json.Net with NuGet. NuGet will automatically add all dependencies and perform any needed assembly binding redirects.
Even if you don't want to use NuGet, run a diff and see what changes it makes so you can make the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With