| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Nodes; |
| | | 5 | | |
| | | 6 | | namespace Chronicler; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides methods for editing serialized payloads without fully deserializing them. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class SerializationPayloadEditor |
| | | 12 | | { |
| | | 13 | | #region JSON Editing |
| | | 14 | | |
| | 1 | 15 | | private static readonly JsonSerializerOptions JsonOptions = new() |
| | 1 | 16 | | { |
| | 1 | 17 | | IncludeFields = true |
| | 1 | 18 | | }; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Removes a property from a JSON payload at the specified path. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="json">The JSON payload.</param> |
| | | 24 | | /// <param name="path">The path to the property to remove.</param> |
| | | 25 | | /// <returns>The modified JSON payload.</returns> |
| | | 26 | | /// <exception cref="ArgumentException"></exception> |
| | | 27 | | public static string RemoveJsonProperty(string json, params string[] path) |
| | 12 | 28 | | { |
| | 12 | 29 | | if (path == null || path.Length == 0) |
| | 2 | 30 | | throw new ArgumentException("A JSON property path is required.", nameof(path)); |
| | | 31 | | |
| | 10 | 32 | | JsonObject root = ParseJsonRoot(json); |
| | 9 | 33 | | JsonObject parent = GetJsonParent(root, path); |
| | 9 | 34 | | parent.Remove(path[^1]); |
| | 9 | 35 | | return root.ToJsonString(JsonOptions); |
| | 9 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Sets a property value in a JSON payload at the specified path. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <typeparam name="T">The type of the value to set.</typeparam> |
| | | 42 | | /// <param name="json">The JSON payload.</param> |
| | | 43 | | /// <param name="value">The value to set.</param> |
| | | 44 | | /// <param name="path">The path to the property to set.</param> |
| | | 45 | | /// <returns>The modified JSON payload.</returns> |
| | | 46 | | /// <exception cref="ArgumentException"></exception> |
| | | 47 | | public static string SetJsonValue<T>(string json, T value, params string[] path) |
| | 7 | 48 | | { |
| | 7 | 49 | | if (path == null || path.Length == 0) |
| | 2 | 50 | | throw new ArgumentException("A JSON property path is required.", nameof(path)); |
| | | 51 | | |
| | 5 | 52 | | JsonObject root = ParseJsonRoot(json); |
| | 4 | 53 | | JsonObject parent = GetJsonParent(root, path); |
| | 3 | 54 | | parent[path[^1]] = JsonSerializer.SerializeToNode(value, JsonOptions); |
| | 3 | 55 | | return root.ToJsonString(JsonOptions); |
| | 3 | 56 | | } |
| | | 57 | | |
| | | 58 | | private static JsonObject ParseJsonRoot(string json) |
| | 15 | 59 | | { |
| | 15 | 60 | | JsonNode rootNode = JsonNode.Parse(json) |
| | 15 | 61 | | ?? throw new InvalidOperationException("Unable to parse JSON payload."); |
| | 14 | 62 | | return rootNode as JsonObject |
| | 14 | 63 | | ?? throw new InvalidOperationException("Expected JSON root object."); |
| | 13 | 64 | | } |
| | | 65 | | |
| | | 66 | | private static JsonObject GetJsonParent(JsonObject root, string[] path) |
| | 13 | 67 | | { |
| | 13 | 68 | | JsonObject current = root; |
| | 30 | 69 | | for (int i = 0; i < path.Length - 1; i++) |
| | 3 | 70 | | { |
| | 3 | 71 | | current = current[path[i]] as JsonObject |
| | 3 | 72 | | ?? throw new InvalidOperationException( |
| | 3 | 73 | | $"Expected JSON object at path segment '{path[i]}'."); |
| | 2 | 74 | | } |
| | | 75 | | |
| | 12 | 76 | | return current; |
| | 12 | 77 | | } |
| | | 78 | | |
| | | 79 | | #endregion |
| | | 80 | | |
| | | 81 | | #region MemoryPack Editing |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Removes a MemoryPack entry from a serialized payload at the specified path. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="data">The serialized MemoryPack payload.</param> |
| | | 87 | | /// <param name="path">The path to the entry to remove.</param> |
| | | 88 | | /// <returns>The modified serialized MemoryPack payload.</returns> |
| | | 89 | | /// <exception cref="ArgumentException"></exception> |
| | | 90 | | public static byte[] RemoveMemoryPackEntry(byte[] data, params string[] path) |
| | 12 | 91 | | { |
| | 12 | 92 | | if (path == null || path.Length == 0) |
| | 2 | 93 | | throw new ArgumentException("A MemoryPack entry path is required.", nameof(path)); |
| | | 94 | | |
| | 10 | 95 | | MemoryPackRecordEnvelope envelope = ReadEnvelope(data); |
| | 10 | 96 | | RemoveMemoryPackEntry(envelope, path, 0); |
| | 9 | 97 | | return MemoryPackSerializer.Serialize(envelope); |
| | 9 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Sets a MemoryPack entry value in a serialized payload at the specified path. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <typeparam name="T">The type of the value to set.</typeparam> |
| | | 104 | | /// <param name="data">The serialized MemoryPack payload.</param> |
| | | 105 | | /// <param name="value">The value to set.</param> |
| | | 106 | | /// <param name="path">The path to the entry to set.</param> |
| | | 107 | | /// <returns>The modified serialized MemoryPack payload.</returns> |
| | | 108 | | /// <exception cref="ArgumentException"></exception> |
| | | 109 | | public static byte[] SetMemoryPackValue<T>(byte[] data, T value, params string[] path) |
| | 6 | 110 | | { |
| | 6 | 111 | | if (path == null || path.Length == 0) |
| | 2 | 112 | | throw new ArgumentException("A MemoryPack entry path is required.", nameof(path)); |
| | | 113 | | |
| | 4 | 114 | | MemoryPackRecordEnvelope envelope = ReadEnvelope(data); |
| | 4 | 115 | | SetMemoryPackValue(envelope, path, 0, MemoryPackSerializer.Serialize(value)); |
| | 3 | 116 | | return MemoryPackSerializer.Serialize(envelope); |
| | 3 | 117 | | } |
| | | 118 | | |
| | | 119 | | private static MemoryPackRecordEnvelope ReadEnvelope(byte[] data) |
| | 16 | 120 | | { |
| | 16 | 121 | | return MemoryPackSerializer.Deserialize<MemoryPackRecordEnvelope>(data) |
| | 16 | 122 | | ?? new MemoryPackRecordEnvelope(); |
| | 16 | 123 | | } |
| | | 124 | | |
| | | 125 | | private static void RemoveMemoryPackEntry(MemoryPackRecordEnvelope envelope, string[] path, int depth) |
| | 11 | 126 | | { |
| | 11 | 127 | | if (depth == path.Length - 1) |
| | 9 | 128 | | { |
| | 9 | 129 | | envelope.Entries.Remove(path[depth]); |
| | 9 | 130 | | return; |
| | | 131 | | } |
| | | 132 | | |
| | 2 | 133 | | if (!envelope.Entries.TryGetValue(path[depth], out byte[]? nestedData) |
| | 2 | 134 | | || nestedData == null) |
| | 1 | 135 | | { |
| | 1 | 136 | | throw new InvalidOperationException( |
| | 1 | 137 | | $"Unable to locate MemoryPack entry '{path[depth]}' at depth {depth}."); |
| | | 138 | | } |
| | | 139 | | |
| | 1 | 140 | | MemoryPackRecordEnvelope nestedEnvelope = ReadEnvelope(nestedData); |
| | 1 | 141 | | RemoveMemoryPackEntry(nestedEnvelope, path, depth + 1); |
| | 1 | 142 | | envelope.Entries[path[depth]] = MemoryPackSerializer.Serialize(nestedEnvelope); |
| | 10 | 143 | | } |
| | | 144 | | |
| | | 145 | | private static void SetMemoryPackValue( |
| | | 146 | | MemoryPackRecordEnvelope envelope, |
| | | 147 | | string[] path, |
| | | 148 | | int depth, |
| | | 149 | | byte[] serializedValue) |
| | 5 | 150 | | { |
| | 5 | 151 | | if (depth == path.Length - 1) |
| | 3 | 152 | | { |
| | 3 | 153 | | envelope.Entries[path[depth]] = serializedValue; |
| | 3 | 154 | | return; |
| | | 155 | | } |
| | | 156 | | |
| | 2 | 157 | | if (!envelope.Entries.TryGetValue(path[depth], out byte[]? nestedData) |
| | 2 | 158 | | || nestedData == null) |
| | 1 | 159 | | { |
| | 1 | 160 | | throw new InvalidOperationException( |
| | 1 | 161 | | $"Unable to locate MemoryPack entry '{path[depth]}' at depth {depth}."); |
| | | 162 | | } |
| | | 163 | | |
| | 1 | 164 | | MemoryPackRecordEnvelope nestedEnvelope = ReadEnvelope(nestedData); |
| | 1 | 165 | | SetMemoryPackValue(nestedEnvelope, path, depth + 1, serializedValue); |
| | 1 | 166 | | envelope.Entries[path[depth]] = MemoryPackSerializer.Serialize(nestedEnvelope); |
| | 4 | 167 | | } |
| | | 168 | | |
| | | 169 | | #endregion |
| | | 170 | | } |