Reputation: 394
I just updated the way my code loads a file:
string filename = Path.Combine(Data.BaseDir, "entities.txt");
if (File.Exists(filename))
{
string tempJson = System.IO.File.ReadAllText(filename);
var settings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Objects
};
IList parsedEntities = (IList)JsonConvert.DeserializeObject<List<Entity>>(tempJson, settings);
foreach (Entity e in parsedEntities)
{
//e.Initialise();
Map.Get(e.Coord).Entity = e;
}
}
This code compiles but then has a runtime exception in the Unity 5 editor.
(log below)
This code happens to be common with my editor which is a windows forms project and it runs fine when compiled by visual studio.
My Newtonsoft.Json.dll
says that it is version 7.
Is this a problem with the Unity compiler? Are certain parts of Newtonsoft Json DLL uncompatible with unities version of .Net? How should I write this to be compatible with Unity?
Log:
JsonSerializationException: Could not load assembly 'MO1Common'. Newtonsoft.Json.Serialization.DefaultSerializationBinder.GetTypeFromTypeNameKey (TypeNameKey typeNameKey) Newtonsoft.Json.Utilities.ThreadSafeStore
2[Newtonsoft.Json.Serialization.DefaultSerializationBinder+TypeNameKey,System.Type].AddValue (TypeNameKey key) Newtonsoft.Json.Utilities.ThreadSafeStore
2[Newtonsoft.Json.Serialization.DefaultSerializationBinder+TypeNameKey,System.Type].Get (TypeNameKey key) Newtonsoft.Json.Serialization.DefaultSerializationBinder.BindToType (System.String assemblyName, System.String typeName) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolveTypeName (Newtonsoft.Json.JsonReader reader, System.Type& objectType, Newtonsoft.Json.Serialization.JsonContract& contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.String qualifiedTypeName) Rethrow as JsonSerializationException: Error resolving type specified in JSON 'MO1.Definitions.Entities.Charactor, MO1Common'. Path '[0].$type', line 1, position 57. Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolveTypeName (Newtonsoft.Json.JsonReader reader, System.Type& objectType, Newtonsoft.Json.Serialization.JsonContract& contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.String qualifiedTypeName) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadMetadataProperties (Newtonsoft.Json.JsonReader reader, System.Type& objectType, Newtonsoft.Json.Serialization.JsonContract& contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue, System.Object& newValue, System.String& id) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (IList list, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonArrayContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id)
Upvotes: 1
Views: 916
Reputation: 394
I fixed this by adding this code:
#If Unity
tempJson = tempJson.Replace("MO1Common", "Assembly-CSharp")
#EndIf
This requires adding the IfDef "Unity" to the unity project.
The problem is that Json.Net was trying to reference the assembly library "MO1Common" which I had set up in visual studio, however, Unity compiles all of the classes in its "scripts" folder into one default assembly library called "Assembly-CSharp". Simply replacing the name of the assembly library in the Json string when the code is being compiled by Unity fixes this problem.
Upvotes: 1