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. Dismiss Notice

Newtonsoft Json package

Discussion in 'Scripting' started by SLGSimon, Mar 9, 2020.

  1. mrekuc

    mrekuc

    Joined:
    Apr 17, 2009
    Posts:
    116
    how can i remove the newtonsoft plugin? I just upgraded to 2020.3 and now there is conflict. I cannot use the new one because another package requires an older version and will not work with the newer version.
     
  2. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    599
    @christophera_unity Did something change in Unity 2021.1.19f? I upgraded from 2021.1.18f to 2021.1.19f and the Unity editor automatically removes package references to Newstonsoft.Json. The removal then causes compile errors due to missing Newtonsoft.Json namespaces.

    I went to the release notes for 2021.1.19f1 and there is no mention of changes for Unity Newtsonsoft.Json integration. Is it a regression or new feature? It would be great if the release notes could be updated with the relevant change.
     
    macicka12 and DINmatin like this.
  3. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    599
    @JoshPeterson Josh, it would seem there is an undocumented change in the 2021.1.19 release. Are you aware of what changed?
     
    macicka12 likes this.
  4. markmozza

    markmozza

    Joined:
    Oct 16, 2015
    Posts:
    85
    Hi, Similar problem i have just upgraded from 2020.3.16 to 2020.3.18. Package is missing, completely broken the project, not sure what the right approach is to fixing as multiple places say different things.
     
    Tommy4421 and Carrotpie like this.
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Add the newtonsoft json package through the package manager. Since your project depends on it.
    + -> add by git url -> com.unity.nuget.newtonsoft-json

    or add it manually by editting the manifest.json
    "com.unity.nuget.newtonsoft-json": "2.0.0"


    It is likely that one of the packages first had a dependency on newtonsoft json. When you update unity, some packages may be updated as well and that package probably no longer depended on newtonsoft json. Thus that package is removed. If your project relies on it, you should add it yourself.
     
  6. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    599
    I add it and Unity strips it back out. @markmozza Is this the behavior you get?
     
  7. Chris9465

    Chris9465

    Joined:
    Nov 3, 2020
    Posts:
    21
    From my notes upgrading from 2019.4 to 2020.3 (and then to android build)
    • You will have to remove canvasrenderer component from textmeshpro objects
    • You will have to remove the newtonsoft plugin as Unity added the same package built in
    1. In my case, I had a DLL that I dropped into Unity to use Newtonsoft.Json. I didn't use the package manager to install it. After upgrading it gave me the error for duplicate assemblies existing. I deleted the DLL from my asset folder and verified the namespace using Newtonsoft.Json; still works.
    • You will have to re set the .net version to 4.x
    • Recommend upgrading visual studio editor package in unity package manager
    • Unity will dump your assemblies to another folder, they will have to be moved back into the right directory
    I will test upgrading to a version of 2021 and reply with my results:
    Okay so after upgrading to 2021.1.20, Newtonsoft namespace is unresolved with and without including my DLL.
    I'm going to wait until 2021 is built out some more, I don't need anything bleeding edge right now.
     
    Last edited: Sep 10, 2021
    stonstad likes this.
  8. markmozza

    markmozza

    Joined:
    Oct 16, 2015
    Posts:
    85
    This makes total sense and looks like it was this after all. Thanks for clearing that up. After adding i had no problems using it.
     
    stonstad likes this.
  9. markmozza

    markmozza

    Joined:
    Oct 16, 2015
    Posts:
    85
    After following Chris's post i had no problems using it.
     
    Chris9465 and stonstad like this.
  10. laultman7775

    laultman7775

    Joined:
    Jul 13, 2021
    Posts:
    2
    Regardless of the technical issues, I really don't care. I need it to work. If you want it to work you must get the package into your project.
    Here is my README.md file.
    Hope it helps.

    Intact uses the Unity fork of Newtonsoft.Json that has been modifed to work with the il2cpp. (Instructions below.)

    **When creating a new project that will use Intact's script set it is best to install the Unity Newtonsoft.Json first.**
    This is because our scripts will not compile without the Newtonsoft.Json being already added to the package registry
    in Unity.



    **!!!!!!! Important Notes and Usage Updates !!!!!!!**
    The instructions have changed.
    After downloading the unity package to your local machine you need to have the intended project open.
    The process will add a new scope to your project Package Manager registry.
    The instructions (last line) inform you that all errors must be corrected prior to applying the update. This can be a
    difficult task if you have used Newtonsoft liberally in your scripts. There is **no good answer** here. Comment out
    all instances of Newtonsoft in your code.
    The above is absolutely true, it will NOT install until all script errors are resolved.

    **New Project** - Adding Unity Newtonsoft.Json
    Step 1: Follow the link below and download the latest version of the Unity Newtonsoft.Json fork.
    Step 2: With your project open and active window, double-click the unity package downloaded earlier. This will cause Unity to run the import process, click the Import button.

    **Existing Project** - Adding Unity Newtonsoft.Json
    If your existing project needs Newtonsoft.Json and you currently do not have any scripts referencing the Newtonsoft.Json, then you should follow the New Project steps above before adding scripts that require the Json libraries.

    **Upgrading Existing Project** that uses Newtonsoft.Json
    Basic how to fix a broken project.
    Starting with version 2020.3.18f1 Unity removes the old package Newtonsoft.json from your project but **does NOT replace** it with the new Unity fork. This causes your scripts that have registered Newtonsoft.Json to have an unresolved reference and the script will not compile.

    Step 1: Follow the link below and download the latest version of the Unity Newtonsoft.Json fork.
    Step 2: With your project open and active window, double-click the unity package downloaded earlier. This will cause Unity to run the import process, click the Import button.
    Step 3: Check for script compile errors. If there are any, the installer will not complete. You must fix all script errors now for the installer to complete. See discussion below.

    Fixing Script Errors:
    The best way to locate the offending script(s) is to click on the Console tab and locate the error references. Double click a reference to go to the Visual Studio project file that has the error. Comment out all calls to the Newtonsoft package. This can be tedious and can have cascading effects. There is no good solution here.

    After making all your changes, save all modified files in Visual Studio. Then return to your Unity project. This will cause Unity to attempt to recompile your scripts. If it compiles successfully then the import script will complete and install the necessary new Unity Newtonsoft fork. After the successful install return to Visual Studio and undo all the comments to enable your scripts.

    Verifying Import success:
    In the console there will be two information lines (!) about the Needle Installer. Look for success and delete of the installer.



    The Unity instructions to install are found on GitHub at: https://github.com/jilleJr/Newtonsoft.Json-for-Unity

    ======== Repeated here: see site for latest link =======
    Click here to download Install-jillejr.newtonsoft.json-for-unity-13.0.102.unitypackage

    Open the downloaded .unitypackage file in Unity. Easiestly done by drag'n'dropping the file into the Unity window. (This did not work for me. Do this instead: double-click the package to execute the import.)

    Click "Import" to import it all.

    Once the installer has successfully compiled, it will add the correct UPM registry and package to your project, followed by removing itself.

    The installer **does not run until your project can successfully compile**. Make sure to resolve all syntax- and other compiling errors, even just temporarily, so that the installer may execute.
     
  11. SuperDuperPooperGames

    SuperDuperPooperGames

    Joined:
    Sep 28, 2017
    Posts:
    47
    So I am using the jillejr version. I do not care which to use as I really don't know the differences. However, after upgrading to unity 2021.2.0, the editor now gives the following errors.

    ====================
    GUID [6c694cfdc33ae264fb33e0cd1c7e25cf] for asset 'Packages/jillejr.newtonsoft.json-for-unity/Plugins/Newtonsoft.Json AOT/Newtonsoft.Json.dll' conflicts with:
    'Packages/com.unity.nuget.newtonsoft-json/Runtime/AOT/Newtonsoft.Json.dll' (current owner)
    We can't assign a new GUID because the asset is in an immutable folder. The asset will be ignored.

    GUID [8c4bfcb5b17948478ccb955bccff9652] for asset 'Packages/jillejr.newtonsoft.json-for-unity/Plugins/Newtonsoft.Json Editor/Newtonsoft.Json.dll' conflicts with:
    'Packages/com.unity.nuget.newtonsoft-json/Runtime/Newtonsoft.Json.dll' (current owner)
    We can't assign a new GUID because the asset is in an immutable folder. The asset will be ignored.
    =========================
    I am assuming it is conflicting with the unity one. I see you said there is a way to disable the unity but can not figure it out. Could you be more specific (as in step by step).
     
    unity_SBt0djhrxNe9mA likes this.
  12. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    You replied to a year old post. The most recent post, with the steps you requested, is right above yours. I haven't tested it, but looks complete.
     
  13. SuperDuperPooperGames

    SuperDuperPooperGames

    Joined:
    Sep 28, 2017
    Posts:
    47
    Thanks for the reply. Yeah I did read that but there are no steps there. I have been deleting and messing around with this for days. After restarting unity the unity json gets rebuilt and gives errors again.
     
  14. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
  15. SuperDuperPooperGames

    SuperDuperPooperGames

    Joined:
    Sep 28, 2017
    Posts:
    47
    Thanks for the reply but I have read that several times. It is not the issue I am having ad doing what it says does not fix anything. But thanks for trying to help anyways.

    The issue I think was that "version control" unity package used the newtonsoft json package as a dependacy and thus would not allow it to be removed. After deleting the version control package then the unity newtonsoft json went away and no errors. The jjilljr version 13 is the only one there. I think.
     
    Last edited: Nov 5, 2021
  16. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    80
    It's disappointing that this is still a problem, either you have to include a duplicate Newtonsoft.Json package like jillejr's, or use the unity provided one and just expect Unity to tell you to get lost if anything goes wrong.
     
    Carrotpie likes this.
  17. WallaceHS

    WallaceHS

    Joined:
    Aug 7, 2021
    Posts:
    9
    Hello!
    This is not a simple task to me. In other words, I dont know how to do this.

    And I am receiving the error: "Multiple precompiled assemblies with the same name Newtonsoft.Json.dll included or the current platform."

    Could you help me, pls?

    Thank you much!
     
  18. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    869
    I tried deleting the newton package using the package manager, but unity keeps putting it back. I even tried removing the libraries folder, but same result.
     
  19. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    You don't need to delete it from the package manager, delete the files giving the error. If you click on the error it should show you, i'm pretty sure theres 2, one in a folder, and one outside it. I've had this error on two separate projects and fixed it this way.
     
  20. Carrotpie

    Carrotpie

    Joined:
    Sep 25, 2014
    Posts:
    29
    So it's been a few years and people still fight this cancer of a package that Unity insists on keeping. For gods sake, just change the namespace, or do something so that people stop wasting time scratching their heads for a workaround which shouldn't be even an issue.

    Year 2022 and I'm fighting the same stupid newtonsoft json references error as people did back in 2018, this time with an unremovable 12th version, whereas my packages (Microsoft.ML dll kit) need version 10. Can I have both somehow???

    From this whole thread it seems that most people either gave up and moved on with their lives :confused:
     
    Stormer2020, Vuda, Anoa and 1 other person like this.
  21. Chris9465

    Chris9465

    Joined:
    Nov 3, 2020
    Posts:
    21
    Changing the namespaces seems like a pretty easy and straightforward thing they could do. I wonder why they haven't
     
  22. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
  23. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Newtonsoft is a third party dll. You don't just change namespaces.
    What you can do is move the package into the packages folder so it becomes a development package. Then replace the dlls with the version you need.

    Or you could go the other way around about this. It's the year 2022 and Microsoft.ML is still using an ancient old version of newtonsoft from 2017?
    Is there no update for Microsoft.ML that uses a more recent version of newtonsoft?
    You can also try overriding the package with an older version of newtonsoft (or newer version for that matter).

    Or have found a solution. I never had any problem with Unity's newtonsoft package. A lot of the times a simple override would do but there are also times where it is simply incompatible due to the fact that an other third party package is using an ancient old version of newtonsoft. In a PoC phase the third party package would simply be deemed as "not usable" in its current state.
     
    Mashimaro7 and Chris9465 like this.
  24. Chris9465

    Chris9465

    Joined:
    Nov 3, 2020
    Posts:
    21
    Ah silly me, always forgetting the obvious. If they were able to get permission to take the source code maybe they could make their own plugin. UnityEngine.Newtonsoft.Json or something
     
  25. Carrotpie

    Carrotpie

    Joined:
    Sep 25, 2014
    Posts:
    29
    Tried that. Breaks things, like everything else I tried.

    True, MS things often need a dependency bump. IMO in this case a bigger problem is how Unity handles assemblies, but what do I know, I'm not a pro in this area.

    As I see it, a real problem is not the exacly Unity's package usage only. Newtonsoft.Json is heavilly used everywhere in NuGet. If it was concise - Unity's package manager itself would host MS.ML.NET then perhaps it could manage the versions properly. However in the current state I'd need to use NuGet which is oblivious to what Unity has within. Hence two package managers erupt with screaming error logs about assemblies.

    Anyways, seems that Unity 2018 or similar worked xD perhaps I'll try that some time.

    You say 'In a PoC phase...deemed as "not usable" ', yet this is my PoC phase... and there seems to be three packages which I could use for my ML inference and all of them I've deemed, as you say, "not usable". Kinda in a pickle :D
     
  26. SynodicArc

    SynodicArc

    Joined:
    Mar 7, 2015
    Posts:
    3
    Going to post an "answer" here for how to get a custom JSON package in your project and overwrite the automatically installed one.
    • With your project open, place your JSON DLLs anywhere in your project’s Asset folder.
    • Inside <YourProject>\Library\PackageCache\com.unity.nuget.newtonsoft-json@2.0.0\Runtime, delete all the JSON DLLs and their meta files in each folder.
    This will cause Unity to use your own JSON DLLs instead of the built-in ones.

    This is not a perfect solution because this will not solve the problem for any users on source control, but it will solve it locally. Technically, you might be able to add the Unity PackageCache files to your source control, then delete the JSON DLLs, but this might not be permanent since Unity likes to regenerate files every so often when upgrading versions.
     
    AXimter likes this.
  27. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    Move the package out of
    PackageCache
    into
    PROJECT_ROOT/Packages
    folder and make the change there. Unity won't regenerate the package and you can include it in VC. Just don't expect seamless package update after that (since you modified the package).
     
  28. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    80
    I'm not sure I'd recommend changing this package though, since it's used internally by Unity packages, so only do so if you absolutely need to.
     
  29. ricardo-silva

    ricardo-silva

    Joined:
    Aug 24, 2012
    Posts:
    3
    Hi,

    I am using UnityEngine. Is it possible?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. ...
    4.  
    5. SOMECLASS someClass = JsonUtility.FromJson<SOMECLASS>(jsonString);
    6. ...
    7.  
     
  30. notunusual

    notunusual

    Joined:
    Jun 13, 2016
    Posts:
    12
    We are in the process of updating a project from 2020.2 to 2021.2 and are getting the following error on our usage of newtsonsoft json -

    error CS1701: Assuming assembly reference 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' used by 'Newtonsoft.Json' matches identity 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' of 'netstandard', you may need to supply runtime policy

    It's not clear how we're meant to resolve this.
     
  31. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    659
    Hello, is there a way to NOT Serialize properties? Thanks.
     
  32. Skotrap7

    Skotrap7

    Joined:
    May 24, 2018
    Posts:
    125
    Unity has their own philosophy on serialization that is not the commonly accepted serialization philosophy of the C# .NET community. Unity will serialize public FIELDS by default. If you want a property to be serialized, you will need to have it backed by a private field with the [SerializeField] attribute. i.e.

    Code (CSharp):
    1. [SerializeField]
    2. private int _someInt;
    3.  
    4. public int SomeInt { get => _someInt; private set => _someInt = value; }
    I do this frequently because I don't want public setters in a lot of cases, but the Editor's use of SerializedProperty is able to still set the values. This lets you write your code with the right level of access to other objects at runtime without fully removing your ability to set these fields via the editor.

    If you are trying to serialize an AutoProperty with Unity you simply cant. You should either use a public field (yuck) or use a property backed by a private field.

    There are many, many, many other nuances to Unity Serialization that you are going to need to learn and get used to, so I strongly recommend reading all about Unity Serialization here and here.
     
  33. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    659
    Hi, Thanks for the response. Yeah I am aware of this. I am not referring to unity editor serialization.

    My question is about the NewtonSoft Json package in this thread. It seems to serialize (when I need to save to disk) properties. Where as binary formatter and the old built in json serializer did not.

    with the newton json, if I do following, it serializes the property value (to 6) and can save to disk.

    Code (CSharp):
    1.  
    2.  
    3. private int _someInt=3;
    4.  
    5. public int MultipliedInt => _someInt * 2;
    6.  
    7.  
    I do ****NOT**** want this serialized. I am wondering if there is some json attribute or something (e.g. [DoNotSerialize])...
     
    Skotrap7 likes this.
  34. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Skotrap7 likes this.
  35. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    659
    Munchy2007 likes this.
  36. Slock20969

    Slock20969

    Joined:
    Nov 18, 2015
    Posts:
    15
    I feel like this should be in the docs. I had to Google and go through so many forum posts to figure out how to install it.
     
    adamgryu and FlightOfOne like this.
  37. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Yeah well, it is and always will be a third party dll package. Unity has other hidden packages as well. Unless you know about them from the forum you might never knew they existed. It's all due to this change since 2020.1: https://forum.unity.com/threads/visibility-changes-for-preview-packages-in-2020-1.910880/
    With that change the VectorGraphics package got hidden as well. One I actually use quite often for crisp looking UI.
     
  38. joolsa

    joolsa

    Joined:
    Mar 20, 2013
    Posts:
    10
    +1 for "this package needs a better way to be discovered & installed"
     
  39. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    863
    Hey, did you find a solution to this warning?
     
  40. notunusual

    notunusual

    Joined:
    Jun 13, 2016
    Posts:
    12
    Unity support told us to simply disable the warning by doing -

    upload_2022-7-29_13-56-55.png

    In the player settings.

    I wasn't pleased with this as an answer, but that's what we had to roll with.
     
  41. nemanjab

    nemanjab

    Joined:
    Jan 10, 2019
    Posts:
    12
    Hey, thank you for sharing this.
    Is it possible to keep for example this asset JSON .NET For Unity and update to unity 2020.3 version where newtonsoft is added automatically?
    When you said to rename the namespace and assembly of newtonsoft json, did you mean the third party asset like this?
    And do you have an advice on how would we do that?

    We are heavily heavily dependent on this asset JSON .NET For Unity and built in newtonsoft is not working on Android build, but it's working perfectly in editor. Working with AOT Helper would be impossible because we have a big json and instructions on how to work with AOT Helper are insufficient. And also, there is no way to debug it it... AOT Helper would work if we are making something from scratch.

    Thanks in advance!
     
  42. MaryamRasheed

    MaryamRasheed

    Joined:
    Jan 25, 2021
    Posts:
    6
    I have updated my unity editor to version 2021.3.10 and also getting this error on serializeobject in the android build while it is working absolutely fine on editor. What could be the issue?

    Thanks in advance!
     
  43. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    65
    Unity, I don't get it. This is open source software and an extremely common dependency for 3rd party plugins.

    Why don't you just use a different name for it internally if you need to pin a version; just call it "newtonsoft.unity_internal" or something and then reference it by namespace.

    Why must you create problems for everyone with this conflict, it's been nearly 5 years for something that can be solved with proper namespacing, no?
     
  44. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    It is stupid if any 3rd party plugin comes with their own json dll.
    If you'd have 2 different packages with both a json dll, you'd have the same problem.
    The fix is simple, but ppl just can't seem to figure it out on how to fix it.
    Apparently ppl need a really detailed step-by-step guide on how to solve this problem.

    This is not even a solution. You're creating a different problem, now it would be duplicate code in your project.
    The problem is that there are 2 separate instances of newtonsoft json. Delete the one from the third party package and just use the one Unity provides.

    Asset developers should just reference Unity's newtonsoft json package. Problem solved.
    It's there, it is available and it is no big mystery.
    I would love to see this package publically visible in the package manager instead hiding it and having to manually add it.

    Another option is to just not ship your asset with newtonsoft json at all. Just rely on the developer to provide it themselves. But you'll likely get complaints that your asset doesn't work because it is missing newtonsoft json and they don't know how to add that.
     
  45. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    65
    These are interesting points, and thank you for weighing in. I see two problems:

    1. I don't think it's that 3rd party plugins are necessarily "stupid," mostly, they're just targeting .net instead of Unity, or trying to be engine agnostic. .NET targetted libraries can often be incredibly useful to Unity devs, and Unity would do well to do what they can to allow inter-compatibility, especially when it's something easy like this.
    2. It also has to do with version pinning iiuc. Unity has chosen a specific (and old) version of newtonsoft json, which might not be what's being used by another plugin. It's easy enough for devs to try to consolidate dependency versions where they can if disk space is a major concern for them, but imo, having plugins just work out of the box is a much, much higher priority.
    I'm interested that you think the solution is simple, I haven't, so far, been able to get unity to play well with NuGet downloaded packages that use Newtonsoft.JSON by deleting one of the dlls. It usually breaks the reference, or causes weird problems. I suspect because of version mismatching. Any tips appreciated!
     
  46. Starbox

    Starbox

    Joined:
    Sep 17, 2014
    Posts:
    438
    Super novice question here, but from what I'm reading above, if there are different Newtonsoft.Json files in my Assets which would usually be brought by third party plugins (and perhaps even Google? I don't know), plus one in packages if I get it correctly, is the solution to go postal and delete all but one, even if they're not exactly named Newtonsoft.Json but could be named Newtonsoft_stuff.Json? How is Unity actually supposed to know which one to use? Is it scanning the entirety of the project?

    Within the Unity context, what is the one and only one Newtonsoft.Json that should be used, what is its (old?) version, and where is it exactly?
     
  47. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    With that thought I was talking about 3rd party plugins made specifically for Unity. Because that's what you usually look for, compatibility with Unity.
    When you bring in any NuGet packages, they're always a hit and miss. Not everything is made AoT compatible. Unity doesn't support everything in the .NET space either. You're bound to have some packages that just aren't compatible.
    But when any package or repository comes with their own Newtonsoft Json, you can just delete that and use the version that comes with Unity's package.
    If it is a specific old version that is required to work with this third party package, you can override Unity's package by making it a development package and replacing the dll's.

    The current version that is available from Unity's Newtonsoft Json package is version 13.0.1
    Looking at json.net the current version is 13.0.1.
    I don't see how that is old in any way.
    Even if it were getting stale you can update it yourself by making it a development package replacing the dll's.
    Talking about old, a Unity Asset Store asset that ppl are usually referring to is using version 8.0.3. That one is old.

    I don't know what kind of things you've done with NuGet packages but I'm not using them for compatibility reasons mentioned before.
    But if you have access to the NuGet packages' code you can create a custom package. Refer to Unity's Newtonsoft Json package in the package.json and assembly definition. Then delete any other newtonsoft dll's that came with it.
     
    CodeRonnie likes this.
  48. gwelkind

    gwelkind

    Joined:
    Sep 16, 2015
    Posts:
    65
    Really appreciate your thorough response here.

    >if it is a specific old version that is required to work with this third party package, you can override Unity's package by making it a development package and replacing the dll's.

    Is there a way to have both though? I mean, this seems like a more general problem and maybe I need to search separately, but it seems like there must be a way to reference two different versions of a .dll for different dependencies.

    I'm coming from the npm world where libraries can pin version-ranges of dependencies and the package manager will resolve both of them with as few versions as possible.
     
  49. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    not that I know.

    I'm not all too familiar with the npm / nuget package world. But wouldn't it select the most compatible version within the range that is set? If so that would mean it would be one single dll / package too.
    With assembly definitions you have version defines as well. You can probably do this inside the package and split up different versions of newtonsoft json. But I'd still suggest the latest version as it is just json text serialization / deserialization. Newtonsoft Json is not AoT, so you can't just pick it from the source and expect it to work with IL2CPP. There's a fork that made it compatible.

    As I mentioned before, easiest thing is to remove the newtonsoft json from any third party package and make a reference to unity's newtonsoft json package in the assembly definition (if there is one). Even cleaner is to make a custom package of it so it doesn't sit in your Assets folder but rather in the Packages folder. It takes a bit of extra effort.

    If the current updated version does not work with the third party package because it relies on a really old code of newtonsoft json. Code that no longer exists in the newer versions. Then you can make unity's package a development package and override the dll with an older one. But if that breaks any other package that relies on a newer version, you have an incompatible package. It would just be outside of the version range.
     
  50. BlackclawsK

    BlackclawsK

    Joined:
    Jan 9, 2019
    Posts:
    100
    I'd like to point out for anyone else running into this issue that the current 3.1 version that is force-shipped with 2023.1 does not have the correct public key token and therefore fails to link to some libraries that have strong name linking enabled. This was no issue in 3.0.2 as the public key token was intact there.

    Edit: You dont' even need to have strong name linking enabled in the third party library. This will reproduce it:


    Code (CSharp):
    1.  
    2. using Newtonsoft.Json;
    3.  
    4. namespace ClassLibrary2
    5. {
    6.     public class Class1
    7.     {
    8.         private readonly JsonSerializerSettings _settings;
    9.  
    10.         public Class1(JsonSerializerSettings settings)
    11.         {
    12.             _settings = settings;
    13.         }
    14.  
    15.         public static Class1 Create(JsonSerializerSettings settings = default)
    16.         {
    17.             return new Class1(settings);
    18.         }
    19.     }
    20. }
    Put this code in a project targeting netstandard 2.0, add Newtonsoft.Json 13.0.2 nuget package and put the resulting dll in a Unity project that has the 3.1 package.

    You now just need to call the function anywhere in your Unity script files to trigger an undefined reference error:

    Code (CSharp):
    1.  
    2. var classOne = Class1.Create(new JsonSerializerSettings());
    3.  
    I've already submitted this as a bug:

    https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-36676
     
    Last edited: Mar 28, 2023
    Starbox and Mashimaro7 like this.