| | | 1 | | #if !CHRONICLER_DISABLE_MEMORYPACK |
| | | 2 | | using MemoryPack; |
| | | 3 | | #endif |
| | | 4 | | using System; |
| | | 5 | | using System.Text.Json; |
| | | 6 | | using System.Text.Json.Nodes; |
| | | 7 | | |
| | | 8 | | namespace Chronicler; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides methods for editing serialized payloads without fully deserializing them. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class SerializationPayloadEditor |
| | | 14 | | { |
| | | 15 | | #region Common |
| | | 16 | | |
| | | 17 | | #if CHRONICLER_DISABLE_MEMORYPACK |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Serializes a record to a payload format (JSON string or MemoryPack byte array) based on the specified options. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="record">The record to serialize.</param> |
| | | 23 | | /// <returns>The serialized payload.</returns> |
| | | 24 | | public static object SerializeRecord(IRecordable record) |
| | | 25 | | { |
| | | 26 | | return JsonRecordSerializer.Serialize(record, writeIndented: true); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Populates a record with data from a serialized payload (JSON string or MemoryPack byte array) based on the speci |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="target">The record to populate.</param> |
| | | 33 | | /// <param name="payload">The serialized payload.</param> |
| | | 34 | | public static void PopulateRecord(IRecordable target, object payload) |
| | | 35 | | { |
| | | 36 | | JsonRecordSerializer.Populate(target, (string)payload); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Removes an entry from a serialized payload (JSON property or MemoryPack entry) at the specified path based on th |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="payload">The serialized payload.</param> |
| | | 43 | | /// <param name="path">The path to the entry to remove.</param> |
| | | 44 | | /// <returns>The modified payload.</returns> |
| | | 45 | | public static object RemovePayloadEntry(object payload, params string[] path) |
| | | 46 | | { |
| | | 47 | | return RemoveJsonProperty((string)payload, path); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Sets a value in a serialized payload (JSON property or MemoryPack entry) at the specified path based on the spec |
| | | 53 | | /// </summary> |
| | | 54 | | /// <typeparam name="T">The type of the value to set.</typeparam> |
| | | 55 | | /// <param name="payload">The serialized payload.</param> |
| | | 56 | | /// <param name="value">The value to set.</param> |
| | | 57 | | /// <param name="path">The path to the entry to set.</param> |
| | | 58 | | /// <returns>The modified payload.</returns> |
| | | 59 | | public static object SetPayloadValue<T>( |
| | | 60 | | object payload, |
| | | 61 | | T value, |
| | | 62 | | params string[] path) |
| | | 63 | | { |
| | | 64 | | return SetJsonValue((string)payload, value, path); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | #else |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Serializes a record to a payload format (JSON string or MemoryPack byte array) based on the specified options. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="record">The record to serialize.</param> |
| | | 73 | | /// <param name="useMemoryPack">Whether to use MemoryPack for serialization.</param> |
| | | 74 | | /// <returns>The serialized payload.</returns> |
| | | 75 | | public static object SerializeRecord(IRecordable record, bool useMemoryPack = true) |
| | | 76 | | { |
| | 0 | 77 | | return useMemoryPack |
| | 0 | 78 | | ? MemoryPackRecordSerializer.Serialize(record) |
| | 0 | 79 | | : JsonRecordSerializer.Serialize(record, writeIndented: true); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Populates a record with data from a serialized payload (JSON string or MemoryPack byte array) based on the speci |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="target">The record to populate.</param> |
| | | 86 | | /// <param name="payload">The serialized payload.</param> |
| | | 87 | | /// <param name="useMemoryPack">Whether to use MemoryPack for deserialization.</param> |
| | | 88 | | public static void PopulateRecord( |
| | | 89 | | IRecordable target, |
| | | 90 | | object payload, |
| | | 91 | | bool useMemoryPack = true) |
| | | 92 | | { |
| | 0 | 93 | | if (useMemoryPack) |
| | | 94 | | { |
| | 0 | 95 | | MemoryPackRecordSerializer.Populate(target, (byte[])payload); |
| | 0 | 96 | | return; |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | JsonRecordSerializer.Populate(target, (string)payload); |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Removes an entry from a serialized payload (JSON property or MemoryPack entry) at the specified path based on th |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="payload">The serialized payload.</param> |
| | | 106 | | /// <param name="useMemoryPack">Whether to use MemoryPack for deserialization.</param> |
| | | 107 | | /// <param name="path">The path to the entry to remove.</param> |
| | | 108 | | /// <returns>The modified payload.</returns> |
| | | 109 | | public static object RemovePayloadEntry( |
| | | 110 | | object payload, |
| | | 111 | | bool useMemoryPack = true, |
| | | 112 | | params string[] path) |
| | | 113 | | { |
| | 0 | 114 | | return useMemoryPack |
| | 0 | 115 | | ? RemoveMemoryPackEntry((byte[])payload, path) |
| | 0 | 116 | | : RemoveJsonProperty((string)payload, path); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Sets a value in a serialized payload (JSON property or MemoryPack entry) at the specified path based on the spec |
| | | 122 | | /// </summary> |
| | | 123 | | /// <typeparam name="T">The type of the value to set.</typeparam> |
| | | 124 | | /// <param name="payload">The serialized payload.</param> |
| | | 125 | | /// <param name="value">The value to set.</param> |
| | | 126 | | /// <param name="useMemoryPack">Whether to use MemoryPack for deserialization.</param> |
| | | 127 | | /// <param name="path">The path to the entry to set.</param> |
| | | 128 | | /// <returns>The modified payload.</returns> |
| | | 129 | | public static object SetPayloadValue<T>( |
| | | 130 | | object payload, |
| | | 131 | | T value, |
| | | 132 | | bool useMemoryPack = true, |
| | | 133 | | params string[] path) |
| | | 134 | | { |
| | 0 | 135 | | return useMemoryPack |
| | 0 | 136 | | ? SetMemoryPackValue((byte[])payload, value, path) |
| | 0 | 137 | | : SetJsonValue((string)payload, value, path); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | #endif |
| | | 141 | | |
| | | 142 | | #endregion |
| | | 143 | | |
| | | 144 | | #region JSON Editing |
| | | 145 | | |
| | 1 | 146 | | private static readonly JsonSerializerOptions JsonOptions = new() |
| | 1 | 147 | | { |
| | 1 | 148 | | IncludeFields = true |
| | 1 | 149 | | }; |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Removes a property from a JSON payload at the specified path. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <param name="json">The JSON payload.</param> |
| | | 155 | | /// <param name="path">The path to the property to remove.</param> |
| | | 156 | | /// <returns>The modified JSON payload.</returns> |
| | | 157 | | /// <exception cref="ArgumentException"></exception> |
| | | 158 | | public static string RemoveJsonProperty(string json, params string[] path) |
| | | 159 | | { |
| | 12 | 160 | | if (path == null || path.Length == 0) |
| | 2 | 161 | | throw new ArgumentException("A JSON property path is required.", nameof(path)); |
| | | 162 | | |
| | 10 | 163 | | JsonObject root = ParseJsonRoot(json); |
| | 9 | 164 | | JsonObject parent = GetJsonParent(root, path); |
| | 9 | 165 | | parent.Remove(path[^1]); |
| | 9 | 166 | | return root.ToJsonString(JsonOptions); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Sets a property value in a JSON payload at the specified path. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <typeparam name="T">The type of the value to set.</typeparam> |
| | | 173 | | /// <param name="json">The JSON payload.</param> |
| | | 174 | | /// <param name="value">The value to set.</param> |
| | | 175 | | /// <param name="path">The path to the property to set.</param> |
| | | 176 | | /// <returns>The modified JSON payload.</returns> |
| | | 177 | | /// <exception cref="ArgumentException"></exception> |
| | | 178 | | public static string SetJsonValue<T>(string json, T value, params string[] path) |
| | | 179 | | { |
| | 7 | 180 | | if (path == null || path.Length == 0) |
| | 2 | 181 | | throw new ArgumentException("A JSON property path is required.", nameof(path)); |
| | | 182 | | |
| | 5 | 183 | | JsonObject root = ParseJsonRoot(json); |
| | 4 | 184 | | JsonObject parent = GetJsonParent(root, path); |
| | 3 | 185 | | parent[path[^1]] = JsonSerializer.SerializeToNode(value, JsonOptions); |
| | 3 | 186 | | return root.ToJsonString(JsonOptions); |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | private static JsonObject ParseJsonRoot(string json) |
| | | 190 | | { |
| | 15 | 191 | | JsonNode rootNode = JsonNode.Parse(json) |
| | 15 | 192 | | ?? throw new InvalidOperationException("Unable to parse JSON payload."); |
| | 14 | 193 | | return rootNode as JsonObject |
| | 14 | 194 | | ?? throw new InvalidOperationException("Expected JSON root object."); |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | private static JsonObject GetJsonParent(JsonObject root, string[] path) |
| | | 198 | | { |
| | 13 | 199 | | JsonObject current = root; |
| | 30 | 200 | | for (int i = 0; i < path.Length - 1; i++) |
| | | 201 | | { |
| | 3 | 202 | | current = current[path[i]] as JsonObject |
| | 3 | 203 | | ?? throw new InvalidOperationException( |
| | 3 | 204 | | $"Expected JSON object at path segment '{path[i]}'."); |
| | | 205 | | } |
| | | 206 | | |
| | 12 | 207 | | return current; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | #endregion |
| | | 211 | | |
| | | 212 | | #if !CHRONICLER_DISABLE_MEMORYPACK |
| | | 213 | | |
| | | 214 | | #region MemoryPack Editing |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Removes a MemoryPack entry from a serialized payload at the specified path. |
| | | 218 | | /// </summary> |
| | | 219 | | /// <param name="data">The serialized MemoryPack payload.</param> |
| | | 220 | | /// <param name="path">The path to the entry to remove.</param> |
| | | 221 | | /// <returns>The modified serialized MemoryPack payload.</returns> |
| | | 222 | | /// <exception cref="ArgumentException"></exception> |
| | | 223 | | public static byte[] RemoveMemoryPackEntry(byte[] data, params string[] path) |
| | | 224 | | { |
| | 12 | 225 | | if (path == null || path.Length == 0) |
| | 2 | 226 | | throw new ArgumentException("A MemoryPack entry path is required.", nameof(path)); |
| | | 227 | | |
| | 10 | 228 | | MemoryPackRecordEnvelope envelope = ReadEnvelope(data); |
| | 10 | 229 | | RemoveMemoryPackEntry(envelope, path, 0); |
| | 9 | 230 | | return MemoryPackSerializer.Serialize(envelope); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Sets a MemoryPack entry value in a serialized payload at the specified path. |
| | | 235 | | /// </summary> |
| | | 236 | | /// <typeparam name="T">The type of the value to set.</typeparam> |
| | | 237 | | /// <param name="data">The serialized MemoryPack payload.</param> |
| | | 238 | | /// <param name="value">The value to set.</param> |
| | | 239 | | /// <param name="path">The path to the entry to set.</param> |
| | | 240 | | /// <returns>The modified serialized MemoryPack payload.</returns> |
| | | 241 | | /// <exception cref="ArgumentException"></exception> |
| | | 242 | | public static byte[] SetMemoryPackValue<T>(byte[] data, T value, params string[] path) |
| | | 243 | | { |
| | 6 | 244 | | if (path == null || path.Length == 0) |
| | 2 | 245 | | throw new ArgumentException("A MemoryPack entry path is required.", nameof(path)); |
| | | 246 | | |
| | 4 | 247 | | MemoryPackRecordEnvelope envelope = ReadEnvelope(data); |
| | 4 | 248 | | SetMemoryPackValue(envelope, path, 0, MemoryPackSerializer.Serialize(value)); |
| | 3 | 249 | | return MemoryPackSerializer.Serialize(envelope); |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | private static MemoryPackRecordEnvelope ReadEnvelope(byte[] data) |
| | | 253 | | { |
| | 16 | 254 | | return MemoryPackSerializer.Deserialize<MemoryPackRecordEnvelope>(data) |
| | 16 | 255 | | ?? new MemoryPackRecordEnvelope(); |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | private static void RemoveMemoryPackEntry(MemoryPackRecordEnvelope envelope, string[] path, int depth) |
| | | 259 | | { |
| | 11 | 260 | | if (depth == path.Length - 1) |
| | | 261 | | { |
| | 9 | 262 | | envelope.RemoveEntry(path[depth]); |
| | 9 | 263 | | return; |
| | | 264 | | } |
| | | 265 | | |
| | 2 | 266 | | if (!envelope.TryGetEntry(path[depth], out byte[]? nestedData) |
| | 2 | 267 | | || nestedData == null) |
| | | 268 | | { |
| | 1 | 269 | | throw new InvalidOperationException( |
| | 1 | 270 | | $"Unable to locate MemoryPack entry '{path[depth]}' at depth {depth}."); |
| | | 271 | | } |
| | | 272 | | |
| | 1 | 273 | | MemoryPackRecordEnvelope nestedEnvelope = ReadEnvelope(nestedData); |
| | 1 | 274 | | RemoveMemoryPackEntry(nestedEnvelope, path, depth + 1); |
| | 1 | 275 | | envelope.SetEntry(path[depth], MemoryPackSerializer.Serialize(nestedEnvelope)); |
| | 1 | 276 | | } |
| | | 277 | | |
| | | 278 | | private static void SetMemoryPackValue( |
| | | 279 | | MemoryPackRecordEnvelope envelope, |
| | | 280 | | string[] path, |
| | | 281 | | int depth, |
| | | 282 | | byte[] serializedValue) |
| | | 283 | | { |
| | 5 | 284 | | if (depth == path.Length - 1) |
| | | 285 | | { |
| | 3 | 286 | | envelope.SetEntry(path[depth], serializedValue); |
| | 3 | 287 | | return; |
| | | 288 | | } |
| | | 289 | | |
| | 2 | 290 | | if (!envelope.TryGetEntry(path[depth], out byte[]? nestedData) |
| | 2 | 291 | | || nestedData == null) |
| | | 292 | | { |
| | 1 | 293 | | throw new InvalidOperationException( |
| | 1 | 294 | | $"Unable to locate MemoryPack entry '{path[depth]}' at depth {depth}."); |
| | | 295 | | } |
| | | 296 | | |
| | 1 | 297 | | MemoryPackRecordEnvelope nestedEnvelope = ReadEnvelope(nestedData); |
| | 1 | 298 | | SetMemoryPackValue(nestedEnvelope, path, depth + 1, serializedValue); |
| | 1 | 299 | | envelope.SetEntry(path[depth], MemoryPackSerializer.Serialize(nestedEnvelope)); |
| | 1 | 300 | | } |
| | | 301 | | |
| | | 302 | | #endregion |
| | | 303 | | |
| | | 304 | | #endif |
| | | 305 | | } |