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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

JSON .NET for Unity

Discussion in 'Assets and Asset Store' started by Dustin-Horne, Sep 13, 2013.

  1. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    This is quite the common AOT issue.

    If you're using my Newtonsoft.Json package (check my signature), the one from SaladLab, or Unity's internal Newtonsoft.Json UPM package then you can use the AotHelper from here.

    Code (CSharp):
    1. using Newtonsoft.Json.Utilities;
    2. using UnityEngine;
    3. public class AotTypeEnforcer : MonoBehaviour
    4. {
    5.     public void Awake()
    6.     {
    7.         AotHelper.EnsureList<float?>();
    8.     }
    9. }
    https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/Fix-AOT-using-AotHelper

    If you're using Dustin Horne's Newtonsoft.Json package, and really can't upgrade (his package is starting to show its age, and haven't been updated in years), then you're a bit out of luck. Maybe look into using link.xml, but it can be difficult to configure (https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/Fix-AOT-using-link.xml)
     
  2. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Yeah just skip the object writing part in your custom converter.

    Code (CSharp):
    1. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    2.     {
    3.         Level level = value as Level;
    4.         serializer.Serialize(writer, level.ID);
    5.     }
     
    blablaalb likes this.
  3. TonismoGames

    TonismoGames

    Joined:
    Jun 12, 2018
    Posts:
    109
    Wow great work around!Thank You
     
  4. junaidali-playvation

    junaidali-playvation

    Joined:
    Sep 24, 2020
    Posts:
    3
    Hi Oliver,

    I'm facing a similar issue as yours. I'm compiling some C# code outside unity in a DLL which makes some server calls and expects a JSON response. When I try to run and build for iOS, i get similar error (Unable to find a constructor to use for type). What's the possible solution to this, can you please elaborate?
     
  5. RSH1

    RSH1

    Joined:
    Jul 9, 2012
    Posts:
    250
    Trying to import this from the package manager gives me the error:

    Code (CSharp):
    1. Multiple precompiled assemblies with the same name Newtonsoft.Json.dll included or the current platform. Only one assembly with the same name is allowed per platform. (E:/Unity Projects/Ulti/Library/PackageCache/com.unity.nuget.newtonsoft-json@2.0.0/Runtime/Newtonsoft.Json.dll)
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Make sure there is no longer a JsonDotNet folder in your project and that no other assets embed a copy of Newtonsoft.Json.Dll. everyone should be using only the version from UPM as it's up to date and resolves issues with conflicts when it's required by other Unity assets.
     
    xofreshxo and ModLunar like this.
  7. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    It's the end of an era. In Unity 2021 the com.unity.purchasing package depends on the com.unity.nuget.newtonsoft-json package which means the JSON. NET for Unity plugin has to go.
     
  8. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    557
  9. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Unity has actually adopted my package, and have promised to keep it up to date with the upstream Newtonsoft.JSON whenever he makes any new releases.

    Read more: https://github.com/jilleJr/Newtonsoft.Json-for-Unity#-deprecation-warning

    Their long term plan is to switch over to System.Text.Json as they get the .NET version up to speed. By then Unity's ecosystem of JSON.NET packages will probably have a comeback, but that may be years from now so best plan is to focus on Unity's official JSON.NET package now.
     
  10. Slashbot64

    Slashbot64

    Joined:
    Jun 15, 2020
    Posts:
    233
    I hope not.. System.Text.Json is a gimped version of Newton.. maybe not as useless as the original Json stuff Unity added years ago... will just stick to the Newtonsoft one.
     
  11. jilleJr

    jilleJr

    Joined:
    Jan 21, 2015
    Posts:
    63
    Well to clarify, if Unity switches to using System.Text.Json internally instead of JSON.NET then we won't be locked to using a specific version/variant of JSON.NET. By then we could choose freely again which JSON.NET we'd want to use, as then they could remove the hard dependency on JSON.NET v13.0.1, allowing your specific project to import a v11.0.1 DLL or something.

    System.Text.Json is part of the standard library in the newer .NET versions, so for that they won't even need to lock it to a specific package once they get there.
     
  12. RandomCharacters

    RandomCharacters

    Joined:
    Nov 29, 2012
    Posts:
    262
    I just upgraded again t newest unity and again get errors after the upgrade (when will it ever end.) I have JSON.net 13.0.1 for unity installed jilleJr. Unity 2022.1.14.

    I remember something vaguely that there was an issue with a unity package. I thought it was version control. I thought that I deleted it and the conflict went away (I could be wrong). Version control says v1.4.2 is installed by is not being used. However, there is no way to delete it off the system as far as i can tell.

    The projects were fine before. Now I get this error. So does anyone have any ideas to fix these complile errors?

    -------------------------------
    Library\PackageCache\com.unity.services.core@1.4.2\Runtime\Core.Internal\Registry\DependencyTreeExtensions.cs(4,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)

    Library\PackageCache\com.unity.services.core@1.4.2\Runtime\Core.Internal\Registry\DependencyTreeExtensions.cs(51,24): error CS0246: The type or namespace name 'JObject' could not be found (are you missing a using directive or an assembly reference?)
    -----------------------------
     
    Qbit86 likes this.
  13. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    As @andymads pointed out, the era has sadly ended and Json .Net for Unity is no longer officially supported. You need to use the version Unity provides via the package manager.

    It felt great being the first developer and publisher to get Json .Net working (clear back in Unity 3 I believe) across all supported platforms. It was an honor seeing it used from indie devs to reasonably big titles from PC to mobile to consoles. And it's wonderful that Unity now has an officially supported version.

     
  14. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    System.Text.Json in .net core is actually very good. It only misses a couple features that Json .Net has, and it's far more efficient and less allocatey.
     
  15. Slashbot64

    Slashbot64

    Joined:
    Jun 15, 2020
    Posts:
    233
    yeah well those couple of features were the ones I found very useful the last time I looked into it (thinkit was some of the attribute stuff). As for efficient and less allocatey.. I mean if the kind of json I was using was required to be highly performant I'd just use something else entirely... lazy of MS not to just properly feature complete things really.
     
  16. RandomCharacters

    RandomCharacters

    Joined:
    Nov 29, 2012
    Posts:
    262
    Dumb question. In the packages area, so I delete json.net 13.0.1 for unity (it's this one right) or newtonsoft json.

    Also, after I do, what changes to the code would I need to do? I am assuming there will be errors.