我可以将ServiceStack Deserialize json值1设置为true吗?

编程入门 行业动态 更新时间:2024-10-10 17:32:20
本文介绍了我可以将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吗?

本文发布于:2023-11-12 10:12:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   Deserialize   ServiceStack   true   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!