For a strange reason that does not matter for this question I need to create a JSON compatible substring that represents a DateTime and that will be manually insterted into a larger JSON string that later will be parsed by .NET's DataContractJsonSerializer. I have come 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 can the code above be optimized in any way? Or is there even a mistake in the code above?
Also it would be really cool if I could do it without the use of DataContractJsonSerializer since the string building will also be done in a pure .NET 1.1 process.