我可以将ServiceStack Deserialize json值1设置为true吗? [英] Can I make ServiceStack Deserialize json value of 1 as true?
本文介绍了我可以将ServiceStack Deserialize json值1设置为true吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以将ServiceStack Deserialize json值1设置为true吗?
这是显示我要做什么的单元测试.这可能吗?如果可以,怎么办?
Can I make ServiceStack Deserialize json value of 1 as true?
Here's a unit test showing what I want to do.
Is this possible? if so how?
public class Foo
{
public bool isWorking { get; set; }
}
...
[Test]
public void Deserialise1AsBoolean()
{
var json = @"{""isWorking"": 1}";
var myFoo = json.FromJson<Foo>();
Assert.IsTrue(myFoo.isWorking);
}
推荐答案
编辑这是我的解决方案,但也请查看Mythz,因为我确信它也可以使用.
EDIT Here's my solution, but please check out Mythz as well since I'm sure that will work also.
我反序列化为自定义结构 MyBool
而不是 bool
.
这是MyBool结构的代码.
I deserialise to a custom struct MyBool
rather than bool
.
Here's the code for the MyBool struct.
public struct MyBool
{
public bool Value { get; set; }
public static MyBool Parse(string value)
{
return new MyBool {Value = (value == "1" || value=="true")};
}
public override string ToString()
{
return Value.ToString(CultureInfo.InvariantCulture);
}
public static implicit operator bool(MyBool lValue)
{
return lValue.Value;
}
并将Foo更改为:
public class Foo
{
public MyBool isWorking { get; set; }
}
欢迎批评.
这篇关于我可以将ServiceStack Deserialize json值1设置为true吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文