Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Newtonsoft Serialize problems

Discussion in 'Scripting' started by Dendog126, Jan 21, 2023.

  1. Dendog126

    Dendog126

    Joined:
    Jul 2, 2021
    Posts:
    4
    Hey everyone!

    I am trying to build a .NET Web API for my unity project.

    I have created a class in .NET. I have built the directory where the class is stored into a dll and put it into my unity project. I can then reference the class I have implemented.

    Whenever I try to serialize this class to json with newtonsoft-json, it gives me the following error:

    TypeLoadException: Could not resolve type with token 01000012 (from typeref, class/assembly System.Diagnostics.DebuggerBrowsableAttribute, System.Runtime, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)

    I have looked around a bit and I've found out that it has something to do with the libraries. In unity I am using newtonsoft-json version 13.0.2. In .NET I am referencing newtonsoft version 13.0.1 and the system runtime is 7.0.0 .

    Some remarks: In dotnet the serialization works as it should, in unity too but only for classes I create within the project. The class in the dll won't get serialized

    Is it true it has something to do with the libraries? If so: How can I fix this? If not: What is the problem here?

    Thanks!
     
  2. Dendog126

    Dendog126

    Joined:
    Jul 2, 2021
    Posts:
    4
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Have you tried doing it the other way around?

    - Add the classes to a separate Assembly(-Definition) in Unity
    - Reference those classes in Unity
    - Use the DLL built by Unity (Found under Library/ScriptAssemblies) in your .NET Web API.

    This should ensure that you're at least using the proper version inside of Unity. And the .NET API should be able to handle that version as well.

    https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html
     
    Bunny83 likes this.
  4. Dendog126

    Dendog126

    Joined:
    Jul 2, 2021
    Posts:
    4
    Yup that worked like a charm! Thank you so much!
     
    Bunny83 and SF_FrankvHoof like this.