public static string Serialize(Unity.PackageManager.Ivy.ModuleRepository repo) { HasErrors = false; ErrorMessage = null; ErrorException = null; Unity.PackageManager.IvyInternal.ModuleRepository repository = Cloner.CloneObject <Unity.PackageManager.IvyInternal.ModuleRepository>(repo); IvyRoot o = new IvyRoot { Repository = repository }; StringBuilder builder = new StringBuilder(); using (UTF8StringWriter writer = new UTF8StringWriter(builder)) { XmlSerializable.GetSerializer(o.GetType()).Serialize(writer, o, Namespaces); } return(builder.ToString().Replace("<root>", "").Replace("</root>", "")); }
public static string Serialize(IvyModule module) { IvyParser.HasErrors = false; IvyParser.ErrorMessage = null; IvyParser.ErrorException = null; Unity.PackageManager.IvyInternal.IvyModule ivyModule = Cloner.CloneObject <Unity.PackageManager.IvyInternal.IvyModule>(module); IvyRoot ivyRoot = new IvyRoot(); ivyRoot.Module = ivyModule; StringBuilder stringBuilder = new StringBuilder(); using (UTF8StringWriter uTF8StringWriter = new UTF8StringWriter(stringBuilder)) { XmlSerializer serializer = XmlSerializable.GetSerializer(ivyModule.GetType()); serializer.Serialize(uTF8StringWriter, ivyModule, IvyParser.Namespaces); } return(stringBuilder.ToString().Replace("<root>", string.Empty).Replace("</root>", string.Empty)); }
public static T Deserialize <T>(string xml) where T : class { IvyParser.HasErrors = false; IvyParser.ErrorMessage = null; IvyParser.ErrorException = null; if (xml.Length <= 0) { IvyParser.HasErrors = true; IvyParser.ErrorMessage = "Cannot deserialize empty xml document."; return((T)((object)null)); } Type typeFromHandle = typeof(T); int startIndex = 0; int num = xml.IndexOf("<ivy-module"); int num2 = xml.IndexOf("<ivy-repository"); if (typeof(T) == typeof(IvyModule)) { if (num < 0) { return((T)((object)null)); } startIndex = num; typeFromHandle = typeof(IvyModule); } else if (typeof(T) == typeof(ModuleRepository)) { if (num2 < 0) { return((T)((object)null)); } startIndex = num2; typeFromHandle = typeof(ModuleRepository); } else if (typeof(T) == typeof(object)) { if (num2 >= 0) { startIndex = num2; typeFromHandle = typeof(ModuleRepository); } else if (num >= 0) { startIndex = num; typeFromHandle = typeof(IvyModule); } } xml = xml.Insert(startIndex, "<root>") + "</root>"; try { XmlTextReader xmlReader = new XmlTextReader(xml, XmlNodeType.Document, null); XmlSerializer serializer = XmlSerializable.GetSerializer(typeof(IvyRoot)); IvyRoot ivyRoot = serializer.Deserialize(xmlReader) as IvyRoot; T result; if (typeFromHandle == typeof(IvyModule)) { result = (Cloner.CloneObject(ivyRoot.Module, typeFromHandle) as T); return(result); } result = (Cloner.CloneObject(ivyRoot.Repository, typeFromHandle) as T); return(result); } catch (Exception errorException) { IvyParser.HasErrors = true; IvyParser.ErrorMessage = "Deserialization failed."; IvyParser.ErrorException = errorException; } return((T)((object)null)); }
public static T Deserialize <T>(string xml) where T : class { HasErrors = false; ErrorMessage = null; ErrorException = null; if (xml.Length <= 0) { HasErrors = true; ErrorMessage = "Cannot deserialize empty xml document."; return(null); } Type targetType = typeof(T); int startIndex = 0; int index = xml.IndexOf("<ivy-module"); int num3 = xml.IndexOf("<ivy-repository"); if (typeof(T) == typeof(Unity.PackageManager.Ivy.IvyModule)) { if (index < 0) { return(null); } startIndex = index; targetType = typeof(Unity.PackageManager.Ivy.IvyModule); } else if (typeof(T) == typeof(Unity.PackageManager.Ivy.ModuleRepository)) { if (num3 < 0) { return(null); } startIndex = num3; targetType = typeof(Unity.PackageManager.Ivy.ModuleRepository); } else if (typeof(T) == typeof(object)) { if (num3 >= 0) { startIndex = num3; targetType = typeof(Unity.PackageManager.Ivy.ModuleRepository); } else if (index >= 0) { startIndex = index; targetType = typeof(Unity.PackageManager.Ivy.IvyModule); } } xml = xml.Insert(startIndex, "<root>") + "</root>"; try { XmlTextReader xmlReader = new XmlTextReader(xml, XmlNodeType.Document, null); IvyRoot root = XmlSerializable.GetSerializer(typeof(IvyRoot)).Deserialize(xmlReader) as IvyRoot; if (targetType == typeof(Unity.PackageManager.Ivy.IvyModule)) { return(Cloner.CloneObject(root.Module, targetType) as T); } return(Cloner.CloneObject(root.Repository, targetType) as T); } catch (Exception exception) { HasErrors = true; ErrorMessage = "Deserialization failed."; ErrorException = exception; } return(null); }