Commit e79230b1 authored by Thomas's avatar Thomas
Browse files

Use JSON instead of XML to serialize patches

parent 06436df5
using System; using pEp.Extensions;
using System;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
......
using System; using pEp.Extensions;
using System.IO; using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace pEp.DPE namespace pEp.DPE
{ {
...@@ -62,54 +59,13 @@ namespace pEp.DPE ...@@ -62,54 +59,13 @@ namespace pEp.DPE
} }
/// <summary> /// <summary>
/// Serializes this patch into XML format. /// Deserializes a JSON string into a Patch object.
/// </summary> /// </summary>
/// <returns>The serialized object as XML string.</returns> /// <param name="json">The JSON string to process.</param>
public string Serialize()
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
{
//Indent = true,
OmitXmlDeclaration = true,
Encoding = Encoding.UTF8,
CheckCharacters = false
};
using (StringWriter stringWriter = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(stringWriter, xmlWriterSettings))
{
XmlSerializer serializer = new XmlSerializer(typeof(Patch));
serializer.Serialize(writer, this, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));
return stringWriter.ToString();
}
}
/// <summary>
/// Deserializes an XML string into a Patch object.
/// </summary>
/// <param name="xml">The XML to process.</param>
/// <returns>The Patch object or null if an error occured.</returns> /// <returns>The Patch object or null if an error occured.</returns>
public static Patch Deserialize(string xml) public static Patch Deserialize(string json)
{ {
if (string.IsNullOrEmpty(xml)) return json.Deserialize<Patch>();
{
Log.Info("Deserialize: XML is empty.");
return null;
}
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Patch));
using (StringReader stringReader = new StringReader(xml))
{
try
{
return xmlSerializer.Deserialize(stringReader) as Patch;
}
catch (Exception ex)
{
Log.Error("Deserialize: Error deserializing Patch object: " + ex.ToString());
return null;
}
}
} }
} }
} }
using System; using Newtonsoft.Json;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace pEp.Extensions namespace pEp.Extensions
{ {
internal static class TypeExtensions internal static class TypeExtensions
{ {
/// <summary> /// <summary>
/// Converts an object to its serialized XML format. /// Deserializes a JSON string into a given object.
/// </summary> /// </summary>
/// <typeparam name="T">The type of object we are operating on</typeparam> /// <param name="json">The json string to process.</param>
/// <param name="value">The object we are operating on</param> /// <returns>The deserialized object or null if an error occured.</returns>
/// <returns>The XML string representation of the object</returns> public static T Deserialize<T>(this string json) where T : class
public static string Serialize<T>(this T value, bool indent = true) where T : class
{ {
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings if (string.IsNullOrEmpty(json))
{
Indent = indent,
OmitXmlDeclaration = true,
CheckCharacters = false,
Encoding = Encoding.UTF8
};
using (StringWriter stringWriter = new StringWriter())
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings))
{ {
XmlSerializer serializer = new XmlSerializer(value.GetType()); Log.Info("Deserialize: json is empty.");
return null;
try
{
serializer.Serialize(xmlWriter, value, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));
}
catch (Exception ex)
{
Log.Error("Serialize: Error occured. " + ex.ToString());
return null;
}
return stringWriter.ToString();
} }
return JsonConvert.DeserializeObject<T>(json);
} }
/// <summary> /// <summary>
/// Deserializes an XML string into a given object. /// Serializes a given object into a´JSON string.
/// </summary> /// </summary>
/// <param name="xml">The XML to process.</param> /// <typeparam name="T">The type of the object to serialize.</typeparam>
/// <returns>The deserialized object or null if an error occured.</returns> /// <param name="obj">The object to serialize.</param>
public static object Deserialize(Type type, string xml) /// <returns>The serialized string or null if an error occured.</returns>
public static string Serialize<T>(this T obj)
{ {
if (string.IsNullOrEmpty(xml)) return JsonConvert.SerializeObject(obj);
{
Log.Info("Deserialize: XML is empty.");
return null;
}
XmlSerializer xmlSerializer = new XmlSerializer(type);
using (StringReader stringReader = new StringReader(xml))
{
try
{
return xmlSerializer.Deserialize(stringReader);
}
catch (Exception ex)
{
Log.Error("Deserialize: Error deserializing Patch object: " + ex.ToString());
return null;
}
}
} }
} }
} }
...@@ -66,8 +66,8 @@ ...@@ -66,8 +66,8 @@
BorderBrush="Black" BorderBrush="Black"
BorderThickness="1" BorderThickness="1"
Grid.Row="1" Grid.Row="1"
Height="400" MinHeight="400"
Width="600" MinWidth="600"
Margin="5,10,10,10" Margin="5,10,10,10"
Document="{Binding DisplayDiff}" Document="{Binding DisplayDiff}"
Visibility="{Binding IsEditable, Converter={StaticResource InvertBoolToVisibility}}"/> Visibility="{Binding IsEditable, Converter={StaticResource InvertBoolToVisibility}}"/>
......
...@@ -267,6 +267,9 @@ ...@@ -267,6 +267,9 @@
<Reference Include="MimeKitLite, Version=2.10.0.0, Culture=neutral, PublicKeyToken=bede1c8a46c66814, processorArchitecture=MSIL"> <Reference Include="MimeKitLite, Version=2.10.0.0, Culture=neutral, PublicKeyToken=bede1c8a46c66814, processorArchitecture=MSIL">
<HintPath>..\packages\MimeKitLite.2.10.1\lib\net45\MimeKitLite.dll</HintPath> <HintPath>..\packages\MimeKitLite.2.10.1\lib\net45\MimeKitLite.dll</HintPath>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"> <Reference Include="Office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<EmbedInteropTypes>True</EmbedInteropTypes> <EmbedInteropTypes>True</EmbedInteropTypes>
</Reference> </Reference>
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<packages> <packages>
<package id="Microsoft.VisualStudio.OLE.Interop" version="16.7.30328.74" targetFramework="net45" /> <package id="Microsoft.VisualStudio.OLE.Interop" version="16.7.30328.74" targetFramework="net45" />
<package id="MimeKitLite" version="2.10.1" targetFramework="net45" /> <package id="MimeKitLite" version="2.10.1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net45" />
<package id="Stub.System.Data.SQLite.Core.NetFramework" version="1.0.113.3" targetFramework="net45" /> <package id="Stub.System.Data.SQLite.Core.NetFramework" version="1.0.113.3" targetFramework="net45" />
<package id="System.Buffers" version="4.5.1" targetFramework="net45" /> <package id="System.Buffers" version="4.5.1" targetFramework="net45" />
<package id="System.Data.SQLite.Core" version="1.0.113.7" targetFramework="net45" /> <package id="System.Data.SQLite.Core" version="1.0.113.7" targetFramework="net45" />
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment