如何使JsonConverter写入键/值而不是键/数组
我有包含标识符的类,如下所示如何使JsonConverter写入键/值而不是键/数组,json,json.net,Json,Json.net,我有包含标识符的类,如下所示 public class Type1 { public int Id { get; set; } // other properties } public class Type2 { public int Id { get; set; } // other properties } public class ParentType { [JsonConverter(typeof(IdConverter))] publ
public class Type1
{
public int Id { get; set; }
// other properties
}
public class Type2
{
public int Id { get; set; }
// other properties
}
public class ParentType
{
[JsonConverter(typeof(IdConverter))]
public Type1 Instance1 { get; set; }
[JsonConverter(typeof(IdConverter))]
public Type2 Instance2 { get; set; }
}
class IdConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
PropertyInfo pi = objectType.GetProperty("Id");
return (pi != null && pi.PropertyType == typeof(int));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
int id = (int)value.GetType().GetProperty("Id").GetValue(value);
JObject o = JObject.FromObject(new { Id = id });
o.WriteTo(writer);
}
}
}
{
"Instance1": 1,
"Instance2": 2
}
{
"Instance1": 1,
"Instance2": 2
}
我想将父类型
序列化为Json,但不需要序列化整个类。我只需要Id,因为它唯一标识实例。IdConverter是这样实现的
public class Type1
{
public int Id { get; set; }
// other properties
}
public class Type2
{
public int Id { get; set; }
// other properties
}
public class ParentType
{
[JsonConverter(typeof(IdConverter))]
public Type1 Instance1 { get; set; }
[JsonConverter(typeof(IdConverter))]
public Type2 Instance2 { get; set; }
}
class IdConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
PropertyInfo pi = objectType.GetProperty("Id");
return (pi != null && pi.PropertyType == typeof(int));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
int id = (int)value.GetType().GetProperty("Id").GetValue(value);
JObject o = JObject.FromObject(new { Id = id });
o.WriteTo(writer);
}
}
}
{
"Instance1": 1,
"Instance2": 2
}
{
"Instance1": 1,
"Instance2": 2
}
这很好,但输出Json的格式并不理想
{
"Instance1": {
"Id": 1
},
"Instance2": {
"Id": 2
}
}
我希望它看起来更像这样
public class Type1
{
public int Id { get; set; }
// other properties
}
public class Type2
{
public int Id { get; set; }
// other properties
}
public class ParentType
{
[JsonConverter(typeof(IdConverter))]
public Type1 Instance1 { get; set; }
[JsonConverter(typeof(IdConverter))]
public Type2 Instance2 { get; set; }
}
class IdConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
PropertyInfo pi = objectType.GetProperty("Id");
return (pi != null && pi.PropertyType == typeof(int));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
int id = (int)value.GetType().GetProperty("Id").GetValue(value);
JObject o = JObject.FromObject(new { Id = id });
o.WriteTo(writer);
}
}
}
{
"Instance1": 1,
"Instance2": 2
}
{
"Instance1": 1,
"Instance2": 2
}
有什么办法可以做到这一点吗?对不起,我自己解决的
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
int id = (int)value.GetType().GetProperty("Id").GetValue(value);
writer.WriteValue(id);
}
}
结果