-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added experimental fix for interface serialization
The communicator was failing to (de)serialize the communicator wrapper messages because it was unable to determine the type. I added a special converter for interfaces that includes the type name, so the serializer knows how to handle the JSON data.
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Yggdrasil.Network.Communication | ||
{ | ||
/// <summary> | ||
/// A converter for interfaces. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class InterfaceConverter<T> : JsonConverter<T> where T : class | ||
{ | ||
private const string TypePropertyName = "Type"; | ||
private const string DataPropertyName = "Data"; | ||
|
||
/// <summary> | ||
/// Writes the value to the JSON writer. | ||
/// </summary> | ||
/// <param name="writer"></param> | ||
/// <param name="value"></param> | ||
/// <param name="options"></param> | ||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteString(TypePropertyName, value.GetType().AssemblyQualifiedName); | ||
writer.WritePropertyName(DataPropertyName); | ||
JsonSerializer.Serialize(writer, value, value.GetType(), options); | ||
writer.WriteEndObject(); | ||
} | ||
|
||
/// <summary> | ||
/// Reads a value from the JSON reader. | ||
/// </summary> | ||
/// <param name="reader"></param> | ||
/// <param name="typeToConvert"></param> | ||
/// <param name="options"></param> | ||
/// <returns></returns> | ||
/// <exception cref="JsonException"></exception> | ||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
throw new JsonException(); | ||
|
||
reader.Read(); // Object start | ||
|
||
if (reader.TokenType != JsonTokenType.PropertyName || reader.GetString() != TypePropertyName) | ||
throw new JsonException(); | ||
|
||
reader.Read(); // Property name | ||
|
||
var typeName = reader.GetString(); | ||
var type = Type.GetType(typeName) ?? throw new JsonException($"Unable to find type: {typeName}"); | ||
|
||
reader.Read(); // Property name | ||
|
||
if (reader.GetString() != DataPropertyName) | ||
throw new JsonException(); | ||
|
||
reader.Read(); // Object start | ||
|
||
var result = JsonSerializer.Deserialize(ref reader, type, options) as T; | ||
|
||
reader.Read(); // Object end | ||
|
||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the specified type can be converted. | ||
/// </summary> | ||
/// <param name="typeToConvert"></param> | ||
/// <returns></returns> | ||
public override bool CanConvert(Type typeToConvert) | ||
{ | ||
return typeof(T).IsAssignableFrom(typeToConvert); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters