| | 1 | | using NostrSure.Domain.Entities; |
| | 2 | | using NostrSure.Domain.ValueObjects; |
| | 3 | | using System.Runtime.CompilerServices; |
| | 4 | | using System.Text.Json; |
| | 5 | | using System.Text.Json.Serialization; |
| | 6 | |
|
| | 7 | | namespace NostrSure.Infrastructure.Serialization; |
| | 8 | |
|
| | 9 | | public sealed class NostrEventJsonConverter : JsonConverter<NostrEvent> |
| | 10 | | { |
| | 11 | | // Pre-allocated property name spans for faster comparison |
| 163 | 12 | | private static ReadOnlySpan<byte> IdPropertyName => "id"u8; |
| 141 | 13 | | private static ReadOnlySpan<byte> PubkeyPropertyName => "pubkey"u8; |
| 119 | 14 | | private static ReadOnlySpan<byte> CreatedAtPropertyName => "created_at"u8; |
| 97 | 15 | | private static ReadOnlySpan<byte> KindPropertyName => "kind"u8; |
| 75 | 16 | | private static ReadOnlySpan<byte> TagsPropertyName => "tags"u8; |
| 54 | 17 | | private static ReadOnlySpan<byte> ContentPropertyName => "content"u8; |
| 32 | 18 | | private static ReadOnlySpan<byte> SigPropertyName => "sig"u8; |
| | 19 | |
|
| | 20 | | // Valid EventKind values for fast lookup |
| 1 | 21 | | private static readonly HashSet<int> ValidEventKinds = new() |
| 1 | 22 | | { |
| 1 | 23 | | (int)EventKind.Note, |
| 1 | 24 | | (int)EventKind.ContactList, |
| 1 | 25 | | (int)EventKind.Zap |
| 1 | 26 | | }; |
| | 27 | |
|
| | 28 | | public override bool CanConvert(Type typeToConvert) |
| 242 | 29 | | { |
| 242 | 30 | | return typeof(NostrEvent).IsAssignableFrom(typeToConvert); |
| 242 | 31 | | } |
| | 32 | |
|
| | 33 | | public override NostrEvent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 22 | 34 | | { |
| 22 | 35 | | if (reader.TokenType != JsonTokenType.StartObject) |
| 0 | 36 | | ThrowInvalidJsonException(); |
| | 37 | |
|
| 22 | 38 | | string? id = null; |
| 22 | 39 | | string? pubkey = null; |
| 22 | 40 | | long? createdAt = null; |
| 22 | 41 | | int? kindInt = null; |
| 22 | 42 | | List<NostrTag>? tags = null; |
| 22 | 43 | | string? content = null; |
| 22 | 44 | | string? sig = null; |
| | 45 | |
|
| 22 | 46 | | var fieldsFound = 0; |
| | 47 | |
|
| 175 | 48 | | while (reader.Read()) |
| 175 | 49 | | { |
| 175 | 50 | | if (reader.TokenType == JsonTokenType.EndObject) |
| 20 | 51 | | break; |
| | 52 | |
|
| 155 | 53 | | if (reader.TokenType != JsonTokenType.PropertyName) |
| 0 | 54 | | ThrowInvalidJsonException(); |
| | 55 | |
|
| | 56 | | // Use ValueSpan for zero-allocation property name comparison |
| 155 | 57 | | var propertyNameSpan = reader.ValueSpan; |
| 155 | 58 | | reader.Read(); |
| | 59 | |
|
| 154 | 60 | | if (propertyNameSpan.SequenceEqual(IdPropertyName)) |
| 22 | 61 | | { |
| 22 | 62 | | id = reader.GetString(); |
| 22 | 63 | | fieldsFound++; |
| 22 | 64 | | } |
| 132 | 65 | | else if (propertyNameSpan.SequenceEqual(PubkeyPropertyName)) |
| 22 | 66 | | { |
| 22 | 67 | | pubkey = reader.GetString(); |
| 22 | 68 | | fieldsFound++; |
| 22 | 69 | | } |
| 110 | 70 | | else if (propertyNameSpan.SequenceEqual(CreatedAtPropertyName)) |
| 22 | 71 | | { |
| 22 | 72 | | createdAt = reader.GetInt64(); |
| 22 | 73 | | fieldsFound++; |
| 22 | 74 | | } |
| 88 | 75 | | else if (propertyNameSpan.SequenceEqual(KindPropertyName)) |
| 22 | 76 | | { |
| 22 | 77 | | kindInt = reader.GetInt32(); |
| 22 | 78 | | fieldsFound++; |
| 22 | 79 | | } |
| 66 | 80 | | else if (propertyNameSpan.SequenceEqual(TagsPropertyName)) |
| 21 | 81 | | { |
| 21 | 82 | | if (reader.TokenType == JsonTokenType.Null) |
| 1 | 83 | | { |
| 1 | 84 | | tags = new List<NostrTag>(); |
| 1 | 85 | | } |
| | 86 | | else |
| 20 | 87 | | { |
| 20 | 88 | | tags = ReadTagsOptimized(ref reader); |
| 19 | 89 | | } |
| 20 | 90 | | fieldsFound++; |
| 20 | 91 | | } |
| 45 | 92 | | else if (propertyNameSpan.SequenceEqual(ContentPropertyName)) |
| 22 | 93 | | { |
| 22 | 94 | | if (reader.TokenType == JsonTokenType.Null) |
| 1 | 95 | | { |
| 1 | 96 | | content = string.Empty; |
| 1 | 97 | | } |
| | 98 | | else |
| 21 | 99 | | { |
| 21 | 100 | | content = reader.GetString(); |
| 21 | 101 | | } |
| 22 | 102 | | fieldsFound++; |
| 22 | 103 | | } |
| 23 | 104 | | else if (propertyNameSpan.SequenceEqual(SigPropertyName)) |
| 22 | 105 | | { |
| 22 | 106 | | sig = reader.GetString(); |
| 22 | 107 | | fieldsFound++; |
| 22 | 108 | | } |
| | 109 | | else |
| 1 | 110 | | { |
| 1 | 111 | | reader.Skip(); |
| 1 | 112 | | } |
| 153 | 113 | | } |
| | 114 | |
|
| | 115 | | // Validate all required fields are present and handle nullable types properly |
| | 116 | | // We need at minimum: id, pubkey, created_at, kind, tags |
| | 117 | | // content and sig are always written but can be empty strings |
| 20 | 118 | | if (fieldsFound < 5 || |
| 20 | 119 | | id is null || pubkey is null || createdAt is null || |
| 20 | 120 | | kindInt is null || tags is null) |
| 0 | 121 | | { |
| 0 | 122 | | ThrowMissingRequiredFieldsException(); |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | // Fast EventKind validation using HashSet lookup |
| 20 | 126 | | if (!ValidEventKinds.Contains(kindInt!.Value)) |
| 0 | 127 | | ThrowUnknownEventKindException(kindInt.Value); |
| | 128 | |
|
| 20 | 129 | | var eventKind = (EventKind)kindInt.Value; |
| 20 | 130 | | var baseEvent = new NostrEvent( |
| 20 | 131 | | id ?? string.Empty, |
| 20 | 132 | | new Pubkey(pubkey ?? string.Empty), |
| 20 | 133 | | DateTimeOffset.FromUnixTimeSeconds(createdAt!.Value), |
| 20 | 134 | | eventKind, |
| 20 | 135 | | tags ?? new List<NostrTag>(), |
| 20 | 136 | | content ?? string.Empty, |
| 20 | 137 | | sig ?? string.Empty |
| 20 | 138 | | ); |
| | 139 | |
|
| | 140 | | // For ContactList events, return ContactListEvent with extracted contacts |
| 20 | 141 | | if (eventKind == EventKind.ContactList) |
| 5 | 142 | | { |
| 5 | 143 | | return ContactListEvent.FromNostrEvent(baseEvent); |
| | 144 | | } |
| | 145 | |
|
| 15 | 146 | | return baseEvent; |
| 20 | 147 | | } |
| | 148 | |
|
| | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 150 | | private static List<NostrTag> ReadTagsOptimized(ref Utf8JsonReader reader) |
| 20 | 151 | | { |
| 20 | 152 | | if (reader.TokenType != JsonTokenType.StartArray) |
| 0 | 153 | | ThrowInvalidJsonException(); |
| | 154 | |
|
| 20 | 155 | | var tags = new List<NostrTag>(); |
| | 156 | |
|
| 46 | 157 | | while (reader.Read()) |
| 46 | 158 | | { |
| 46 | 159 | | if (reader.TokenType == JsonTokenType.EndArray) |
| 19 | 160 | | break; |
| | 161 | |
|
| 27 | 162 | | if (reader.TokenType != JsonTokenType.StartArray) |
| 1 | 163 | | ThrowInvalidJsonException(); |
| | 164 | |
|
| | 165 | | // Pre-allocate list for tag elements |
| 26 | 166 | | var tagElements = new List<string>(); |
| | 167 | |
|
| 91 | 168 | | while (reader.Read()) |
| 91 | 169 | | { |
| 91 | 170 | | if (reader.TokenType == JsonTokenType.EndArray) |
| 26 | 171 | | break; |
| | 172 | |
|
| 65 | 173 | | if (reader.TokenType != JsonTokenType.String) |
| 0 | 174 | | ThrowInvalidJsonException(); |
| | 175 | |
|
| 65 | 176 | | var value = reader.GetString(); |
| 65 | 177 | | if (value is not null) |
| 65 | 178 | | tagElements.Add(value); |
| 65 | 179 | | } |
| | 180 | |
|
| | 181 | | // Only create NostrTag if we have at least one element (the tag name) |
| 26 | 182 | | if (tagElements.Count > 0) |
| 26 | 183 | | { |
| | 184 | | // Use NostrTag.FromArray for proper validation and construction |
| 26 | 185 | | tags.Add(NostrTag.FromArray(tagElements)); |
| 26 | 186 | | } |
| 26 | 187 | | } |
| | 188 | |
|
| 19 | 189 | | return tags; |
| 19 | 190 | | } |
| | 191 | |
|
| | 192 | | public override void Write(Utf8JsonWriter writer, NostrEvent value, JsonSerializerOptions options) |
| 9 | 193 | | { |
| 9 | 194 | | writer.WriteStartObject(); |
| | 195 | |
|
| | 196 | | // Write properties in optimal order (most common lookups first) |
| 9 | 197 | | writer.WriteString(IdPropertyName, value.Id); |
| 9 | 198 | | writer.WriteString(PubkeyPropertyName, value.Pubkey.Value); |
| 9 | 199 | | writer.WriteNumber(CreatedAtPropertyName, value.CreatedAt.ToUnixTimeSeconds()); |
| 9 | 200 | | writer.WriteNumber(KindPropertyName, (int)value.Kind); |
| | 201 | |
|
| | 202 | | // Inline tags serialization for better performance |
| 9 | 203 | | writer.WritePropertyName(TagsPropertyName); |
| 9 | 204 | | WriteTagsOptimized(writer, value.Tags ?? new List<NostrTag>()); |
| | 205 | |
|
| 9 | 206 | | writer.WriteString(ContentPropertyName, value.Content ?? string.Empty); |
| 9 | 207 | | writer.WriteString(SigPropertyName, value.Sig ?? string.Empty); |
| | 208 | |
|
| 9 | 209 | | writer.WriteEndObject(); |
| 9 | 210 | | } |
| | 211 | |
|
| | 212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 213 | | private static void WriteTagsOptimized(Utf8JsonWriter writer, IReadOnlyList<NostrTag> tags) |
| 9 | 214 | | { |
| 9 | 215 | | writer.WriteStartArray(); |
| | 216 | |
|
| 35 | 217 | | foreach (var tag in tags) |
| 4 | 218 | | { |
| 4 | 219 | | writer.WriteStartArray(); |
| | 220 | |
|
| | 221 | | // Write tag name first |
| 4 | 222 | | writer.WriteStringValue(tag.Name); |
| | 223 | |
|
| | 224 | | // Write tag values |
| 30 | 225 | | foreach (var value in tag.Values) |
| 9 | 226 | | { |
| 9 | 227 | | writer.WriteStringValue(value); |
| 9 | 228 | | } |
| | 229 | |
|
| 4 | 230 | | writer.WriteEndArray(); |
| 4 | 231 | | } |
| | 232 | |
|
| 9 | 233 | | writer.WriteEndArray(); |
| 9 | 234 | | } |
| | 235 | |
|
| | 236 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 237 | | private static void ThrowInvalidJsonException() => |
| 1 | 238 | | throw new JsonException("Invalid JSON format for NostrEvent"); |
| | 239 | |
|
| | 240 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 241 | | private static void ThrowMissingRequiredFieldsException() => |
| 0 | 242 | | throw new JsonException("Missing required NostrEvent fields"); |
| | 243 | |
|
| | 244 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 245 | | private static void ThrowUnknownEventKindException(int kind) => |
| 0 | 246 | | throw new JsonException($"Unknown event kind: {kind}"); |
| | 247 | | } |