-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathSyncObjectSerializer.cs
More file actions
96 lines (78 loc) · 3.28 KB
/
SyncObjectSerializer.cs
File metadata and controls
96 lines (78 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.Extensions.Logging;
using System.Runtime.Serialization;
using System.Xml.Linq;
using uSync.Core;
using uSync.Core.Models;
using uSync.Core.Serialization;
namespace uSync.Extend;
/// <summary>
/// a generic seriazlier that will serialize any object,
/// </summary>
public abstract class SyncObjectSerializer<TObject> : SyncSerializerRoot<TObject>, ISyncSerializer<TObject>
{
protected SyncObjectSerializer(ILogger<SyncSerializerRoot<TObject>> logger) : base(logger)
{ }
public abstract TObject CreateItem(XElement node);
protected virtual void SetKey(TObject item, Guid key)
{
var property = typeof(TObject).GetProperty("Key");
if (property != null && property.CanWrite)
{
property.SetValue(item, key);
}
}
protected virtual void SetAlias(TObject item, string alias)
{
var property = typeof(TObject).GetProperty("Alias");
if (property != null && property.CanWrite)
{
property.SetValue(item, alias);
}
}
protected override async Task<SyncAttempt<TObject>> DeserializeCoreAsync(XElement node, SyncSerializerOptions options)
{
var item = (await FindItemAsync(node)) ?? CreateItem(node);
SetKey(item, node.GetKey());
SetAlias(item, node.GetAlias());
var propertyNode = node.Element("Properties");
if (propertyNode != null)
{
var properties = typeof(TObject).GetProperties();
foreach (var property in properties)
{
var valueNode = propertyNode.Element(property.Name);
if (valueNode != null)
{
var currentValue = property.GetValue(item)?.ToString() ?? string.Empty;
if (valueNode.Value != currentValue)
{
var value = Convert.ChangeType(valueNode.Value, property.PropertyType);
property.SetValue(item, value);
}
}
}
}
return SyncAttempt<TObject>.Succeed(ItemAlias(item), item, ChangeType.Import, []);
}
protected override Task<SyncAttempt<XElement>> SerializeCoreAsync(TObject item, SyncSerializerOptions options)
{
if (item == null)
return Task.FromResult(SyncAttempt<XElement>.Fail(string.Empty, null, ChangeType.Fail, "Item is null", new ArgumentNullException(nameof(item))));
var node = new XElement(ItemType,
new XAttribute(uSyncConstants.Xml.Key, ItemKey(item)),
new XAttribute(uSyncConstants.Xml.Alias, ItemAlias(item)));
var propertyNode = new XElement("Properties");
Type objectType = item.GetType();
var properies = objectType.GetProperties();
foreach (var property in properies)
{
// ignore if the object has "IgnoreDataMember" attribute
if (property.GetCustomAttributes(typeof(IgnoreDataMemberAttribute), true).Length > 0)
continue;
var value = property.GetValue(item)?.ToString() ?? string.Empty;
propertyNode.Add(new XElement(property.Name, value));
}
node.Add(propertyNode);
return Task.FromResult(SyncAttempt<XElement>.Succeed(ItemAlias(item), node, ChangeType.Export, []));
}
}