##// END OF EJS Templates
Fixed promise rejection when there is not specified error handler in the reaction....
Fixed promise rejection when there is not specified error handler in the reaction. FIXED SPELLING IN THE XML CONTAINER CONFIGURATION signleton->singleton Code cleanup Update tests make them working on dotnet core

File last commit:

r295:28af686e24f7 default
r295:28af686e24f7 default
Show More
SerializationHelpers.cs
88 lines | 3.2 KiB | text/x-csharp | CSharpLexer
/ Implab / src / Xml / SerializationHelpers.cs
cin
Fixed promise rejection when there is not specified error handler in the reaction....
r295 using System.IO;
cin
Added tests for Implab.ServiceHost.Unity configuration loader.
r289 using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace Implab.Xml {
public static class SerializationHelpers {
public static string SerializeAsString<T>(T obj) {
return XmlDefaultSerializer<T>.Instance.SerializeAsString(obj);
}
public static void Serialize<T>(XmlWriter writer, T obj) {
XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
}
public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
var doc = new XmlDocument();
using (var writer = doc.CreateNavigator().AppendChild()) {
XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
}
return doc;
}
public static XDocument SerializeAsXDocument<T>(T obj) {
var doc = new XDocument();
using (var writer = doc.CreateWriter()) {
XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
}
return doc;
}
public static void SerializeToFile<T>(string file, T obj) {
XmlDefaultSerializer<T>.Instance.SerializeToFile(obj, file);
}
public static void SerializeToElementChild<T>(XmlElement element, T obj) {
XmlDefaultSerializer<T>.Instance.Serialize(obj, element);
}
public static T Deserialize<T>(XmlReader reader) {
return (T)XmlDefaultSerializer<T>.Instance.Deserialize(reader);
}
public static T DeserializeFromFile<T>(string file) {
return (T)XmlDefaultSerializer<T>.Instance.DeserializeFromFile(file);
}
public static T DeserializeFromString<T>(string data) {
return (T)XmlDefaultSerializer<T>.Instance.DeserializeFromString(data);
}
public static T DeserializeFromXmlNode<T>(XmlNode node) {
Safe.ArgumentNotNull(node, nameof(node));
using (var reader = node.CreateNavigator().ReadSubtree())
return (T)XmlDefaultSerializer<T>.Instance.Deserialize(reader);
}
public static T DeserializeJson<T>(TextReader textReader) {
var options = new JsonXmlReaderOptions {
NamespaceUri = typeof(T).GetCustomAttribute<XmlRootAttribute>()?.Namespace,
RootName = typeof(T).Name,
FlattenArrays = true
};
using (var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options))
return Deserialize<T>(reader);
}
public static T DeserializeJsonFromString<T>(string data) {
using (var reader = new StringReader(data)) {
return DeserializeJson<T>(reader);
}
}
public static void SerializeJson<T>(TextWriter writer, T obj) {
var doc = SerializeAsXmlDocument(obj);
XmlToJson.Default.Transform(doc, null, writer);
}
public static string SerializeJsonAsString<T>(T obj) {
using (var writer = new StringWriter()) {
SerializeJson(writer, obj);
return writer.ToString();
}
}
}
}