Can I make the value of ServiceStack Deserialize json equal to 1 as true?

Is it possible to make ServiceStack Deserialize json value 1 as true?
Here unit test shows what I want to do. Is it 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); } 
+1
source share
2 answers

EDIT Here is my solution, but please check out Mythz, as I am sure it will work.

I deserialise for the custom structure of MyBool , not bool .
Here is the code for the MyBool structure.

 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; } 

and change Foo to:

 public class Foo { public MyBool isWorking { get; set; } } 

Criticism is welcome.

0
source

Now it is built in to ServiceStack.Text with this commit , available from v3.9.55 +.

+1
source

Source: https://habr.com/ru/post/1488146/


All Articles