Serialization.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text.Json;
  8. using System.Text.Json.Serialization;
  9. using InABox.Clients;
  10. namespace InABox.Core
  11. {
  12. public enum SerializationFormat
  13. {
  14. Json,
  15. Binary
  16. }
  17. public class SerialisationException : Exception
  18. {
  19. public SerialisationException(string message): base(message) { }
  20. }
  21. public interface ISerializeBinary
  22. {
  23. public void SerializeBinary(CoreBinaryWriter writer);
  24. public void DeserializeBinary(CoreBinaryReader reader);
  25. }
  26. public static class Serialization
  27. {
  28. //private static JsonSerializerOptions? _serializerSettings;
  29. private static JsonSerializerOptions SerializerSettings(bool indented = true)
  30. {
  31. var serializerSettings = CreateSerializerSettings();
  32. serializerSettings.WriteIndented = indented;
  33. return serializerSettings;
  34. }
  35. public static JsonSerializerOptions CreateSerializerSettings(bool indented = true)
  36. {
  37. var settings = new JsonSerializerOptions
  38. {
  39. // DateParseHandling = DateParseHandling.DateTime,
  40. // DateFormatHandling = DateFormatHandling.IsoDateFormat,
  41. // DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
  42. };
  43. settings.Converters.Add(new CoreTableJsonConverter());
  44. ////serializerSettings.Converters.Add(new DateTimeJsonConverter());
  45. settings.Converters.Add(new FilterJsonConverter());
  46. settings.Converters.Add(new ColumnJsonConverter());
  47. settings.Converters.Add(new SortOrderJsonConverter());
  48. settings.Converters.Add(new UserPropertiesJsonConverter());
  49. settings.Converters.Add(new BaseObjectJSONConverter());
  50. //settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
  51. settings.WriteIndented = indented; // ? Formatting.Indented : Formatting.None;
  52. return settings;
  53. }
  54. public static string Serialize(object? o, bool indented = false)
  55. {
  56. var json = JsonSerializer.Serialize(o, SerializerSettings(indented));
  57. return json;
  58. }
  59. public static void Serialize(object o, Stream stream, bool indented = false)
  60. {
  61. var settings = SerializerSettings(indented);
  62. JsonSerializer.Serialize(stream, o, settings);
  63. // using (var sw = new StreamWriter(stream))
  64. // {
  65. // using (JsonWriter writer = new JsonTextWriter(sw))
  66. // {
  67. // var serializer = JsonSerializer.Create(settings);
  68. // serializer.Serialize(writer, o);
  69. // }
  70. // }
  71. }
  72. // public static void DeserializeInto(string json, object target)
  73. // {
  74. // JsonConvert.PopulateObject(json, target, SerializerSettings());
  75. // }
  76. [return: MaybeNull]
  77. public static T Deserialize<T>(Stream? stream, bool strict = false)
  78. {
  79. if (stream == null)
  80. return default;
  81. try
  82. {
  83. var settings = SerializerSettings();
  84. return JsonSerializer.Deserialize<T>(stream, settings);
  85. // using var sr = new StreamReader(stream);
  86. // using JsonReader reader = new JsonTextReader(sr);
  87. // var serializer = JsonSerializer.Create(settings);
  88. // return serializer.Deserialize<T>(reader);
  89. }
  90. catch (Exception e)
  91. {
  92. if (strict)
  93. throw;
  94. Logger.Send(LogType.Error, ClientFactory.UserID, $"Error in Deserialize<{typeof(T)}>(): {e.Message}");
  95. return default;
  96. }
  97. }
  98. public static object? Deserialize(Type type, Stream? stream)
  99. {
  100. if (stream == null)
  101. return null;
  102. object? result = null;
  103. var settings = SerializerSettings();
  104. result = JsonSerializer.Deserialize(stream, type, settings);
  105. // using (var sr = new StreamReader(stream))
  106. // {
  107. // using (JsonReader reader = new JsonTextReader(sr))
  108. // {
  109. // var serializer = JsonSerializer.Create(settings);
  110. // result = serializer.Deserialize(reader, type);
  111. // }
  112. // }
  113. return result;
  114. }
  115. // [return: MaybeNull]
  116. // public static T Deserialize<T>(JToken obj, bool strict = false)
  117. // {
  118. // var ret = default(T);
  119. // try
  120. // {
  121. // var settings = SerializerSettings();
  122. // var serializer = JsonSerializer.Create(settings);
  123. // return obj.ToObject<T>();
  124. // }
  125. // catch (Exception)
  126. // {
  127. // if (strict)
  128. // {
  129. // throw;
  130. // }
  131. // if (typeof(T).IsArray)
  132. // {
  133. // ret = (T)(object)Array.CreateInstance(typeof(T).GetElementType(), 0);
  134. // }
  135. // else
  136. // {
  137. // ret = Activator.CreateInstance<T>();
  138. // }
  139. // }
  140. //
  141. // return ret;
  142. // }
  143. [return: MaybeNull]
  144. public static T Deserialize<T>(string? json, bool strict = false) // where T : new()
  145. {
  146. var ret = default(T);
  147. if (string.IsNullOrWhiteSpace(json))
  148. return ret;
  149. try
  150. {
  151. var settings = SerializerSettings();
  152. //if (typeof(T).IsSubclassOf(typeof(BaseObject)))
  153. //{
  154. // ret = Activator.CreateInstance<T>();
  155. // (ret as BaseObject).SetObserving(false);
  156. // JsonConvert.PopulateObject(json, ret, settings);
  157. // (ret as BaseObject).SetObserving(true);
  158. //}
  159. //else
  160. if (typeof(T).IsArray)
  161. {
  162. ret = JsonSerializer.Deserialize<T>(json, settings);
  163. //object o = Array.CreateInstance(typeof(T).GetElementType(), 0);
  164. //ret = (T)o;
  165. }
  166. else
  167. {
  168. ret = JsonSerializer.Deserialize<T>(json, settings);
  169. }
  170. }
  171. catch (Exception e)
  172. {
  173. if (strict)
  174. {
  175. throw;
  176. }
  177. CoreUtils.LogException("", e);
  178. if (typeof(T).IsArray)
  179. {
  180. ret = (T)(object)Array.CreateInstance(typeof(T).GetElementType(), 0);
  181. }
  182. else
  183. {
  184. ret = (T)Activator.CreateInstance(typeof(T), true);
  185. }
  186. }
  187. return ret;
  188. }
  189. public static object? Deserialize(Type T, string json) // where T : new()
  190. {
  191. var ret = T.GetDefault();
  192. if (string.IsNullOrWhiteSpace(json))
  193. return ret;
  194. try
  195. {
  196. var settings = SerializerSettings();
  197. //if (typeof(T).IsSubclassOf(typeof(BaseObject)))
  198. //{
  199. // ret = Activator.CreateInstance<T>();
  200. // (ret as BaseObject).SetObserving(false);
  201. // JsonConvert.PopulateObject(json, ret, settings);
  202. // (ret as BaseObject).SetObserving(true);
  203. //}
  204. //else
  205. if (T.IsArray)
  206. {
  207. object o = Array.CreateInstance(T.GetElementType(), 0);
  208. ret = o;
  209. }
  210. else
  211. {
  212. ret = JsonSerializer.Deserialize(json, T, settings);
  213. }
  214. }
  215. catch (Exception)
  216. {
  217. ret = Activator.CreateInstance(T, true);
  218. }
  219. return ret;
  220. }
  221. #region Binary Serialization
  222. public static byte[] WriteBinary(this ISerializeBinary obj, BinarySerializationSettings settings)
  223. {
  224. using var stream = new MemoryStream();
  225. obj.SerializeBinary(new CoreBinaryWriter(stream, settings));
  226. return stream.ToArray();
  227. }
  228. public static void WriteBinary(this ISerializeBinary obj, Stream stream, BinarySerializationSettings settings)
  229. {
  230. obj.SerializeBinary(new CoreBinaryWriter(stream, settings));
  231. }
  232. public static T ReadBinary<T>(byte[] data, BinarySerializationSettings settings)
  233. where T : ISerializeBinary, new() => (T)ReadBinary(typeof(T), data, settings);
  234. public static T ReadBinary<T>(Stream stream, BinarySerializationSettings settings)
  235. where T : ISerializeBinary, new() => (T)ReadBinary(typeof(T), stream, settings);
  236. public static object ReadBinary(Type T, byte[] data, BinarySerializationSettings settings)
  237. {
  238. using var stream = new MemoryStream(data);
  239. return ReadBinary(T, stream, settings);
  240. }
  241. public static object ReadBinary(Type T, Stream stream, BinarySerializationSettings settings)
  242. {
  243. var obj = (Activator.CreateInstance(T) as ISerializeBinary)!;
  244. obj.DeserializeBinary(new CoreBinaryReader(stream, settings));
  245. return obj;
  246. }
  247. #endregion
  248. }
  249. public class CoreBinaryReader : BinaryReader
  250. {
  251. public BinarySerializationSettings Settings { get; set; }
  252. public CoreBinaryReader(Stream stream, BinarySerializationSettings settings) : base(stream)
  253. {
  254. Settings = settings;
  255. }
  256. }
  257. public class CoreBinaryWriter : BinaryWriter
  258. {
  259. public BinarySerializationSettings Settings { get; set; }
  260. public CoreBinaryWriter(Stream stream, BinarySerializationSettings settings) : base(stream)
  261. {
  262. Settings = settings;
  263. }
  264. }
  265. /// <summary>
  266. /// A class to maintain the consistency of serialisation formats across versions.
  267. /// The design of this is such that specific versions of serialisation have different parameters set,
  268. /// and the versions are maintained as static properties. Please keep the constructor private.
  269. /// </summary>
  270. /// <remarks>
  271. /// Note that <see cref="Latest"/> should always be updated to point to the latest version.
  272. /// <br/>
  273. /// Note also that all versions should have an entry in the <see cref="ConvertVersionString(string)"/> function.
  274. /// <br/>
  275. /// Also, if you create a new format, it would probably be a good idea to add a database update script to get all
  276. /// <see cref="IPackable"/> and <see cref="ISerializeBinary"/> properties and update the version of the format.
  277. /// (Otherwise, we'd basically be nullifying all data that is currently binary serialised.)
  278. /// </remarks>
  279. public class BinarySerializationSettings
  280. {
  281. /// <summary>
  282. /// Should the Info() call return RPC and Rest Ports? This is
  283. /// To workaround a bug in RPCsockets that crash on large uploads
  284. /// </summary>
  285. /// <remarks>
  286. /// True in all serialization versions >= 1.2
  287. /// </remarks>
  288. public bool RPCClientWorkaround { get; set; }
  289. /// <summary>
  290. /// Should reference types include a flag for nullability? (Adds an extra boolean field for whether the value is null or not).
  291. /// </summary>
  292. /// <remarks>
  293. /// True in all serialisation versions >= 1.1.
  294. /// </remarks>
  295. public bool IncludeNullables { get; set; }
  296. public string Version { get; set; }
  297. public static BinarySerializationSettings Latest => V1_2;
  298. public static BinarySerializationSettings V1_0 = new BinarySerializationSettings("1.0")
  299. {
  300. IncludeNullables = false,
  301. RPCClientWorkaround = false
  302. };
  303. public static BinarySerializationSettings V1_1 = new BinarySerializationSettings("1.1")
  304. {
  305. IncludeNullables = true,
  306. RPCClientWorkaround = false
  307. };
  308. public static BinarySerializationSettings V1_2 = new BinarySerializationSettings("1.2")
  309. {
  310. IncludeNullables = true,
  311. RPCClientWorkaround = true
  312. };
  313. public static BinarySerializationSettings ConvertVersionString(string version) => version switch
  314. {
  315. "1.0" => V1_0,
  316. "1.1" => V1_1,
  317. "1.2" => V1_2,
  318. _ => V1_0
  319. };
  320. private BinarySerializationSettings(string version)
  321. {
  322. Version = version;
  323. }
  324. }
  325. public static class SerializationUtils
  326. {
  327. public static void Write(this BinaryWriter writer, Guid guid)
  328. {
  329. writer.Write(guid.ToByteArray());
  330. }
  331. public static Guid ReadGuid(this BinaryReader reader)
  332. {
  333. return new Guid(reader.ReadBytes(16));
  334. }
  335. public static void Write(this BinaryWriter writer, DateTime dateTime)
  336. {
  337. writer.Write(dateTime.Ticks);
  338. }
  339. public static DateTime ReadDateTime(this BinaryReader reader)
  340. {
  341. return new DateTime(reader.ReadInt64());
  342. }
  343. private static bool MatchType<T1>(Type t) => typeof(T1) == t;
  344. private static bool MatchType<T1,T2>(Type t) => (typeof(T1) == t) || (typeof(T2) == t);
  345. /// <summary>
  346. /// Binary serialize a bunch of different types of values. <see cref="WriteBinaryValue(CoreBinaryWriter, Type, object?)"/> and
  347. /// <see cref="ReadBinaryValue(CoreBinaryReader, Type)"/> are inverses of each other.
  348. /// </summary>
  349. /// <remarks>
  350. /// Handles <see cref="byte"/>[], <see cref="Array"/>s of serialisable values, <see cref="Enum"/>, <see cref="bool"/>, <see cref="string"/>,
  351. /// <see cref="Guid"/>, <see cref="byte"/>, <see cref="Int16"/>, <see cref="Int32"/>, <see cref="Int64"/>, <see cref="float"/>, <see cref="double"/>,
  352. /// <see cref="DateTime"/>, <see cref="TimeSpan"/>, <see cref="LoggablePropertyAttribute"/>, <see cref="IPackable"/>, <see cref="Nullable{T}"/>
  353. /// and <see cref="ISerializeBinary"/>.
  354. /// </remarks>
  355. /// <param name="writer"></param>
  356. /// <param name="type"></param>
  357. /// <param name="value"></param>
  358. /// <exception cref="Exception">If an object of <paramref name="type"/> is unable to be serialized.</exception>
  359. public static void WriteBinaryValue(this CoreBinaryWriter writer, Type type, object? value)
  360. {
  361. value ??= CoreUtils.GetDefault(type);
  362. if (value == null)
  363. {
  364. if (MatchType<string>(type))
  365. writer.Write("");
  366. else if (writer.Settings.IncludeNullables && typeof(IPackable).IsAssignableFrom(type))
  367. writer.Write(false);
  368. else if (writer.Settings.IncludeNullables && typeof(ISerializeBinary).IsAssignableFrom(type))
  369. writer.Write(false);
  370. else if (Nullable.GetUnderlyingType(type) is Type t)
  371. writer.Write(false);
  372. else if (MatchType<LoggablePropertyAttribute, object>(type))
  373. writer.Write("");
  374. else
  375. writer.Write(0);
  376. }
  377. else if (MatchType<byte[], object>(type) && value is byte[] bArray)
  378. {
  379. writer.Write(bArray.Length);
  380. writer.Write(bArray);
  381. }
  382. else if (type.IsArray && value is Array array)
  383. {
  384. var elementType = type.GetElementType();
  385. writer.Write(array.Length);
  386. foreach (var val1 in array)
  387. {
  388. WriteBinaryValue(writer, elementType, val1);
  389. }
  390. }
  391. else if (type.IsEnum && value is Enum e)
  392. {
  393. var underlyingType = type.GetEnumUnderlyingType();
  394. WriteBinaryValue(writer, underlyingType, Convert.ChangeType(e, underlyingType));
  395. }
  396. else if (MatchType<bool, object>(type) && value is bool b)
  397. {
  398. writer.Write(b);
  399. }
  400. else if (MatchType<string, object>(type) && value is string str)
  401. writer.Write(str);
  402. else if (MatchType<Guid, object>(type) && value is Guid guid)
  403. writer.Write(guid);
  404. else if (MatchType<byte, object>(type) && value is byte i8)
  405. writer.Write(i8);
  406. else if (MatchType<Int16, object>(type) && value is Int16 i16)
  407. writer.Write(i16);
  408. else if (MatchType<Int32, object>(type) && value is Int32 i32)
  409. writer.Write(i32);
  410. else if (MatchType<Int64, object>(type) && value is Int64 i64)
  411. writer.Write(i64);
  412. else if (MatchType<float, object>(type) && value is float f32)
  413. writer.Write(f32);
  414. else if (MatchType<double, object>(type) && value is double f64)
  415. writer.Write(f64);
  416. else if (MatchType<DateTime, object>(type) && value is DateTime date)
  417. writer.Write(date.Ticks);
  418. else if (MatchType<TimeSpan, object>(type) && value is TimeSpan time)
  419. writer.Write(time.Ticks);
  420. else if (MatchType<LoggablePropertyAttribute, object>(type) && value is LoggablePropertyAttribute lpa)
  421. writer.Write(lpa.Format ?? string.Empty);
  422. else if (typeof(IPackable).IsAssignableFrom(type) && value is IPackable pack)
  423. {
  424. if (writer.Settings.IncludeNullables)
  425. writer.Write(true);
  426. pack.Pack(writer);
  427. }
  428. else if (typeof(ISerializeBinary).IsAssignableFrom(type) && value is ISerializeBinary binary)
  429. {
  430. if (writer.Settings.IncludeNullables)
  431. writer.Write(true);
  432. binary.SerializeBinary(writer);
  433. }
  434. else if (Nullable.GetUnderlyingType(type) is Type t)
  435. {
  436. writer.Write(true);
  437. writer.WriteBinaryValue(t, value);
  438. }
  439. else if (value is UserProperty userprop)
  440. WriteBinaryValue(writer, userprop.Type, userprop.Value);
  441. else
  442. throw new SerialisationException($"Invalid type; Target DataType is {type} and value DataType is {value?.GetType().ToString() ?? "null"}");
  443. }
  444. public static void WriteBinaryValue<T>(this CoreBinaryWriter writer, T value)
  445. => WriteBinaryValue(writer, typeof(T), value);
  446. /// <summary>
  447. /// Binary deserialize a bunch of different types of values. <see cref="WriteBinaryValue(CoreBinaryWriter, Type, object?)"/> and
  448. /// <see cref="ReadBinaryValue(CoreBinaryReader, Type)"/> are inverses of each other.
  449. /// </summary>
  450. /// <remarks>
  451. /// Handles <see cref="byte"/>[], <see cref="Array"/>s of serialisable values, <see cref="Enum"/>, <see cref="bool"/>, <see cref="string"/>,
  452. /// <see cref="Guid"/>, <see cref="byte"/>, <see cref="Int16"/>, <see cref="Int32"/>, <see cref="Int64"/>, <see cref="float"/>, <see cref="double"/>,
  453. /// <see cref="DateTime"/>, <see cref="TimeSpan"/>, <see cref="LoggablePropertyAttribute"/>, <see cref="IPackable"/>, <see cref="Nullable{T}"/>
  454. /// and <see cref="ISerializeBinary"/>.
  455. /// </remarks>
  456. /// <param name="reader"></param>
  457. /// <param name="type"></param>
  458. /// <exception cref="Exception">If an object of <paramref name="type"/> is unable to be deserialized.</exception>
  459. public static object? ReadBinaryValue(this CoreBinaryReader reader, Type type)
  460. {
  461. if (type == typeof(byte[]))
  462. {
  463. var length = reader.ReadInt32();
  464. return reader.ReadBytes(length);
  465. }
  466. else if (type.IsArray)
  467. {
  468. var length = reader.ReadInt32();
  469. var elementType = type.GetElementType();
  470. var array = Array.CreateInstance(elementType, length);
  471. for (int i = 0; i < array.Length; ++i)
  472. {
  473. array.SetValue(ReadBinaryValue(reader, elementType), i);
  474. }
  475. return array;
  476. }
  477. else if (type.IsEnum)
  478. {
  479. var val = ReadBinaryValue(reader, type.GetEnumUnderlyingType());
  480. return Enum.ToObject(type, val);
  481. }
  482. else if (type == typeof(bool))
  483. {
  484. return reader.ReadBoolean();
  485. }
  486. else if (type == typeof(string))
  487. {
  488. return reader.ReadString();
  489. }
  490. else if (type == typeof(Guid))
  491. {
  492. return reader.ReadGuid();
  493. }
  494. else if (type == typeof(byte))
  495. {
  496. return reader.ReadByte();
  497. }
  498. else if (type == typeof(Int16))
  499. {
  500. return reader.ReadInt16();
  501. }
  502. else if (type == typeof(Int32))
  503. {
  504. return reader.ReadInt32();
  505. }
  506. else if (type == typeof(Int64))
  507. {
  508. return reader.ReadInt64();
  509. }
  510. else if (type == typeof(float))
  511. {
  512. return reader.ReadSingle();
  513. }
  514. else if (type == typeof(double))
  515. {
  516. return reader.ReadDouble();
  517. }
  518. else if (type == typeof(DateTime))
  519. {
  520. return new DateTime(reader.ReadInt64());
  521. }
  522. else if (type == typeof(TimeSpan))
  523. {
  524. return new TimeSpan(reader.ReadInt64());
  525. }
  526. else if (type == typeof(LoggablePropertyAttribute))
  527. {
  528. String format = reader.ReadString();
  529. return String.IsNullOrWhiteSpace(format)
  530. ? null
  531. : new LoggablePropertyAttribute() { Format = format };
  532. }
  533. else if (typeof(IPackable).IsAssignableFrom(type))
  534. {
  535. if (!reader.Settings.IncludeNullables || reader.ReadBoolean()) // Note the short-circuit operator preventing reading a boolean.
  536. {
  537. var packable = (Activator.CreateInstance(type) as IPackable)!;
  538. packable.Unpack(reader);
  539. return packable;
  540. }
  541. else
  542. {
  543. return null;
  544. }
  545. }
  546. else if (typeof(ISerializeBinary).IsAssignableFrom(type))
  547. {
  548. if (!reader.Settings.IncludeNullables || reader.ReadBoolean()) // Note the short-circuit operator preventing reading a boolean.
  549. {
  550. var obj = (Activator.CreateInstance(type, true) as ISerializeBinary)!;
  551. obj.DeserializeBinary(reader);
  552. return obj;
  553. }
  554. else
  555. {
  556. return null;
  557. }
  558. }
  559. else if (Nullable.GetUnderlyingType(type) is Type t)
  560. {
  561. var isNull = reader.ReadBoolean();
  562. if (isNull)
  563. {
  564. return null;
  565. }
  566. else
  567. {
  568. return reader.ReadBinaryValue(t);
  569. }
  570. }
  571. else
  572. {
  573. throw new SerialisationException($"Invalid type; Target DataType is {type}");
  574. }
  575. }
  576. public static T ReadBinaryValue<T>(this CoreBinaryReader reader)
  577. {
  578. var result = ReadBinaryValue(reader, typeof(T));
  579. return (result != null ? (T)result : default)!;
  580. }
  581. public static IEnumerable<IProperty> SerializableProperties(Type type, Predicate<IProperty>? filter = null) =>
  582. DatabaseSchema.Properties(type)
  583. .Where(x => (!(x is StandardProperty st) || st.IsSerializable) && (filter?.Invoke(x) ?? true));
  584. private static void WriteOriginalValues<TObject>(this CoreBinaryWriter writer, TObject obj)
  585. where TObject : BaseObject
  586. {
  587. var originalValues = new List<Tuple<Type, string, object?>>();
  588. foreach (var (key, value) in obj.OriginalValueList)
  589. {
  590. if (DatabaseSchema.Property(obj.GetType(), key) is IProperty prop && prop.IsSerializable)
  591. {
  592. originalValues.Add(new Tuple<Type, string, object?>(prop.PropertyType, key, value));
  593. }
  594. }
  595. writer.Write(originalValues.Count);
  596. foreach (var (type, key, value) in originalValues)
  597. {
  598. writer.Write(key);
  599. try
  600. {
  601. writer.WriteBinaryValue(type, value);
  602. }
  603. catch (Exception e)
  604. {
  605. CoreUtils.LogException("", e, "Error serialising OriginalValues");
  606. }
  607. }
  608. }
  609. private static void ReadOriginalValues<TObject>(this CoreBinaryReader reader, TObject obj)
  610. where TObject : BaseObject
  611. {
  612. var nOriginalValues = reader.ReadInt32();
  613. for (int i = 0; i < nOriginalValues; ++i)
  614. {
  615. var key = reader.ReadString();
  616. if (DatabaseSchema.Property(obj.GetType(), key) is IProperty prop)
  617. {
  618. var value = reader.ReadBinaryValue(prop.PropertyType);
  619. obj.OriginalValueList[prop.Name] = value;
  620. }
  621. }
  622. }
  623. public static void WriteObject<TObject>(this CoreBinaryWriter writer, TObject entity, Type type)
  624. where TObject : BaseObject
  625. {
  626. if (!typeof(TObject).IsAssignableFrom(type))
  627. throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
  628. var properties = SerializableProperties(type).ToList();
  629. writer.Write(properties.Count);
  630. foreach (var property in properties)
  631. {
  632. writer.Write(property.Name);
  633. writer.WriteBinaryValue(property.PropertyType, property.Getter()(entity));
  634. }
  635. writer.WriteOriginalValues(entity);
  636. }
  637. /// <summary>
  638. /// An implementation of binary serialising a <typeparamref name="TObject"/>; this is the inverse of <see cref="ReadObject{TObject}(CoreBinaryReader)"/>.
  639. /// </summary>
  640. /// <remarks>
  641. /// Also serialises the names of properties along with the values.
  642. /// </remarks>
  643. /// <typeparam name="TObject"></typeparam>
  644. /// <param name="writer"></param>
  645. /// <param name="entity"></param>
  646. public static void WriteObject<TObject>(this CoreBinaryWriter writer, TObject entity)
  647. where TObject : BaseObject, new() => WriteObject(writer, entity, typeof(TObject));
  648. public static TObject ReadObject<TObject>(this CoreBinaryReader reader, Type type)
  649. where TObject : BaseObject
  650. {
  651. if (!typeof(TObject).IsAssignableFrom(type))
  652. throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
  653. var obj = (Activator.CreateInstance(type) as TObject)!;
  654. obj.SetObserving(false);
  655. var nProps = reader.ReadInt32();
  656. for (int i = 0; i < nProps; ++i)
  657. {
  658. var propName = reader.ReadString();
  659. var property = DatabaseSchema.Property(type, propName)
  660. ?? throw new SerialisationException($"Property {propName} does not exist on {type.EntityName()}");
  661. property.Setter()(obj, reader.ReadBinaryValue(property.PropertyType));
  662. }
  663. reader.ReadOriginalValues(obj);
  664. obj.SetObserving(true);
  665. return obj;
  666. }
  667. /// <summary>
  668. /// The inverse of <see cref="WriteObject{TObject}(CoreBinaryWriter, TObject)"/>.
  669. /// </summary>
  670. /// <typeparam name="TObject"></typeparam>
  671. /// <param name="reader"></param>
  672. /// <returns></returns>
  673. public static TObject ReadObject<TObject>(this CoreBinaryReader reader)
  674. where TObject : BaseObject, new() => reader.ReadObject<TObject>(typeof(TObject));
  675. /// <summary>
  676. /// An implementation of binary serialising multiple <typeparamref name="TObject"/>s;
  677. /// this is the inverse of <see cref="ReadObjects{TObject}(CoreBinaryReader)"/>.
  678. /// </summary>
  679. /// <remarks>
  680. /// Also serialises the names of properties along with the values.
  681. /// </remarks>
  682. /// <typeparam name="TObject"></typeparam>
  683. /// <param name="writer"></param>
  684. /// <param name="objects"></param>
  685. public static void WriteObjects<TObject>(this CoreBinaryWriter writer, ICollection<TObject>? objects)
  686. where TObject : BaseObject, new() => WriteObjects(writer, typeof(TObject), objects);
  687. public static void WriteObjects<TObject>(this CoreBinaryWriter writer, Type type, ICollection<TObject>? objects, Predicate<IProperty>? filter = null)
  688. where TObject : BaseObject
  689. {
  690. if (!typeof(TObject).IsAssignableFrom(type))
  691. throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
  692. var nObjs = objects?.Count ?? 0;
  693. writer.Write(nObjs);
  694. if (nObjs == 0)
  695. {
  696. return;
  697. }
  698. var properties = SerializableProperties(type, filter).ToList();
  699. writer.Write(properties.Count);
  700. foreach (var property in properties)
  701. {
  702. writer.Write(property.Name);
  703. }
  704. if(objects != null)
  705. {
  706. foreach (var obj in objects)
  707. {
  708. foreach (var property in properties)
  709. {
  710. writer.WriteBinaryValue(property.PropertyType, property.Getter()(obj));
  711. }
  712. writer.WriteOriginalValues(obj);
  713. }
  714. }
  715. }
  716. /// <summary>
  717. /// The inverse of <see cref="WriteObjects{TObject}(CoreBinaryWriter, ICollection{TObject})"/>.
  718. /// </summary>
  719. /// <typeparam name="TObject"></typeparam>
  720. /// <param name="reader"></param>
  721. /// <returns></returns>
  722. public static List<TObject> ReadObjects<TObject>(this CoreBinaryReader reader)
  723. where TObject : BaseObject, new()
  724. {
  725. return ReadObjects<TObject>(reader, typeof(TObject));
  726. }
  727. public static List<TObject> ReadObjects<TObject>(this CoreBinaryReader reader, Type type) where TObject : BaseObject
  728. {
  729. if (!typeof(TObject).IsAssignableFrom(type))
  730. throw new SerialisationException($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
  731. var objs = new List<TObject>();
  732. var properties = new List<IProperty>();
  733. var nObjs = reader.ReadInt32();
  734. if(nObjs == 0)
  735. {
  736. return objs;
  737. }
  738. var nProps = reader.ReadInt32();
  739. for (int i = 0; i < nProps; ++i)
  740. {
  741. var propertyName = reader.ReadString();
  742. var property = DatabaseSchema.Property(type, propertyName)
  743. ?? throw new SerialisationException($"Property {propertyName} does not exist on {type.EntityName()}");
  744. properties.Add(property);
  745. }
  746. for (int i = 0; i < nObjs; ++i)
  747. {
  748. var obj = (Activator.CreateInstance(type) as TObject)!;
  749. obj.SetObserving(false);
  750. foreach (var property in properties)
  751. {
  752. property.Setter()(obj, reader.ReadBinaryValue(property.PropertyType));
  753. }
  754. reader.ReadOriginalValues(obj);
  755. obj.SetObserving(true);
  756. objs.Add(obj);
  757. }
  758. return objs;
  759. }
  760. }
  761. public abstract class CustomJsonConverter<T> : JsonConverter<T>
  762. {
  763. protected object? ReadJson(ref Utf8JsonReader reader)
  764. {
  765. switch (reader.TokenType)
  766. {
  767. case JsonTokenType.String:
  768. return reader.GetString();
  769. case JsonTokenType.Number:
  770. if (reader.TryGetInt32(out int intValue))
  771. return intValue;
  772. if (reader.TryGetDouble(out double doubleValue))
  773. return doubleValue;
  774. return null;
  775. case JsonTokenType.True:
  776. return true;
  777. case JsonTokenType.False:
  778. return false;
  779. case JsonTokenType.Null:
  780. return null;
  781. case JsonTokenType.StartArray:
  782. var values = new List<object?>();
  783. reader.Read();
  784. while(reader.TokenType != JsonTokenType.EndArray)
  785. {
  786. values.Add(ReadJson(ref reader));
  787. reader.Read();
  788. }
  789. return values;
  790. default:
  791. return null;
  792. }
  793. }
  794. /// <summary>
  795. /// Write a value as a JSON object; note that some data types, like
  796. /// <see cref="Guid"/> and <see cref="DateTime"/> will be encoded as
  797. /// strings, and therefore will be returned as strings when read by
  798. /// <see cref="ReadJson(Utf8JsonReader)"/>. However, all types that
  799. /// this can write should be able to be retrieved by calling <see
  800. /// cref="CoreUtils.ChangeType(object?, Type)"/> on the resultant
  801. /// value.
  802. /// </summary>
  803. protected void WriteJson(Utf8JsonWriter writer, object? value)
  804. {
  805. if (value == null)
  806. writer.WriteNullValue();
  807. else if (value is string sVal)
  808. writer.WriteStringValue(sVal);
  809. else if (value is bool bVal)
  810. writer.WriteBooleanValue(bVal);
  811. else if (value is byte b)
  812. writer.WriteNumberValue(b);
  813. else if (value is short i16)
  814. writer.WriteNumberValue(i16);
  815. else if (value is int i32)
  816. writer.WriteNumberValue(i32);
  817. else if (value is long i64)
  818. writer.WriteNumberValue(i64);
  819. else if (value is float f)
  820. writer.WriteNumberValue(f);
  821. else if (value is double dVal)
  822. writer.WriteNumberValue(dVal);
  823. else if (value is DateTime dtVal)
  824. writer.WriteStringValue(dtVal.ToString());
  825. else if (value is TimeSpan tsVal)
  826. writer.WriteStringValue(tsVal.ToString());
  827. else if (value is Guid guid)
  828. writer.WriteStringValue(guid.ToString());
  829. else if(value is byte[] arr)
  830. {
  831. writer.WriteBase64StringValue(arr);
  832. }
  833. else if(value is Array array)
  834. {
  835. writer.WriteStartArray();
  836. foreach(var val1 in array)
  837. {
  838. WriteJson(writer, val1);
  839. }
  840. writer.WriteEndArray();
  841. }
  842. else if(value is Enum e)
  843. {
  844. WriteJson(writer, Convert.ChangeType(e, e.GetType().GetEnumUnderlyingType()));
  845. }
  846. else
  847. {
  848. Logger.Send(LogType.Error, "", $"Could not write object of type {value.GetType()} as JSON");
  849. }
  850. }
  851. protected void WriteJson(Utf8JsonWriter writer, string name, object value)
  852. {
  853. writer.WritePropertyName(name);
  854. WriteJson(writer,value);
  855. }
  856. }
  857. public class BaseObjectJSONConverter : CustomJsonConverter<BaseObject>
  858. {
  859. public override bool CanConvert(Type objectType)
  860. {
  861. return objectType.IsSubclassOf(typeof(BaseObject));
  862. }
  863. public override BaseObject? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  864. {
  865. BaseObject obj = (Activator.CreateInstance(typeToConvert) as BaseObject)!;
  866. obj.OriginalValues ??= new ConcurrentDictionary<string, object?>();
  867. obj.SetObserving(false);
  868. if (reader.TokenType == JsonTokenType.StartObject)
  869. {
  870. reader.Read();
  871. while (reader.TokenType != JsonTokenType.EndObject)
  872. {
  873. if (reader.TokenType != JsonTokenType.PropertyName)
  874. throw new JsonException("Expected PropertyName token.");
  875. string propertyName = reader.GetString();
  876. reader.Read(); // Advance to the property value
  877. if (Equals(propertyName, "OriginalValues"))
  878. {
  879. while (reader.Read())
  880. {
  881. if (reader.TokenType == JsonTokenType.EndObject)
  882. break;
  883. string? name = reader.GetString();
  884. reader.Read();
  885. if (!string.IsNullOrWhiteSpace(name))
  886. obj.OriginalValues[name] = ReadJson(ref reader);
  887. }
  888. }
  889. else if (DatabaseSchema.Property(typeToConvert, propertyName) is IProperty prop)
  890. {
  891. var value = ReadJson(ref reader);
  892. prop.Setter()(obj, value);
  893. }
  894. }
  895. }
  896. obj.SetObserving(true);
  897. return obj;
  898. }
  899. public override void Write(Utf8JsonWriter writer, BaseObject obj, JsonSerializerOptions options)
  900. {
  901. writer.WriteStartObject();
  902. writer.WritePropertyName("OriginalValues");
  903. writer.WriteStartObject();
  904. if (obj.OriginalValues != null)
  905. {
  906. foreach (var key in obj.OriginalValues.Keys)
  907. {
  908. var val = obj.OriginalValues[key];
  909. if (val == null)
  910. writer.WriteNull(key);
  911. else
  912. writer.WriteString(key, val.ToString());
  913. }
  914. }
  915. foreach(var property in DatabaseSchema.Properties(obj.GetType()))
  916. {
  917. var val = property.Getter()(obj);
  918. WriteJson(writer, property.Name, property.Getter());
  919. }
  920. writer.WriteEndObject();
  921. }
  922. }
  923. }