Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何告诉ServiceStack不要在我的列表中删除空值?_C#_Json_Datetime_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack - Fatal编程技术网 servicestack,C#,Json,Datetime,servicestack" /> servicestack,C#,Json,Datetime,servicestack" />

C# 如何告诉ServiceStack不要在我的列表中删除空值?

C# 如何告诉ServiceStack不要在我的列表中删除空值?,c#,json,datetime,servicestack,C#,Json,Datetime,servicestack,我有一个问题,列表中的null datetime没有被正确序列化/反序列化 请参见此示例代码: internal class WrapperNullableDateTimeList { public List<DateTime?> MyList { get; set; } } public void T14_Should_skip_output_nullable_datetime_in_list_TODO_THIS_IS_WRONG() { Wr

我有一个问题,列表中的null datetime没有被正确序列化/反序列化

请参见此示例代码:

internal class WrapperNullableDateTimeList
{
    public List<DateTime?> MyList { get; set; }
}

public void T14_Should_skip_output_nullable_datetime_in_list_TODO_THIS_IS_WRONG()
     {
         WrapperNullableDateTimeList input = new WrapperNullableDateTimeList();
         input.MyList = new List<DateTime?>()
         {
           new DateTime(2000, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc),
           null,
           new DateTime(2000, 12, 31, 0, 0, 0, 0, DateTimeKind.Utc),
         };

         JsConfig.IncludeNullValues = true;
         var serializer = new JsonSerializer<WrapperNullableDateTimeList>();
         string serializedInput = serializer.SerializeToString(input);

         WrapperNullableDateTimeList output = serializer.DeserializeFromString(serializedInput);

         output.MyList.Count.Should().Be(3); // Fails here! The 'null' DateTime in the list is dropped
     }
内部类包装器NullableDateTimeList
{
公共列表MyList{get;set;}
}
public void T14\u应\u跳过\u输出\u可为空\u列表中的日期时间\u TODO\u此\u错误()
{
WrapperNullableDateTimeList输入=新的WrapperNullableDateTimeList();
input.MyList=新列表()
{
新的日期时间(2000,01,01,0,0,0,0,DateTimeKind.Utc),
无效的
新的日期时间(2000、12、31、0、0、0、0、DateTimeKind.Utc),
};
JsConfig.IncludeNullValues=true;
var serializer=new JsonSerializer();
string serializedInput=serializer.SerializeToString(输入);
WrapperNullableDateTimeList输出=serializer.DeserializeFromString(serializedInput);
output.MyList.Count.Should()
}
有什么想法吗? 顺便说一句,我打印了serializedInput(json),它如下所示:

{“MyList”:[“2000-01-01T00:00:00.0000000 Z”,null,“2000-12-31T00:00:00.0000000 Z”]


我的JsConfig包含空值。。。那么是什么呢?

这在最新版本的ServiceStack中有效:

using (JsConfig.With(new Config { includeNullValues = true }))
{
    var dto = new WrapperNullableDateTimeList
    {
        MyList = new List<DateTime?>
        {
            new DateTime(2000, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc),
            null,
            new DateTime(2000, 12, 31, 0, 0, 0, 0, DateTimeKind.Utc),
        }
    };

    var json = dto.ToJson();
    json.Print();

    Assert.That(json, Is.EqualTo(
      @"{""MyList"":[""\/Date(946684800000)\/"",null,""\/Date(978220800000)\/""]}"));

    var fromJson = json.FromJson<WrapperNullableDateTimeList>();

    Assert.That(fromJson.MyList.Count, Is.EqualTo(dto.MyList.Count));
}
使用(JsConfig.With(新配置{includeNullValues=true}))
{
var dto=新包装器NullableDateTimeList
{
MyList=新列表
{
新的日期时间(2000,01,01,0,0,0,0,DateTimeKind.Utc),
无效的
新的日期时间(2000、12、31、0、0、0、0、DateTimeKind.Utc),
}
};
var json=dto.ToJson();
json.Print();
Assert.That(json,Is.EqualTo(
@“{”“MyList”“:[”“\/Date(946684800000)\/”,null,“\/Date(978220800000)\/”);
var fromJson=json.fromJson();
Assert.That(fromJson.MyList.Count,Is.EqualTo(dto.MyList.Count));
}

Hmm。。。我仍然在使用3.9x版本。。。因为4.x版本有许可费。@Raymond是的,但确实有。