[英]Serialize complex property of object as scalar value
假設我們有以下類定義
public class A
{
public string Name { get; } = "Johny Bravo";
public B ComplexProperty { get; } = new B();
}
public class B
{
public string Prop1 { get; } = "value1";
public string Prop2 { get; } = "value2";
public int Prop3 { get; } = 100;
public override string ToString()
{
return this.Prop3.ToString();
}
}
序列化時
var a = new A();
var str = JsonConvert.SerializeObject(a);
這將導致以下json字符串
{
"Name":"Johny Bravo",
"ComplexProperty":{
"Prop1":"value1",
"Prop2":"value2",
"Prop3":100
}
}
我們如何使ComplexProperty序列化為標量值? 期望的結果必須是這樣的:
{
"Name":"Johny Bravo",
"ComplexProperty":100
}
您的代碼將無法編譯,因為您正在將B類分配給int值,但是我想我得到了您想要的:
class Program
{
static void Main(string[] args)
{
var a = new A();
var str = JsonConvert.SerializeObject(a);
Console.Write(str);
}
}
public class A
{
public string Name { get; } = "Johny Bravo";
[JsonIgnore]
public B ComplexProperty { get; } = new B();
[JsonProperty("ComplexProperty")]
public int complexValue => ComplexProperty.Prop3;
}
public class B
{
public string Prop1 { get; } = "value1";
public string Prop2 { get; } = "value2";
public int Prop3 { get; } = 100;
public override string ToString()
{
return this.Prop3.ToString();
}
}
由於您希望對象是平坦的(只有1級深度),因此需要在A類上具有該屬性。 由於您不想在json結果中包含您的復雜對象,因此您必須忽略它,並且由於您需要在Json結果中使用相同的名稱,因此您必須告訴json序列化器使用所需的序列化數據名稱
解決此問題的正確方法是使用可處理您的類型的自定義JsonConverter
。 尤其是在您無法控制A和B類的代碼的情況下。 這是示例代碼
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var a = new A();
var str = JsonConvert.SerializeObject(a, new JsonSerializerSettings()
{
Converters = new List<JsonConverter>()
{
new BTypeJsonConverter()
}
});
}
}
public class A
{
public string Name { get; } = "Johny Bravo";
public B ComplexProperty { get; } = new B();
}
public class B
{
public string Prop1 { get; } = "value1";
public string Prop2 { get; } = "value2";
public int Prop3 { get; } = 100;
public override string ToString()
{
return this.Prop3.ToString();
}
}
public class BTypeJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var b = value as B;
if (b == null) return;
writer.WriteValue(b.ToString());
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(B);
}
}
}
添加另一個屬性,並將序列化PropertyName
設置為第一個。 如果您的A
類來自第三方庫,請創建一個從A
派生的類並添加新屬性。
public class A
{
public string Name { get; } = "Johny Bravo";
--> [JsonIgnore]
public B ComplexProperty { get; } = new B();
-->[JsonProperty(PropertyName = "ComplexProperty")]
--> public string ComplexPropertyText { get{ return ComplexProperty.ToString(); } }
}
public class B
{
public string Prop1 { get; } = "value1";
public string Prop2 { get; } = "value2";
public int Prop3 { get; } = 100;
public override string ToString()
{
return this.Prop3.ToString();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.