I am using C# .NET 4.0 and Newtonsoft JSON 4.5.0.11
[JsonObject(MemberSerialization.OptIn)]
public interface IProduct
{
[JsonProperty(PropertyName = "ProductId")]
int Id { get; set; }
[JsonProperty]
string Name { get; set; }
}
public abstract class BaseEntity<T>
{
private object _id;
public T Id
{
get { return (T)_id; }
set { _id = value; }
}
}
public class Product : BaseEntity<int>, IProduct
{
public string Name { get; set; }
public int Quantity { get; set; }
}
I need to serialize part of object and I use interfaces with declared concrete properties to do this.
The serialization looks like:
Product product = new Product { Id = 1, Name = "My Product", Quantity = 5};
JsonConvert.SerializeObject(product);
Expected result is:
{"ProductId": 1, "Name": "My Product"}
But actual result is:
{"Name": "My Product"}
How can I serialize this object correctly?
UPD: Looked at the source code of json.net and came to the conclusion that this is a bug with grab information about object through ReflectionUtils.