From 83f9a4df147ef58c884f43092527f5cb6fa2f0a9 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Thu, 7 Dec 2023 07:26:02 +0100 Subject: Temp state --- LibMatrix/StateEvent.cs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'LibMatrix/StateEvent.cs') diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs index 6d69820..6ca82f4 100644 --- a/LibMatrix/StateEvent.cs +++ b/LibMatrix/StateEvent.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Text.Json; using System.Text.Json.Nodes; @@ -29,26 +30,26 @@ public class StateEvent { return typeof(Dictionary); } - // var eventType = KnownStateEventTypes.FirstOrDefault(x => - // x.GetCustomAttributes()?.Any(y => y.EventName == type) ?? false); - var eventType = KnownStateEventTypesByName.GetValueOrDefault(type); - - return eventType ?? typeof(UnknownEventContent); + return KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent); } + private static readonly JsonSerializerOptions TypedContentSerializerOptions = new JsonSerializerOptions() { + Converters = { + new JsonFloatStringConverter(), + new JsonDoubleStringConverter(), + new JsonDecimalStringConverter() + } + }; + [JsonIgnore] - public EventContent TypedContent { + [SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")] + public EventContent? TypedContent { get { - if (Type == "m.receipt") { - return null!; - } + // if (Type == "m.receipt") { + // return null; + // } try { - var jse = new JsonSerializerOptions(); - jse ??= new JsonSerializerOptions(); - jse.Converters.Add(new JsonFloatStringConverter()); - jse.Converters.Add(new JsonDoubleStringConverter()); - jse.Converters.Add(new JsonDecimalStringConverter()); - return (EventContent)RawContent.Deserialize(GetType, jse)!; + return (EventContent)RawContent.Deserialize(GetType, TypedContentSerializerOptions)!; } catch (JsonException e) { Console.WriteLine(e); @@ -59,7 +60,7 @@ public class StateEvent { } set { if (value is null) { - RawContent = null; + RawContent?.Clear(); } else RawContent = JsonSerializer.Deserialize(JsonSerializer.Serialize(value, value.GetType())); } @@ -69,7 +70,7 @@ public class StateEvent { public string StateKey { get; set; } = ""; [JsonPropertyName("type")] - public string Type { get; set; } + public required string Type { get; set; } [JsonPropertyName("replaces_state")] public string? ReplacesState { get; set; } @@ -121,7 +122,7 @@ public class StateEvent { //debug [JsonIgnore] - public string dtype { + public string InternalSelfTypeName { get { var res = GetType().Name switch { "StateEvent`1" => "StateEvent", @@ -132,7 +133,7 @@ public class StateEvent { } [JsonIgnore] - public string cdtype => TypedContent.GetType().Name; + public string InternalContentTypeName => TypedContent?.GetType().Name ?? "null"; } @@ -152,9 +153,6 @@ public class StateEventResponse : StateEvent { [JsonPropertyName("event_id")] public string EventId { get; set; } - // [JsonPropertyName("user_id")] - // public string UserId { get; set; } - [JsonPropertyName("replaces_state")] public new string ReplacesState { get; set; } -- cgit 1.4.1