Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Getting weird conflicts with existing compiled libraries that use their own Newtonsoft library. #75

Open
nickhudson4 opened this issue Jun 27, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@nickhudson4
Copy link

nickhudson4 commented Jun 27, 2023

Expected behavior

The handling of types correctly.

Actual behavior

The UnityConverters package is severely affecting other libraries that also use Newtonsoft. The two libraries that I tested with were BestHTTP and StreamingClient. These libraries have no issues prior to importing UnityConverters. In general it seems to affect any library that uses newtonsoft.

Getting various type errors. For instance
Exception: Connection closed with an error. InvalidDataException: Expected 'type' to be of type Integer.
and
"Unexpected token Integer when parsing enum.", "stack": " at StreamingClient.Base.Util.StringEnumNameConverter.ReadJson (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue, Newtonsoft.Json.JsonSerializer serializer)

It is hard for me to debug since the errors are throwing in compiled 3rd party libraries. The second error for instance is being throw in a custom Newtonsoft JsonConverter. Here is the code in the library

public class StringEnumNameConverter : JsonConverter
    {
        /// <summary>
        /// Checks to see if this converter can work on the requested type.
        /// </summary>
        /// <param name="objectType">The type to convert to.</param>
        /// <returns>True if it can convert.</returns>
        public override bool CanConvert(Type objectType)
        {
            return objectType.IsEnum;   
        }

        /// <summary>
        /// Reads the raw json to parse to enum
        /// </summary>
        /// <param name="reader">The Json reader to read from.</param>
        /// <param name="objectType">The type being converted to.</param>
        /// <param name="existingValue">The current value.</param>
        /// <param name="serializer">The serializer being used.</param>
        /// <returns></returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.String)
            {
                var enumText = reader.Value?.ToString();
                return EnumHelper.GetEnumValueFromString(objectType, enumText);
            }

            //throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing enum.");
            return null;
        }

        /// <summary>
        /// Write out the enum as a string.
        /// </summary>
        /// <param name="writer">The writer being used.</param>
        /// <param name="value">The enum value.</param>
        /// <param name="serializer">The serializer being used.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            Enum e = (Enum)value;
            var name = EnumHelper.GetEnumName(value.GetType(), e);
            writer.WriteValue(name);
        }
    }

Steps to reproduce

  • Create a new project.
  • Import and test a library that uses their own version of Newtonsoft. The two libraries that I used were BestHTTP and StreamingClient.
  • Import Newtonsoft.Json-for-Unity.Converters
  • Attempt to use original libraries as they are intended

Thanks in advance!

@nickhudson4 nickhudson4 added the bug Something isn't working label Jun 27, 2023
@applejag
Copy link
Owner

Hello! Interesting find you got there. Could you add links to these other libraries?

And which version of these libraries did you use, and for what Unity version?

Also, please supply the C# object and enum you tried to serialize.

I'll take a look at this and try to reproduce it later, but the more elaborate and precise reproduction steps you provide, the faster it'll go.

Kind regards :)

@nickhudson4
Copy link
Author

Thanks for the quick response.

It is hard for me to get more info on the errors since they are happening in compiled 3rd party libraries. So I am unable to insert a debugger or anything like that.

I do believe I have figured out the issue although I am not so sure why it is causing the specific errors. Your library is adding the converters and other settings to the default NewtonsoftSerializationSettings. Which these 3rd party libraries are intercepting since they are in the same assembly. Which caused issues with conflicting converters or other setting.

My solution was to disable shouldAddConvertsToDefaultSettings in UnityConverterInitializer.cs and manually use the settings where I need it.

So maybe a suggestion would be to add that property to the config. Other than that I am not sure I would worry about it.

@EmilioCorvino
Copy link

EmilioCorvino commented Jul 25, 2023

Hello @nickhudson4,

Could you please provide further details on how you resolved the issue?

I seem to be facing a similar problem. In Unity 2021.28.f1, I installed the Newtonsoft package via nuget (despite it already being installed, as it didn't appear in the package manager). Then, I imported this package using the git UPM method.

However, when running the Android application build, I encountered an error related to an infinite loop with the normalize property when attempting to serialize a Vector3.

I suspect the problem may be related, as our project utilizes the MRTK package, which also incorporates Newtonsoft JSON at some point.

Newtonsoft Json version: 3.2.1 (13.0.2)
Json.NET Converters for Unity types version: 1.5.1

@EmilioCorvino
Copy link

EmilioCorvino commented Jul 25, 2023

Nevermind, apparently the package was getting stripped, despite me being on the last version and setting the stripping level to Minimal.

The solution described in this other issue worked.

Erifirin added a commit to Erifirin/Newtonsoft.Json-for-Unity.Converters that referenced this issue Feb 29, 2024
@Erifirin
Copy link
Contributor

I've fixed the issue by sorting a list of converter types as it described in the code comments))).
Created a pull request.

applejag added a commit that referenced this issue Mar 1, 2024
…assemblies (#90)

* fixed importing UnityConverters (included issue #75)

* fixed handling mscorlib

* Changed to store AssemblyName in ConverterConfig for better type lookups

* Remove Debug.Log & minor tweaks

* Cache result of assembly.GetName() to reduce allocations

* Rename types

* Removed excess typeByName dict

* Updated CHANGELOG

* Updated version.json

---------

Co-authored-by: Applejag <[email protected]>
Co-authored-by: George Gromov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants