about summary refs log tree commit diff
path: root/LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-06-01 19:02:28 +0200
committerEmma [it/its]@Rory& <root@rory.gay>2024-06-01 19:03:05 +0200
commita129b321998614b20e4ebb8a7c1632553ebee981 (patch)
treef4e16f7b1bbeffc21e81f8749e4980994242ff19 /LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs
parentEvent serialisation fix (diff)
downloadLibMatrix-bak-a129b321998614b20e4ebb8a7c1632553ebee981.tar.xz
Split event abstractions
Diffstat (limited to 'LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs')
-rw-r--r--LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs b/LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs
new file mode 100644

index 0000000..eba50a5 --- /dev/null +++ b/LibMatrix.EventTypes.Abstractions/BaseMatrixEventContent.cs
@@ -0,0 +1,54 @@ +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using ArcaneLibs.Extensions; + +namespace LibMatrix.EventTypes; + +// <T> : MatrixEventContent where T : MatrixEventContent<T>, new() { +/// <summary> +/// Extensible Event Content, aims to provide an API similar to JsonNode/JsonObject +/// <seealso cref="System.Text.Json.Nodes.JsonNode"/> +/// <seealso cref="System.Text.Json.Nodes.JsonObject"/> +/// </summary> +[JsonConverter(typeof(MatrixEventContentConverter<BaseMatrixEventContent>))] +// [JsonSerializable(typeof(MatrixEventContent))] +public class BaseMatrixEventContent { + public JsonObject InternalJson { get; set; } = new(); + + public BaseMatrixEventContent() { } + + public BaseMatrixEventContent(JsonNode json) { + InternalJson = json.AsObject(); + } + + public static implicit operator BaseMatrixEventContent(JsonNode json) => new(json); + + // public static implicit operator JsonNode(MatrixEventContent content) => content.InternalJson; + + [JsonIgnore] + public IEnumerable<string> EventTypes => this.GetType().GetCustomAttributes<MatrixEventAttribute>().Select(x => x.EventType); + + [JsonIgnore] + public string EventType => EventTypes.First(); + + public JsonNode? this[string key] => InternalJson[key]; + + public string ToJson() => InternalJson.ToJson(); + + + public class MatrixEventContentConverter<T> : JsonConverter<T> where T : BaseMatrixEventContent, new() { + public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + // read entire object into a JsonObject + // Console.WriteLine($"MatrixEventContentConverter<T>: Reading {typeToConvert}"); + var json = JsonNode.Parse(ref reader); + return new T { InternalJson = json.AsObject() }; + } + + public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) { + // Console.WriteLine($"MatrixEventContentConverter<T>: Writing {value.GetType()}"); + value.InternalJson.WriteTo(writer); + } + } +} \ No newline at end of file