在JSON反序列化期间将int转换为bool

时间:2021-09-04 02:57:16

I am receiving a JSON object with RestSharp. Therefor I've written a custom Deserializer, which implements ServiceStack.Text:

我正在使用RestSharp接收JSON对象。因此,我编写了一个自定义反序列化器,它实现了ServiceStack.Text:

public T Deserialize<T>(IRestResponse response)
{
  return JsonSerializer.DeserializeFromString<T>(response.Content);
}

The response is mapped to a POCO, which uses System.Runtime.Serialization to provide better mapping. That's working fine, but not for booleans. There are a lot of properties returned, which are 1 or 0 (ints).

响应映射到POCO,它使用System.Runtime.Serialization来提供更好的映射。这工作正常,但不适合布尔人。返回了很多属性,分别为1或0(整数)。

For example: { favorite: 1 }

例如:{favorite:1}

The problem here is, I don't know how to convert this to a Boolean in my POCO.
This won't work (for sure):

这里的问题是,我不知道如何将它转换为我的POCO中的布尔值。这不起作用(肯定):

[DataContract]
public class Item
{
  [DataMember(Name = "favorite")]
  public bool IsFavorite { get; set; }
}

Any suggestions on how to get it working?

有关如何使其工作的任何建议?

I do not only want to know this for int <=> bool, but for all type conversions in general.

我不仅想知道int <=> bool,而且知道所有类型的转换。

1 个解决方案

#1


4  

Edit:

I've just committed support built-in support for this, so in the next version of ServiceStack.Text (v3.9.55+) you can deserialize 0, 1, true, false into booleans, e.g:

我刚刚为此提供了支持内置支持,因此在下一版本的ServiceStack.Text(v3.9.55 +)中,您可以将0,1,true,false反序列化为布尔值,例如:

var dto1 = "{\"favorite\":1}".FromJson<Item>();
Assert.That(dto1.IsFavorite, Is.True);

var dto0 = "{\"favorite\":0}".FromJson<Item>();
Assert.That(dto0.IsFavorite, Is.False);

var dtoTrue = "{\"favorite\":true}".FromJson<Item>();
Assert.That(dtoTrue.IsFavorite, Is.True);

var dtoFalse = "{\"favorite\":false}".FromJson<Item>();
Assert.That(dtoFalse.IsFavorite, Is.False);

You can do what you want with:

你可以做你想做的事:

JsConfig<bool>.DeSerializeFn = x => x.Length == 1 ? x == "1" : bool.Parse(x);

All of ServiceStack.Text's customizations are available on JsConfig.

JsConfig上提供了所有ServiceStack.Text的自定义。

Other hooks available includes JsConfig<T>.RawSerializeFn and JsConfig<T>.RawDeserializeFn which lets you override the custom serialization of a custom POCO.

其他可用的钩子包括JsConfig .RawSerializeFn和JsConfig .RawDeserializeFn,它允许您覆盖自定义POCO的自定义序列化。

If you just want to some pre/post processing there's also the JsConfig<T>.OnSerializingFn and JsConfig<T>.OnDeserializedFn custom hooks.

如果你只是想要一些前/后处理,那么还有JsConfig .OnSerializingFn和JsConfig .OnDeserializedFn自定义钩子。

Here's an earlier example of using a customizing deserialization using a custom MyBool struct.

这是使用自定义MyBool结构使用自定义反序列化的早期示例。

See the ServiceStack.Text unit tests for examples of the above custom hooks.

有关上述自定义挂钩的示例,请参阅ServiceStack.Text单元测试。

#1


4  

Edit:

I've just committed support built-in support for this, so in the next version of ServiceStack.Text (v3.9.55+) you can deserialize 0, 1, true, false into booleans, e.g:

我刚刚为此提供了支持内置支持,因此在下一版本的ServiceStack.Text(v3.9.55 +)中,您可以将0,1,true,false反序列化为布尔值,例如:

var dto1 = "{\"favorite\":1}".FromJson<Item>();
Assert.That(dto1.IsFavorite, Is.True);

var dto0 = "{\"favorite\":0}".FromJson<Item>();
Assert.That(dto0.IsFavorite, Is.False);

var dtoTrue = "{\"favorite\":true}".FromJson<Item>();
Assert.That(dtoTrue.IsFavorite, Is.True);

var dtoFalse = "{\"favorite\":false}".FromJson<Item>();
Assert.That(dtoFalse.IsFavorite, Is.False);

You can do what you want with:

你可以做你想做的事:

JsConfig<bool>.DeSerializeFn = x => x.Length == 1 ? x == "1" : bool.Parse(x);

All of ServiceStack.Text's customizations are available on JsConfig.

JsConfig上提供了所有ServiceStack.Text的自定义。

Other hooks available includes JsConfig<T>.RawSerializeFn and JsConfig<T>.RawDeserializeFn which lets you override the custom serialization of a custom POCO.

其他可用的钩子包括JsConfig .RawSerializeFn和JsConfig .RawDeserializeFn,它允许您覆盖自定义POCO的自定义序列化。

If you just want to some pre/post processing there's also the JsConfig<T>.OnSerializingFn and JsConfig<T>.OnDeserializedFn custom hooks.

如果你只是想要一些前/后处理,那么还有JsConfig .OnSerializingFn和JsConfig .OnDeserializedFn自定义钩子。

Here's an earlier example of using a customizing deserialization using a custom MyBool struct.

这是使用自定义MyBool结构使用自定义反序列化的早期示例。

See the ServiceStack.Text unit tests for examples of the above custom hooks.

有关上述自定义挂钩的示例,请参阅ServiceStack.Text单元测试。