I'm trying to serialize only the inherited properties of a class using json.net. I'm aware of the [JsonIgnore] attribute, but I only want to do ignore them on certain occasion, so I used a custom JsonConverter instead.
Here's my class:
public class EverythingButBaseJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// Find properties of inherited class
var classType = value.GetType();
var classProps = classType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).ToList();
// Remove the overrided properties
classProps.RemoveAll(t =>
{
var getMethod = t.GetGetMethod(false);
return (getMethod.GetBaseDefinition() != getMethod);
});
// Get json data
var o = (JObject)JToken.FromObject(value);
// Write only properties from inhertied class
foreach (var p in o.Properties().Where(p => classProps.Select(t => t.Name).Contains(p.Name)))
p.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("");
}
public override bool CanRead
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return true;
}
}
When doing a simple o.WriteTo(writer); it give the same result as not using a converter. When iterating through properties and using WriteTo on the properties, it works fine for base type (int, string, etc), but I'm having problem with collections.
Expected:
{
"Type": 128,
"Time": [
1,
2,
],
"Pattern": 1,
"Description": ""
}
Got:
"Type": 128,
"Time": [
1,
2,
]"Pattern": 1,
"Description": ""
As you can see, the collection is missing the "," and endline portion. I'm also missing the global { } for the whole object.
I am doing things the correct way? Is there an easier way to get the result I want?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…