-
-
Notifications
You must be signed in to change notification settings - Fork 44
Description
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!