public string Serialize <T> (T obj, bool readableOutput = false) where T : class, new() { try { System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType(), ExtraTypes.ToArray()); using (StringWriter writer = new StringWriter()) { XmlWriterSettings settings = (readableOutput ? settingsForReadableOutput : settingsForCompactOutput); using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings)) { xmlSerializer.Serialize(xmlWriter, obj); string xml = writer.ToString(); //DebugLog.Info("To XML: " + xml); return(xml); } } } catch (Exception e) { DebugLog.Error("Unable to serialize {0}. XML serialize error: {1}", typeof(T).Name, DebugLog.GetNestedMessages(e)); throw new Exception("Unable to serialize to XML: " + DebugLog.SafeName(obj), e); } }
public string Serialize <T> (T obj, bool readableOutput = false) where T : class, new() { try { Formatting formatting = (readableOutput ? Formatting.Indented : Formatting.None); string json = JsonConvert.SerializeObject(obj, formatting, settings); //DebugLog.Info(Subsystem.Serialization, "Serialized JSON:\n{0}", json); return(json); } catch (Exception e) { throw new Exception("Unable to serialize to JSON: " + DebugLog.SafeName(obj), e); } }