2012-03-16 25 views
9

câu hỏi nhanh:JSonNet serialization boolean

Trong JSONNet - làm thế nào để tôi nhận được bool true/false để serialize như bool 1/0

tôi có thể xem cách chúng tôi xử lý các giá trị null và tất cả những gì chỉ không thể dường như tìm cách làm điều này.

là điều này có thể?

Trả lời

15

Bạn có thể thực hiện một chuyển đổi tùy chỉnh như thế này:

[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); 
    } 
} 
+0

Hi Nó chỉ xử lý nếu giá trị của tôi là "1" nếu tôi vượt qua 2 thay vì 1, Nó cũng sẽ được chuyển thành false –

+0

Chỉ cần mã bất kỳ logic nào bạn muốn. Mã mẫu nên làm cho nó khá rõ ràng làm thế nào để làm những gì bạn đang yêu cầu. –

+0

Tôi đã trả về lỗi trạng thái mô hình trong trường hợp người dùng vượt qua hai. Làm thế nào để xử lý rằng –

0

Dưới đây là phiên bản của tôi (trong vb) nếu có ai cần nó. Nó cũng xử lý 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 

tôi bắt đầu này dựa trên @ John ở đây: how to get newtonsoft to deserialize yes and no to boolean

0

Lấy cảm hứng từ this response in similar question nó có thể được giải quyết như sau.

[JsonIgnore] 
public bool SomeFlag { get; set; } 

[JsonProperty(nameof(SomeFlag))] 
public int SomeFlagAsInt 
{ 
    get => IsOk ? 1 : 0; 
    set => IsOk = value > 0; 
}