Skip to content

Commit 6bc4aad

Browse files
committed
return default struct value if the string is null
1 parent 84a6c3a commit 6bc4aad

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

Assets/Plugins/StreamChat/Core/Serialization/EnumeratedStructConverter.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace StreamChat.Core.Serialization
88
/// Json converter to serialize and deserialize <see cref="IEnumeratedStruct"/>
99
/// </summary>
1010
/// <typeparam name="TType">Specific type of the struct. This should be provided in a decorator</typeparam>
11-
internal class EnumeratedStructConverter<TType> : JsonConverter
11+
internal class EnumeratedStructConverter<TType> : JsonConverter
1212
where TType : struct, IEnumeratedStruct<TType>
1313
{
1414
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
@@ -31,15 +31,21 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
3131
switch (reader.TokenType)
3232
{
3333
case JsonToken.Null:
34-
return default;
34+
return default(TType);
3535
case JsonToken.String:
3636
{
3737
var str = reader.Value?.ToString();
38+
if (str == null)
39+
{
40+
return default(TType);
41+
}
42+
3843
var instance = Activator.CreateInstance<TType>();
3944
return instance.Parse(str);
4045
}
4146
default:
42-
throw new JsonSerializationException($"Unexpected token or value when parsing {objectType.FullName}");
47+
throw new JsonSerializationException(
48+
$"Unexpected token or value when parsing {objectType.FullName}");
4349
}
4450
}
4551

0 commit comments

Comments
 (0)