For some strange reason, it doesnβt matter for this question, I need to create a JSON-compatible substring that represents DateTime and that will be manually entered into a large JSON string that will be parsed later by .NET DataContractJsonSerializer. I came up with the following method:
static readonly DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(DateTime)); private static string ToJsonString(DateTime time) { using (var memStream = new MemoryStream()) { s.WriteObject(memStream, time); return Encoding.UTF8.GetString(memStream.ToArray()); } }
Is there any simpler way to do this or is it possible to optimize the code above? Or is there even an error in the code above?
It would also be great if I could do this without using the DataContractJsonSerializer, since line building would also be done in a pure .NET 1.1 process.
source share