/// <summary> /// Returns an UTF-8 encoded JSON representing the calendar /// </summary> /// <param name="calendar"></param> /// <param name="indent"></param> /// <returns></returns> public static string ToJson(this Calendar calendar, bool indent = false) { if (calendar == null) { throw new ArgumentNullException(nameof(calendar)); } var document = new CalendarDocument(calendar); var ser = new DataContractJsonSerializer(typeof(CalendarDocument)); using var buffer = new MemoryStream(); using var writer = JsonReaderWriterFactory.CreateJsonWriter(buffer, Encoding.UTF8, true, indent); using var reader = new StreamReader(buffer); ser.WriteObject(writer, document); writer.Flush(); buffer.Position = 0; return(reader.ReadToEnd()); }
public static string ToXml(this Calendar calendar, XmlWriterSettings settings = null) { if (calendar == null) { throw new ArgumentNullException(nameof(calendar)); } var document = new CalendarDocument(calendar); var ns = new XmlSerializerNamespaces(); ns.Add("", "http://www.nexusat.it/schemas/calendar"); var ser = new XmlSerializer(typeof(CalendarDocument)); var sb = new StringBuilder(); using var writer = XmlWriter.Create(sb, settings); ser.Serialize(writer, document, ns); writer.Flush(); return(sb.ToString()); }