快速提问:
在JSONNet中 - 如何将bool true / false序列化为bool 1/0
我可以看到我们如何处理空值以及所有似乎无法找到如何执行此操作的内容。
这可能吗?
答案 0 :(得分:17)
您可以实现这样的自定义转换器:
[TestFixture]
public class CustomJsonSerialization
{
[Test]
public void Test()
{
string serializeObject = JsonConvert.SerializeObject(true, new BoolConverter());
Assert.That(serializeObject, Is.EqualTo("1"));
var deserializeObject = JsonConvert.DeserializeObject<bool>(serializeObject, new BoolConverter());
Assert.That(deserializeObject, Is.True);
}
}
public class BoolConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((bool)value) ? 1 : 0);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return reader.Value.ToString() == "1";
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(bool);
}
}
答案 1 :(得分:1)
受this response in similar question的启发,可以解决如下问题。
[JsonIgnore]
public bool SomeFlag { get; set; }
[JsonProperty(nameof(SomeFlag))]
public int SomeFlagAsInt
{
get => SomeFlag ? 1 : 0;
set => SomeFlag = value > 0;
}
答案 2 :(得分:0)
如果有人需要,这是我的版本(在vb中)。它还处理可以为空的布尔
Imports Newtonsoft.Json
Public Class MyBooleanConverter
Inherits JsonConverter
Public Overrides ReadOnly Property CanWrite As Boolean
Get
Return True
End Get
End Property
Public Overrides Sub WriteJson(writer As JsonWriter, value As Object, serializer As JsonSerializer)
Dim boolVal As Boolean = value
writer.WriteValue(If(boolVal, 1, 0))
End Sub
Public Overrides Function ReadJson(reader As JsonReader, objectType As Type, existingValue As Object, serializer As JsonSerializer) As Object
Dim value = reader.Value
If IsNothing(value) OrElse String.IsNullOrWhiteSpace(value.ToString()) OrElse "0" = value Then
Return False
End If
If 0 = String.Compare("yes", value, True) OrElse 0 = String.Compare("true", value, True) Then
Return True
End If
Return False
End Function
Public Overrides Function CanConvert(objectType As Type) As Boolean
Return objectType = GetType(Boolean) OrElse objectType = GetType(Boolean?) 'OrElse objectType = GetType(String)
End Function
End Class
我是根据@John在how to get newtonsoft to deserialize yes and no to boolean
开始的答案 3 :(得分:0)
声明两个单独的属性,对序列化隐藏真正的属性,而改用“ fake” int一个。像这样用牛顿的属性装饰两者:
[JsonIgnore] public bool Bool { internal get; set; }
[JsonProperty("your_dispay_name")] public string Int { get { return this.Bool ? 1 : 0; } }
答案 4 :(得分:0)
@DavidPeden 回答 v2
/// <summary>
/// CUSTOM BOOLEAN -> 0 | 1 CONVERTER
/// </summary>
public class BoolConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(bool.TryParse(value?.ToString(), out var parsed) ? (parsed ? 1 : 0) : 0);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return int.TryParse(reader.Value?.ToString(), out var parsed) && parsed == 1;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(bool) || objectType == typeof(bool?);
}
}