From 7661c4fe2db2cb2cc407ab2c90dd87693c2d107f Mon Sep 17 00:00:00 2001 From: ms2mml <> Date: Mon, 7 Sep 2020 23:00:37 -0700 Subject: [PATCH] Add serialize/deserialize helper functions to SeString --- .../Game/Chat/SeStringHandling/SeString.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Dalamud/Game/Chat/SeStringHandling/SeString.cs b/Dalamud/Game/Chat/SeStringHandling/SeString.cs index 8aa94726a..a3cfa0fd1 100644 --- a/Dalamud/Game/Chat/SeStringHandling/SeString.cs +++ b/Dalamud/Game/Chat/SeStringHandling/SeString.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using Dalamud.Data; using Dalamud.Game.Chat.SeStringHandling.Payloads; using Newtonsoft.Json; @@ -102,5 +103,43 @@ namespace Dalamud.Game.Chat.SeStringHandling return messageBytes.ToArray(); } + + /// + /// Serializes the SeString to json + /// + /// An json representation of this object + public string ToJson() + { + return JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings() + { + PreserveReferencesHandling = PreserveReferencesHandling.Objects, + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + TypeNameHandling = TypeNameHandling.Auto + }); + } + + /// + /// Creates a SeString from a json. (For testing - not recommended for production use.) + /// + /// A serialized SeString produced by ToJson() + /// An initialized instance of DataManager for Lumina queries. + /// A SeString initialized with values from the json + public static SeString FromJson(string json, DataManager dataManager) + { + var s = JsonConvert.DeserializeObject(json, new JsonSerializerSettings + { + PreserveReferencesHandling = PreserveReferencesHandling.Objects, + TypeNameHandling = TypeNameHandling.Auto, + ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor + }); + + foreach(var payload in s.Payloads) + { + var dataResolver = payload.GetType().GetField("DataResolver", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + dataResolver.SetValue(payload, dataManager); + } + + return s; + } } }