本文整理汇总了C#中Response.ToJson方法的典型用法代码示例。如果您正苦于以下问题:C# Response.ToJson方法的具体用法?C# Response.ToJson怎么用?C# Response.ToJson使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Response
的用法示例。
在下文中一共展示了Response.ToJson方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Can_serialize_custom_DateTime2
public void Can_serialize_custom_DateTime2()
{
JsConfig<DateTime>.SerializeFn = time =>
{
var x = new DateTime(time.Ticks, DateTimeKind.Unspecified).ToString("o");
return x;
};
JsConfig<DateTime>.DeSerializeFn = time =>
{
var x = DateTime.ParseExact(time, "o", null);
return x;
};
var dateTime = new DateTime(2015, 08, 12, 12, 12, 12, DateTimeKind.Unspecified);
var json = dateTime.ToJson();
Assert.That(json, Is.EqualTo("\"2015-08-12T12:12:12.0000000\""));
var fromJson = json.FromJson<DateTime>();
Assert.That(fromJson, Is.EqualTo(dateTime));
var dto = new Response
{
DateTime = dateTime,
};
json = dto.ToJson();
Assert.That(json, Is.EqualTo("{\"DateTime\":\"2015-08-12T12:12:12.0000000\"}"));
Assert.That(json.FromJson<Response>().DateTime, Is.EqualTo(dateTime));
JsConfig<DateTime>.SerializeFn = null;
JsConfig<DateTime>.DeSerializeFn = null;
JsConfig.Reset();
}
示例2: Can_serialize_custom_DateTime
public void Can_serialize_custom_DateTime()
{
JsConfig<DateTime>.SerializeFn = time =>
{
var result = time;
if (time.Kind == DateTimeKind.Unspecified)
{
result = DateTime.SpecifyKind(result, DateTimeKind.Local);
}
return result.ToString(CultureInfo.InvariantCulture);
};
var dto = new Response { DateTime = new DateTime(2001, 1, 1, 1, 1, 1) };
var csv = dto.ToCsv();
Assert.That(csv, Is.EqualTo("DateTime\r\n01/01/2001 01:01:01\r\n"));
var json = dto.ToJson();
Assert.That(json, Is.EqualTo("{\"DateTime\":\"01/01/2001 01:01:01\"}"));
JsConfig<DateTime>.SerializeFn = null;
JsConfig.Reset();
}