diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index d8c163d3e..5e131e65e 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -327,9 +327,17 @@ private JToken CreateJObject(JsonReader reader) string s = (string)reader.Value!; // string that needs to be returned as a byte array should be base 64 decoded - if (objectType == typeof(byte[])) + if ( + objectType == typeof(byte[]) || + contract != null && + contract.NonNullableUnderlyingType + .GetMethods() + .Where(a => a.GetParameters().Length == 1 && (a.Name == "op_Explicit" || a.Name == "op_Implicit")) + .Select(a => a.GetParameters().Single()) + .Any(a => a.ParameterType == typeof(byte[])) + ) { - return Convert.FromBase64String(s); + return EnsureType(reader, Convert.FromBase64String(s), CultureInfo.InvariantCulture, contract, objectType); } // convert empty string to null automatically for nullable types