Serialization.cs 33 KB

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