Quick question:
In JSONNet - how do i get bool true/false to serialize as bool 1/0
I can see how we handle null values and all that just cant seem to find how to do this.
is this possible?
Quick question:
In JSONNet - how do i get bool true/false to serialize as bool 1/0
I can see how we handle null values and all that just cant seem to find how to do this.
is this possible?
You can implement a custom converter like this:
[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);
}
}
Inspired by this response in similar question it can be solved like the following.
[JsonIgnore]
public bool SomeFlag { get; set; }
[JsonProperty(nameof(SomeFlag))]
public int SomeFlagAsInt
{
get => SomeFlag ? 1 : 0;
set => SomeFlag = value > 0;
}
@DavidPeden answer 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?);
}
}
Here's my version (in vb) if anyone needs it. It also handles nullable Boolean
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
I started this based on @John here: how to get newtonsoft to deserialize yes and no to boolean
Declare two separate properties, hide the real one from serialization, and use the "fake" int one instead. Decorate both with Newton's attributes like this:
[JsonIgnore] public bool Bool { internal get; set; }
[JsonProperty("your_dispay_name")] public string Int { get { return this.Bool ? 1 : 0; } }