Search Unity

Newtonsoft Json package

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

  1. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    66
    So now that Unity has brought in Newtonsoft.Json.dll in their own package, the docs say:
    "To avoid assembly clashes, please use this package if you intend to use Newtonsoft Json."​

    But the docs for the package say:
    "This is a package intended for internal Unity Development Projects and as such this package is not supported. Use at your own risk."

    So is this package going to be supported now that we're forced to use it?

    (Also it's spelt wrong on https://docs.unity3d.com/Packages/com.unity.nuget.newtonsoft-json@2.0/manual/index.html)

    I should note that I'm using Unity 2019.3.4f1 but it seems the package is for 2020?
     
    Novack, ImpossibleRobert and ETGgames like this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,997
    HugoStudica likes this.
  3. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    66
    It's a new package (com.unity.nuget.newtonsoft-json) which is a dependency of Platforms (com.unity.platforms@0.2) which is a dependency of Entities (com.unity.entities@0.7)
     
    Novack and ETGgames like this.
  4. multivac

    multivac

    Joined:
    Oct 27, 2009
    Posts:
    133
    It's a bit uncouth of Unity to force their own version of Newtonsoft Json as a dependency.

    It's pretty easy to keep using your own version though:
    1. Move the "com.unity.platforms" package from Library/PackageCache to your Packages folder
    2. Remove "com.unity.nuget.newtonsoft-json": "1.1.2" from its dependencies in package.json
     
    Last edited: Mar 23, 2020
    Dr_SuiuS and labandunga like this.
  5. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    66
    What do you mean doesn't support IL2CPP? I haven't noticed any problems with it.
     
  6. multivac

    multivac

    Joined:
    Oct 27, 2009
    Posts:
    133
    I did encounter this unhelpful error in build after trying Unity's version : currently not available on il2cpp Line: -1


    I've seen this before when trying to use Newtonsoft's Json with AOT compilation.
    I didn't bother investigating too deeply what exactly was missing, and reverted back to https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347
    If you didn't encounter any issues, it might actually be fine for most use cases, I've edited my post above.
     
  7. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    66
    @multivac you were right, versions previous to 2.0-preview don't work with IL2CPP, which is pretty crazy.

    But now the package we have to use is a preview and internal package?
     
  8. Paul_Bronowski

    Paul_Bronowski

    Joined:
    Sep 3, 2014
    Posts:
    55
    @Unity package developers, please resolve this.
     
    PraetorBlue likes this.
  9. christophera_unity

    christophera_unity

    Unity Technologies

    Joined:
    Aug 16, 2018
    Posts:
    17
    Hi all,

    I'm sorry that you all have been experiencing issues with the com.unity.nuget.newtonsoft-json package. I am one of the developers that developed the initial package. Firstly, I'd like to provide some context as to why the package was introduced in the first place. I assume that we are all aware that Unity does not handle strong named assemblies (https://docs.microsoft.com/en-us/dotnet/standard/assembly/strong-named). This means that there can only be one copy of a specific assembly on a system, which is an issue when both packages and users may introduce their own copies of some popular library. Newtonsoft Json is one of these. This lead to the decision to create a package to serve as a common ground for other Unity packages.
    The package is marked as not supported because we don't want users to expect a level of support on par to an official Unity package. The package is supported and we will fix some Unity specific issues, but any issues with Newtonsoft Json itself should be directed back to the developers of the library.
    In no way are we taking away choice from the user. In fact by making this package a common dependency, users can use the local package override to override the newtonsoft json version for everything. Simply copy the package into your packages directory and replace the dll and the AOT dll. The link.xml file can also be modified to your needs. However, keep in mind that any change comes with the risk of breaking things.
    As of version 2, we have IL2CPP support similar to the one here https://github.com/jilleJr/Newtonsoft.Json-for-Unity
    Version 2 will be out of preview soon with support for all Unity platforms. I believe it's using the latest version of newtonsoft json.

    The spelling mistake has been fixed and will be included in the next update!
     
    Last edited: May 6, 2020
  10. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    66
    @christophera_unity I have no problem with Unity providing the library (I certainly wouldn't want different copies in my project), but I do have a problem with it being labelled as unsupported and internal. You know people are going to have to put in considerable effort to remove/replace it - not to mention that Unity packages would then be relying on an external json package, surely you aren't going to say that's a supported scenario.

    What I would like to see:
    * Remove the "unsupported" and "internal use" and "use at own risk" texts and replace it with a more friendly reminder that it's a third party library.
    * Since you have made source changes for the AOT version, make the source available online so people can see it and contribute issues/fixes - again you can have some indication here it's a third party dll, but now we are relying on Unity for updates from the original package.
     
  11. christophera_unity

    christophera_unity

    Unity Technologies

    Joined:
    Aug 16, 2018
    Posts:
    17
    @SLGSimon your suggestions and issues have been taken into account. So, a quick update: we have begun some internal discussions to try find the optimal solution to this issue for both Unity and users. This will probably take some time, but I'll update this thread once we find an adequate solution.
    For now, a temporary solution would be to rename the namespace and assembly of newtonsoft json. This would allow you to have your own copy and would avoid any collisions with the package.
     
    noio and Xarbrough like this.
  12. kukurenaiz

    kukurenaiz

    Joined:
    Sep 12, 2017
    Posts:
    53
    manually putting
    Code (CSharp):
    1.     "com.unity.nuget.newtonsoft-json": "2.0.0"
    into the manifest.json seems to work, I'm using 2019.2, not sure about the implications tho
     
    user012345 and Visartech like this.
  13. christophera_unity

    christophera_unity

    Unity Technologies

    Joined:
    Aug 16, 2018
    Posts:
    17
    @Yemnefer The package supports 2018.4 and above as long as NET4 or Net Standard 2.0 is used. There should be no issues using in on 2018.4 and above. We cannot retroactively verify on older versions, so the package is only verified on 2020.1+.
     
    codestage and kukurenaiz like this.
  14. kukurenaiz

    kukurenaiz

    Joined:
    Sep 12, 2017
    Posts:
    53
    good to know, thank you very much.
     
    christophera_unity likes this.
  15. Skotrap7

    Skotrap7

    Joined:
    May 24, 2018
    Posts:
    98
    It looks like the burst preview pacakge 1.3.0-preview.12 has newtonsoft JSON in its .Runtime forlder, which is causing issues when using something that depends on the com.unity.nuget.newtonsoft-json package.

    How would I resolve this correctly?
     
  16. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    66
    The Burst .Runtime folder is outside of Unity, it's a separate environment for the Burst command line tool. What problems are you having?
     
  17. Skotrap7

    Skotrap7

    Joined:
    May 24, 2018
    Posts:
    98
    If you need it, I can set up another project again and give you a screen shot about the error, but basically, Unity reports a compiler error that there are two conflicting versions of NewtonSoft.Json.dll. One is in the com.unity.nuget.newtonsoft-json package, and one is in the burst preview pacakge 1.3.0-preview.12, in the .Runtime folder.

    I didn't need the latest burst preview, so I went through my packages and chose earlier versions that depend on the verified burst 1.2.3 and the problem went away. Also, in burst 1.2.3 the .Runtime folder does NOT contain the Newtonsoft dll. I didn't check every version between 1.2.3 and the latest preview, but this appears to be a new addition. Regardless, as far as package management goes (especially with the strong named assemblies) burst should probably depend on the com.unity.nuget.newtonsoft-json package instead of providing its own.

    If it isn't clear, this is not about Burst actually running, as I'm sure there is no issue in the CLI tool, this is about Unity compilation errors when you have these conflicts in you're packages.

    Let me know if you need that example project, happy to help.
     
  18. XRA

    XRA

    Joined:
    Aug 26, 2010
    Posts:
    248
    **EDIT** I managed to rebuild the dll's to use the latest newtonsoft Json & it accepts the dll from test-framework.performance

    Is there a way to fix this error? I had to remove the newtonsoft.Json that some addons are using in my project, because in 2020.x it gets stuck in a loop trying to open the editor (editor log has repeated spam about dll being the same name).

    But now those addons don't work with the dll that comes in from com.unity.collections which I need to use. ( com.unity.collections pulls in
    "com.unity.test-framework.performance": "2.3.1-preview", )

    references strong named Newtonsoft.Json in a different folder, versions has to match. Assembly references: 11.0.0.0 Found in project: 12.0.1.0.
     
    Last edited: Oct 15, 2020
  19. ScottAdams

    ScottAdams

    Joined:
    Nov 23, 2016
    Posts:
    32
  20. Flindt

    Flindt

    Joined:
    Dec 28, 2016
    Posts:
    78
    is it possible to use this "official" unity packaged version of json - with unity 2019 - and IOS/IL2CPP?

    Thanks
     
  21. pizzaclubgames

    pizzaclubgames

    Joined:
    Oct 22, 2020
    Posts:
    3
    Related to this: We have common dlls that rely on Newtonsoft for Unity. I am happy to move our common code over to new the new com.unity.newtonsoft package but when I try to add https://packages.unity.com/ to Visual Studio as a package source, it didn't work. I know I can manually copy the dll into my projects, but it would be great if unity packages could be imported through Visual Studio so we can stay up to date easily.
     
  22. pizzaclubgames

    pizzaclubgames

    Joined:
    Oct 22, 2020
    Posts:
    3
    I am receiving this error when importing a dll which references the unity newtonsoft library. This seems to be because Unity is looking at the "fileVersion" not the "assemblyVersion" of the dll. fileVersion is 12.0.301.0 and assemblyVersion is 12.0.0.0. Please look into either changing the package to match the fileVersion and assemblyVersion or fix it so that Unity can resolve the same library by checking assemblyVersion instead of fileVersion. Thanks!


    Unable to update following assemblies:Assets/Plugins/lib/Common.dll (Name = Common, Error = 131) (Output: C:\Users\hunni\AppData\Local\Temp\tmp48bb8865.tmp)

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
     
  23. Futtureman

    Futtureman

    Joined:
    Jun 4, 2015
    Posts:
    4
    I am currently using Unity Perception Package which depends on "com.unity.nuget.newtonsoft-json". How can I use this version of json.net in my scripts? I can't give assembly reference to this package.

    Also if import my Json.Net package it causes reference conflict
     
  24. distastee

    distastee

    Joined:
    Mar 25, 2014
    Posts:
    58
    I had the same issue. Upgrading to 2.0.1-preview1 fixed it for me
     
  25. user012345

    user012345

    Joined:
    Nov 10, 2020
    Posts:
    2
    How can I add "com.unity.nuget.newtonsoft-json" as dependency to package.json of my NPM-package?
    As it's Git-package, common format
    obviosly doesn't work.
    But when I compose it as for git-package:
    where
    d2fd6ed876261bbd82f59199ad711e7667bf633a
    is the revision id got from com.unity.nuget.newtonsoft-json package.json,
    it doesn't work under Unity too. And if I try to `npm install ...` this my npm-package manually, GitHub sing in window appears.

    Can it be dependency of any custom NPM-package at all?
     

    Attached Files:

  26. JLawrence7775

    JLawrence7775

    Joined:
    May 20, 2021
    Posts:
    1
    Would be very helpful if there were some actual documentation on how to reference this "built-in" library. The page referenced is great information with no value whatsoever. Those of us who don't live and breath Unity are exasperated by the lack of dependable documentation.
     
  27. jfrs20

    jfrs20

    Joined:
    Jul 8, 2020
    Posts:
    5
    So what's the deal now?

    I upgraded from 2020.3.9f1 to 2020.3.11f1, only to find that the upgrade process breaks because of a "Newtonsoft.Json.dll" I had in my Assets/Plugins folder. There are a lot of new directories in Library/PackageCache - are those dependencies intended?

    There's still a deprecation note at https://docs.unity3d.com/Packages/com.unity.nuget.newtonsoft-json@2.0/manual/index.html - can the package be used now or not? Can I rely on the package existing?

    Also I'm not sure about the short description about local package override above - where can I read more?

    Haven't find anything related in release notes of 2020.3.10f1 and 2020.3.11f1. Seems a bit surprising inside the LTS branch.
     
  28. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    666
    It totally depends on what your other packages are. If some package got updated to the latest verified version they may depend on the newtonsoft package. It might conflict with your own version that you have in the plugins folder.
    You can either delete the one in the plugins folder or you can override the newtonsoft package. To override it, just drag it from the PackageCache into the Packages folder. Then overwrite it with your version.
    I haven't found any reason to do this though. Unless you need some kind of fix in a later version of newtonsoft.

    The package itself is not deprecated. It doesn't even say so in the manual.
    The package is not supported by Unity as in if there are problems with the actual functionality of newtonsoft json, Unity won't help you with that. Because it is a third party library. So any problems with newtonsoft json should be reported at newtonsofts repository.

    I am using this package for quite some time. Haven't had a single problem with it.
     
    jfrs20 likes this.
  29. jfrs20

    jfrs20

    Joined:
    Jul 8, 2020
    Posts:
    5
    Thank you! I'll try whether the "new" package will work for me.

    The thing is, it comes as a new dependency of "com.unity.collab-proxy", which I somehow got included in my project, probably when I had created it. I don't know about those dependencies and haven't found where to read about it (package manager has not reference).

    I probably had used the Unity Newtonsoft package if I had known about it. OTOH I would've been in doubt how to do it. Does it need to be added manually by editing manifest.json or packages-lock.json? Again, I'm missing a bit of documentation.

    Funny thing: https://www.npmjs.com/package/com.unity.nuget.newtonsoft-json - had it been published there by accident or did someone really try to publish malicious code?
     
  30. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    666
    If you don't use Collab, you can remove the package.

    Well you still can. I mean you can simply remove any dll from your plugin folder and then install the package from Unity.

    If you want to override the package's version, simply drag the newtonsoft package folder from the PackageCache to the Packages folder. Return to Unity and it'll change the packages-lock automatically. It'll be served as "development" package.
    After that you can replace the dll

    Yeah that was a malicious attempt. It is even stated as such.
    If you want the newtonsoft package that Unity is using you can simply go into the package manager click the + then from git url and fill in "com.unity.nuget.newtonsoft-json" (without quotes).
    If any of Unity's packages are installed that have the newtonsoft package as dependency it'll already be installed. The package manager installs any required dependency of a package.
     
    jfrs20 likes this.
  31. jfrs20

    jfrs20

    Joined:
    Jul 8, 2020
    Posts:
    5
unityunity