applejag/Newtonsoft.Json-for-Unity.Converters

Bug: UnityConverterInitializer registers too many converters

rumcgit opened this issue ยท 11 comments

Description

JsonConvert.SerializeObject() converts integers to strings, if we pass JToken !

Example

var str = "{\"a\":123}";
var jtoken = JsonConvert.DeserializeObject<JToken>(str);
var badSerializedString = JsonConvert.SerializeObject(jtoken);
var goodSerializedString = jtoken.ToString(Formatting.None);
Debug.Log(badSerializedString);  // outputs: {"a":"123"}
Debug.Log(goodSerializedString); // outputs: {"a":123}

Expected behavior

badSerializedString should be just the same as goodSerializedString: {"a": 123}

Versions

Newtnosoft.Json-for-Unity version: 12.0 (latest)
Unity version: 2019.1.14f1
Platforms affected: android, ios, webgl, editor

Hi! Thanks for reporting this!

I do not have a computer with Unity installed at the moment so can only try reproduce later, but I tried it in dotnet fiddle where it works as expected. This is intriguing!

Will get back to you once I've tested your example.

I am unable to reproduce this issue. I tried it with the following tests:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

namespace Tests
{
    public class Issue60
    {
        [Test]
        public void BadSerialized()
        {
            var str = "{\"a\":123}";
            var jtoken = JsonConvert.DeserializeObject<JToken>(str);
            var badSerializedString = JsonConvert.SerializeObject(jtoken);
            Assert.AreEqual(@"{""a"":""123""}", badSerializedString);
        }

        [Test]
        public void GoodSerialized()
        {
            var str = "{\"a\":123}";
            var jtoken = JsonConvert.DeserializeObject<JToken>(str);
            var goodSerializedString = jtoken.ToString(Formatting.None);
            Assert.AreEqual(@"{""a"":123}", goodSerializedString);
        }
    }
}

And received the results:

image

BadSerialized (0,089s)
---
Expected string length 11 but was 9. Strings differ at index 5.
  Expected: "{"a":"123"}"
  But was:  "{"a":123}"
  ----------------^

I ran the tests in a matrix of the following setups:

  • Editor and in a built player with IL2CPP
  • Unity 2019.1.14f1, 2019.3.14f1, and 2020.1.0b6
  • .NET 4.x and .NET Standard 2.0

I was unable to reproduce your issue in neither of those configuration combinations.


If you could supply a full repro of when this occurs then I can investigate further (that is, for example a minimalistic Unity project zipped and uploaded here), but as of right now I cannot help you as it works on my machine

Amazing! But using exactly your code I have the following ^) :
image

  • Windows 7 and Mac OS X
  • Unity 2019.1.14f1
  • .NET Standard 2.0

Maybe we use different json libraries? ))
Can you attach dlls here?

Which DLLs are you interested in?

Also, do you have any custom converters registered at all? Do you get this issue in a newly created project as well with ONLY these tests and the jillejr.newtonsoft.json-for-unity package added?

I tried to run it on an empty project, and the result is similar to yours. How can it be so ? Any ideas ?

My guess is that you have some JsonConverter that does it. If you upload your project to me I could investigate. I would love to get to the bottom of this, it's an interesting case. If you're worried about uploading it publicly here you can upload it to wetransfer.com or something and send it privately to my email if you're ok with that option.

I've found the reason!
The reason is in UnityConverterInitializer from Newtonsoft.Json-for-Unity.Converters . UnityConverterInitializer goes through all assemblies and register all converters in your code as default converters! I think that's not good idea, because the project can be very big, with dozens of custom logics. In my case I had a custom converter for a very specific case, but surprisingly it was registered as a default converter too.
I suggest to limit the list of auto-registered converters to the list of your library converters.

Ah! Did not realize you also used the converters package.
Yea I'm not too proud of how it registers converters. I copied the registering routine from Wanzyee Studios solution, but explicit is far better than implicit. Especially when you can't turn it off ๐Ÿ˜…

I'll move this issue over to that repo and see if I can get a fix implemented within reasonable time.

Nice debugging by the way!

@rumcgit There is a new preview out with an editor to enable/disable any given converter. Try it out already by updating to version 1.1.0-preview.2, then access the settings via "Edit > Json .NET converters settings..."

image

and then disabling your converter from the global default converters settings.

Example:

GIF 2020-07-16 20-37-31

Don't fray to reopen or create new issues if you have more troubles. :)