Allow unordered xml nodes, Ignore *Specified fields, Convert byte[] to base64

This commit is contained in:
Flamefire committed 2014-10-29 01:27:48 +01:00
1 parent 93a415205e
commit f563bae495
3 files changed
+177 -87

No files matched your search

+8 -4
View File
@@ -55,6 +55,7 @@ namespace VocaluxeLib.Xml
public bool IsList; //List with child elements (<List><El/><El/></List>)
public bool IsEmbeddedList; //List w/o child elements(<List/><List/>)
public bool IsDictionary; //List where the element names are the keys
public bool IsByteArray; // Byte arrays w/o XmlArrayAttribute are serialized as Base64-Encoded strings
public bool IsNullable;
public bool IsNormalized;
public XmlRangedAttribute Ranged;
@@ -83,7 +84,7 @@ namespace VocaluxeLib.Xml
public object GetValue(object o)
{
return (_Field != null) ?_Field.GetValue(o) :_Property.GetValue(o, new object[] {});
return (_Field != null) ? _Field.GetValue(o) : _Property.GetValue(o, new object[] {});
}
}
@@ -132,7 +133,10 @@ namespace VocaluxeLib.Xml
if (!info.IsList)
{
Debug.Assert(!field.HasAttribute<XmlArrayAttribute>(), "A field cannot have an XmlElement- and XmlArray-Attribute");
info.IsEmbeddedList = true;
if (info.Type.IsArray && info.SubType == typeof(byte))
info.IsByteArray = true;
else
info.IsEmbeddedList = true;
}
else
{
@@ -181,7 +185,7 @@ namespace VocaluxeLib.Xml
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
if (field.HasAttribute<XmlIgnoreAttribute>())
if (field.HasAttribute<XmlIgnoreAttribute>() || field.Name.EndsWith("Specified"))
continue;
SFieldInfo info = new SFieldInfo(field);
_FillInfo(ref info, field);
@@ -190,7 +194,7 @@ namespace VocaluxeLib.Xml
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
{
if (property.HasAttribute<XmlIgnoreAttribute>())
if (property.HasAttribute<XmlIgnoreAttribute>() || property.Name.EndsWith("Specified") || !property.CanRead || !property.CanWrite)
continue;
SFieldInfo info = new SFieldInfo(property);
_FillInfo(ref info, property);
+55 -46
View File
@@ -227,55 +227,47 @@ namespace VocaluxeLib.Xml
nodes = parent.ChildNodes;
List<SFieldInfo> fields = result.GetType().GetFields(attributes);
int curField = 0;
if (nodes != null)
{
List<object> subValues = new List<object>();
SFieldInfo curListField = new SFieldInfo();
string curListName = null;
//Dictionary of all embedded lists to allow interleaved/mixed elements
Dictionary<string, Tuple<SFieldInfo, List<object>>> embLists = new Dictionary<string, Tuple<SFieldInfo, List<object>>>();
foreach (XmlNode node in nodes)
{
if (node is XmlComment)
continue;
if (curListName != null)
{
if (node.Name != curListName && !node.Name.StartsWith(curListField.Name))
{
// End an embedded List
_AddList(result, curListField, subValues);
curField++;
curListName = null;
}
else
{
// Continue an embedded List
object subValue = _GetValue(node, curListField.SubType);
subValues.Add(subValue);
continue;
}
}
SFieldInfo field = new SFieldInfo();
for (; curField < fields.Count; curField++)
int curField;
for (curField = 0; curField < fields.Count; curField++)
{
field = fields[curField];
if (field.Name == node.Name || field.AltName == node.Name || (field.IsEmbeddedList && node.Name.StartsWith(field.Name)))
break;
if (!_CheckAndSetDefaultValue(result, field))
throw new XmlException("Unexpected element: " + _GetXPath(node) + "; Expected: " + field.Name);
}
if (curField >= fields.Count)
throw new XmlException("Unexpected element: " + _GetXPath(node));
if (field.IsEmbeddedList)
{
// Start an embedded List
curListField = field;
curListName = field.Name;
subValues.Clear();
object subValue = _GetValue(node, curListField.SubType);
subValues.Add(subValue);
string msg = "Unexpected element: " + _GetXPath(node);
if (fields.Count > 0)
msg += " Expected: " + string.Join(", ", fields.Select(f => f.Name));
throw new XmlException(msg);
}
if (field.IsByteArray)
{
field.SetValue(result, Convert.FromBase64String(node.InnerText));
fields.RemoveAt(curField);
}
else if (field.IsEmbeddedList)
{
Tuple<SFieldInfo, List<object>> entry;
if (!embLists.TryGetValue(field.Name, out entry))
{
entry = new Tuple<SFieldInfo, List<object>>(field, new List<object>());
embLists.Add(field.Name, entry);
}
object subValue = _GetValue(node, field.SubType);
entry.Item2.Add(subValue);
}
else
{
@@ -304,19 +296,18 @@ namespace VocaluxeLib.Xml
}
}
field.SetValue(result, value);
curField++;
fields.RemoveAt(curField);
}
}
if (curListName != null)
//Add embedded lists
foreach (Tuple<SFieldInfo, List<object>> entry in embLists.Values)
{
// End an embedded List
_AddList(result, curListField, subValues);
curField++;
_AddList(result, entry.Item1, entry.Item2);
fields.Remove(entry.Item1);
}
}
for (; curField < fields.Count; curField++)
foreach (SFieldInfo field in fields)
{
SFieldInfo field = fields[curField];
if (!_CheckAndSetDefaultValue(result, field))
throw new XmlException("element: " + field.Name + " is missing in " + _GetXPath(parent));
}
@@ -395,6 +386,8 @@ namespace VocaluxeLib.Xml
foreach (object subValue in values)
_WriteNode(writer, field.Name, field.SubType, subValue, field.IsAttribute);
}
else if (field.IsByteArray)
writer.WriteElementString(field.Name, Convert.ToBase64String((byte[])value));
else
_WriteNode(writer, field.Name, field.Type, value, field.IsAttribute, field.ArrayItemName);
}
@@ -410,11 +403,23 @@ namespace VocaluxeLib.Xml
xDoc.Load(reader);
if (xDoc.DocumentElement == null)
throw new XmlException("No root element found!");
_ReadChildNodes(xDoc.DocumentElement, ref result, false);
try
{
_ReadChildNodes(xDoc.DocumentElement, ref result, false);
}
catch (TargetInvocationException e)
{
throw e.InnerException ?? e;
}
return (T)result;
}
public T DeserializeString<T>(string xml) where T : new()
{
return DeserializeString(xml, new T());
}
public T DeserializeString<T>(string xml, T o) where T : new()
{
var reader = new XmlTextReader(new StringReader(xml))
{
@@ -424,7 +429,7 @@ namespace VocaluxeLib.Xml
};
try
{
return _Deserialize<T>(reader, new T());
return _Deserialize<T>(reader, o);
}
finally
{
@@ -432,11 +437,15 @@ namespace VocaluxeLib.Xml
}
}
public T Deserialize<T>(string filePath, object o = null) where T : new()
public T Deserialize<T>(string filePath) where T : new()
{
Debug.Assert(o == null || o.GetType() is T);
if (o == null)
o = new T();
return Deserialize(filePath, new T());
}
public T Deserialize<T>(string filePath, T o) where T : new()
{
if (!File.Exists(filePath))
throw new FileNotFoundException(filePath);
var reader = new XmlTextReader(filePath)
{
WhitespaceHandling = WhitespaceHandling.Significant,
+114 -37
View File
@@ -71,26 +71,26 @@ namespace VocaluxeTests
public int Auto { get; set; }
// ReSharper restore UnusedAutoPropertyAccessor.Local
}
private struct SBytes
{
public byte[] B;
}
private struct SIgnore
{
[XmlIgnore] public int I;
public int J;
}
private class CIgnore
{
[XmlIgnore] public int I;
public int J;
}
#pragma warning restore 169
#pragma warning restore 649
[TestMethod]
public void TestBasic()
{
const string s = @"<root>
<I>1</I>
<S>2</S>
<F>3</F>
<D>4</D>
</root>";
var xml = new CXmlSerializer();
SBasic foo = xml.DeserializeString<SBasic>(s);
Assert.AreEqual(1, foo.I);
Assert.AreEqual("2", foo.S);
Assert.AreEqual(3, foo.F, 0.0001);
Assert.AreEqual(4, foo.D, 0.0001);
}
private static void _AssertFail<T>(Action test) where T : Exception
{
try
@@ -104,6 +104,49 @@ namespace VocaluxeTests
}
}
private static void _AssertFail<T, T2>(String xmlString) where T2 : Exception where T : new()
{
var xml = new CXmlSerializer();
_AssertFail<T2>(() => xml.DeserializeString<T>(xmlString));
}
private static T _AssertSerDeserMatch<T>(string xmlString) where T : new()
{
var xml = new CXmlSerializer();
T foo = xml.DeserializeString<T>(xmlString);
string xmlNew = xml.Serialize(foo);
Assert.AreEqual(xmlString, xmlNew);
return foo;
}
[TestMethod]
public void TestBasic()
{
const string s = @"<root>
<I>1</I>
<S>2</S>
<F>3</F>
<D>4</D>
</root>";
const string sUnordered = @"<root>
<D>4</D>
<I>1</I>
<F>3</F>
<S>2</S>
</root>";
var xml = new CXmlSerializer();
SBasic foo = xml.DeserializeString<SBasic>(s);
Assert.AreEqual(1, foo.I);
Assert.AreEqual("2", foo.S);
Assert.AreEqual(3, foo.F, 0.0001);
Assert.AreEqual(4, foo.D, 0.0001);
foo = xml.DeserializeString<SBasic>(sUnordered);
Assert.AreEqual(1, foo.I);
Assert.AreEqual("2", foo.S);
Assert.AreEqual(3, foo.F, 0.0001);
Assert.AreEqual(4, foo.D, 0.0001);
}
[TestMethod]
public void TestMissingXmlElement()
{
@@ -120,11 +163,10 @@ namespace VocaluxeTests
<S>2</S>
<F>3</F>
</root>"};
var xml = new CXmlSerializer();
foreach (string s1 in s)
{
string sTmp = s1;
_AssertFail<XmlException>(() => xml.DeserializeString<SBasic>(sTmp));
_AssertFail<SBasic, XmlException>(sTmp);
}
}
@@ -150,11 +192,10 @@ namespace VocaluxeTests
<D>4</D>
<Foo>1</Foo>
</root>"};
var xml = new CXmlSerializer();
foreach (string s1 in s)
{
string sTmp = s1;
_AssertFail<XmlException>(() => xml.DeserializeString<SBasic>(sTmp));
_AssertFail<SBasic, XmlException>(sTmp);
}
}
@@ -167,10 +208,7 @@ namespace VocaluxeTests
<F>3</F>
<D>4</D>
</root>";
var xml = new CXmlSerializer();
SBasic foo = xml.DeserializeString<SBasic>(s);
string s2 = xml.Serialize(foo);
Assert.AreEqual(s, s2);
_AssertSerDeserMatch<SBasic>(s);
}
private readonly string[] _XMLList = new string[]
@@ -212,7 +250,7 @@ namespace VocaluxeTests
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization2 failed");
res = xml.Serialize(foo);
Assert.AreEqual(_XMLList[2], res, "Serialization2 failed");
_AssertFail<XmlException>(() => xml.DeserializeString<SList>(_Empty));
_AssertFail<SList, XmlException>(_Empty);
}
[TestMethod]
@@ -233,7 +271,7 @@ namespace VocaluxeTests
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization2 failed");
res = xml.Serialize(foo);
Assert.AreEqual(_XMLList[2], res, "Serialization2 failed");
_AssertFail<XmlException>(() => xml.DeserializeString<SArray>(_Empty));
_AssertFail<SArray, XmlException>(_Empty);
}
private const string _XMLListEmb = _Head + @"<root>
@@ -276,7 +314,7 @@ namespace VocaluxeTests
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization failed");
res = xml.Serialize(foo);
Assert.AreEqual(_XMLListEmb3, res, "Serialization failed");
_AssertFail<XmlException>(() => xml.DeserializeString<SListEmb>(_Empty));
_AssertFail<SListEmb, XmlException>(_Empty);
}
[TestMethod]
@@ -297,23 +335,62 @@ namespace VocaluxeTests
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization failed");
res = xml.Serialize(foo);
Assert.AreEqual(_XMLListEmb3, res, "Serialization failed");
_AssertFail<XmlException>(() => xml.DeserializeString<SArrayEmb>(_Empty));
_AssertFail<SArrayEmb, XmlException>(_Empty);
}
private const string _XmlProperty = _Head + @"<root>
<Public>2</Public>
<Auto>3</Auto>
</root>";
[TestMethod]
public void TestProperty()
{
const string xmlString = _Head + @"<root>
<Public>2</Public>
<Auto>3</Auto>
</root>";
var xml = new CXmlSerializer();
SProperty foo = xml.DeserializeString<SProperty>(_XmlProperty);
SProperty foo = xml.DeserializeString<SProperty>(xmlString);
Assert.AreEqual(3, foo.Private);
Assert.AreEqual(3, foo.Auto);
string newXml = xml.Serialize(foo);
Assert.AreEqual(_XmlProperty, newXml);
Assert.AreEqual(xmlString, newXml);
}
[TestMethod]
public void TestByteArray()
{
const string xmlString = _Head + @"<root>
<B>MTMzNw==</B>
</root>";
_AssertSerDeserMatch<SBytes>(xmlString);
}
private const string _XmlIgnore = _Head + @"<root>
<J>2</J>
</root>";
private const string _XmlIgnore2 = _Head + @"<root>
<I>2</I>
<J>2</J>
</root>";
[TestMethod]
public void TestIgnore()
{
_AssertSerDeserMatch<SIgnore>(_XmlIgnore);
_AssertFail<SIgnore, XmlException>(_XmlIgnore2);
}
[TestMethod]
public void TestExisting()
{
var foo = new SIgnore {I = 1};
var xml = new CXmlSerializer();
SIgnore bar = xml.DeserializeString(_XmlIgnore, foo);
Assert.AreEqual(1, bar.I);
Assert.AreEqual(_XmlIgnore, xml.Serialize(bar));
var foo2 = new CIgnore {I = 1};
CIgnore bar2 = xml.DeserializeString(_XmlIgnore, foo2);
Assert.AreEqual(1, bar2.I);
Assert.AreEqual(foo2.J, bar2.J, "Original classes should be modified by the deserialization");
Assert.AreEqual(_XmlIgnore, xml.Serialize(bar2));
}
[TestMethod]
@@ -325,7 +402,7 @@ namespace VocaluxeTests
{
string xmlPath = Path.Combine(filePath, type.Name + ".xml");
var xml = new CXmlSerializer(type == typeof(CConfig.SConfig));
object foo = xml.GetType().GetMethod("Deserialize").MakeGenericMethod(new Type[] {type}).Invoke(xml, new object[] {xmlPath});
object foo = xml.Deserialize(xmlPath, Activator.CreateInstance(type));
Assert.IsInstanceOfType(foo, type);
string newXml = xml.Serialize(foo);
string oldXml = File.ReadAllText(xmlPath);