From 77650d16a9cc66fdfe393320164cd8248cdff389 Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 3 Oct 2024 20:51:53 +0200 Subject: More reliable room name fetching, disable room predecessor in creation content schema --- LibMatrix/StateEvent.cs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'LibMatrix/StateEvent.cs') diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs index 073d26d..8b44d2c 100644 --- a/LibMatrix/StateEvent.cs +++ b/LibMatrix/StateEvent.cs @@ -44,6 +44,7 @@ public class StateEvent { public string FriendlyTypeNamePlural => MappedType.GetFriendlyNamePluralOrNull() ?? Type; private static readonly JsonSerializerOptions TypedContentSerializerOptions = new() { + // We need these, NumberHandling covers other number types that we don't want to convert Converters = { new JsonFloatStringConverter(), new JsonDoubleStringConverter(), @@ -55,9 +56,6 @@ public class StateEvent { [SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")] public EventContent? TypedContent { get { - // if (Type == "m.receipt") { - // return null; - // } try { var mappedType = GetStateEventType(Type); if (mappedType == typeof(UnknownEventContent)) @@ -81,6 +79,18 @@ public class StateEvent { } } + public T? ContentAs() { + try { + return RawContent.Deserialize(TypedContentSerializerOptions)!; + } + catch (JsonException e) { + Console.WriteLine(e); + Console.WriteLine("Content:\n" + (RawContent?.ToJson() ?? "null")); + } + + return default; + } + [JsonPropertyName("state_key")] public string? StateKey { get; set; } @@ -254,4 +264,23 @@ public class StateEventContentPolymorphicTypeInfoResolver : DefaultJsonTypeInfoR } */ -#endregion \ No newline at end of file +#endregion + +/* +public class ForgivingObjectConverter : JsonConverter where T : new() { + public override T? Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options) { + try { + var text = JsonDocument.ParseValue(ref reader).RootElement.GetRawText(); + return JsonSerializer.Deserialize(text, options); + } + catch (JsonException ex) { + Console.WriteLine(ex); + return null; + } + } + + public override bool CanConvert(Type typeToConvert) => true; + + public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) + => JsonSerializer.Serialize(writer, value, options); +}*/ \ No newline at end of file -- cgit 1.5.1