about summary refs log tree commit diff
path: root/LibMatrix.EventTypes/MatrixEventContent.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-05-31 13:41:40 +0200
committerEmma [it/its]@Rory& <root@rory.gay>2024-05-31 13:41:56 +0200
commit59016736ceb4aab3975fe429e226da38c239abf9 (patch)
treedfa44e65b7c027ea1b4a2aa2ce3de7e39d8badfe /LibMatrix.EventTypes/MatrixEventContent.cs
parentStart implementing new event system (diff)
downloadLibMatrix-59016736ceb4aab3975fe429e226da38c239abf9.tar.xz
Event serialisation fix
Diffstat (limited to 'LibMatrix.EventTypes/MatrixEventContent.cs')
-rw-r--r--LibMatrix.EventTypes/MatrixEventContent.cs48
1 files changed, 41 insertions, 7 deletions
diff --git a/LibMatrix.EventTypes/MatrixEventContent.cs b/LibMatrix.EventTypes/MatrixEventContent.cs
index c30ebb0..81b8c52 100644
--- a/LibMatrix.EventTypes/MatrixEventContent.cs
+++ b/LibMatrix.EventTypes/MatrixEventContent.cs
@@ -1,26 +1,60 @@
+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<MatrixEventContent>))]
 public class MatrixEventContent {
-    // <T> : MatrixEventContent where T : MatrixEventContent<T>, new() {
-    internal JsonNode _json = new JsonObject();
 
-    public static implicit operator MatrixEventContent(JsonNode json) => new(json);
+    [JsonExtensionData, JsonInclude]
+    public JsonObject InternalJson { get; set; } = new();
+
+    
+
+    public MatrixEventContent() { }
 
-    [JsonConstructor]
     public MatrixEventContent(JsonNode json) {
-        _json = json;
+        InternalJson = json.AsObject();
     }
 
-    public MatrixEventContent() { }
+    public static implicit operator MatrixEventContent(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 : MatrixEventContent, new() {
+        public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
+            // read entire object into a JsonObject
+            var json = JsonNode.Parse(ref reader);
+            return new T { InternalJson = json.AsObject() };
+        }
+
+        public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) {
+            value.InternalJson.WriteTo(writer);
+        }
+    }
+}
 
-    public JsonNode? this[string key] => _json[key];
+public class MatrixEventAttribute(string eventType, bool deprecated = false) : Attribute {
+    public string EventType { get; } = eventType;
+    public bool Deprecated { get; } = deprecated;
 }
\ No newline at end of file