Use XML serialization instead of JSON serialization

XML serialization works better than JSON for Android designer
data - it's formatted better and is more mature, with more
options supported.
This commit is contained in:
Bret Johnson 2023-02-20 18:01:10 -05:00
Родитель 0ed0b9202c
Коммит 6ed2ea5ad4
1 изменённых файлов: 17 добавлений и 5 удалений

Просмотреть файл

@ -31,7 +31,8 @@ using System.IO;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using Xwt.Drawing; using Xwt.Drawing;
using Xwt.Backends; using Xwt.Backends;
using System.Text.Json; using System.Xml.Serialization;
using System.Text;
namespace Xwt namespace Xwt
{ {
@ -140,23 +141,34 @@ namespace Xwt
} }
/// <summary> /// <summary>
/// Serializes a value to a byte array using <see cref="System.Text.Json.JsonSerializer"/> . /// Serializes a value to a byte array using <see cref="System.Xml.XmlSerializer"/> .
/// </summary> /// </summary>
/// <returns>The serialized value.</returns> /// <returns>The serialized value.</returns>
/// <param name="val">The value to serialize.</param> /// <param name="val">The value to serialize.</param>
public static byte[] SerializeValue (object val, Type type) public static byte[] SerializeValue (object val, Type type)
{ {
return JsonSerializer.SerializeToUtf8Bytes (val, type); using (var stream = new MemoryStream ()) {
using (var writer = new StreamWriter (stream, new UTF8Encoding ())) {
var xmlSerializer = new XmlSerializer (type);
xmlSerializer.Serialize (writer, val);
}
return stream.ToArray ();
}
} }
/// <summary> /// <summary>
/// Deserializes a value from a byte array. /// Deserializes a value from a byte array.
/// </summary> /// </summary>
/// <returns>The deserialized value.</returns> /// <returns>The deserialized value.</returns>
/// <param name="data">The byte array containing the Utf8 Json serialized value.</param> /// <param name="data">The byte array containing the Utf8 XML serialized value.</param>
public static object DeserializeValue (byte[] data, Type type) public static object DeserializeValue (byte[] data, Type type)
{ {
return JsonSerializer.Deserialize (data, type); using (var stream = new MemoryStream (data)) {
using (var reader = new StreamReader (stream, new UTF8Encoding ())) {
var xmlSerializer = new XmlSerializer (type);
return xmlSerializer.Deserialize (reader);
}
}
} }
/// <summary> /// <summary>