With a simple class/interface like this
public interface IThing {     string Name { get; set; } }  public class Thing : IThing {     public int Id { get; set; }     public string Name { get; set; } }   How can I get the JSON string with only the "Name" property (only the properties of the underlying interface) ?
Actually, when i make that :
var serialized = JsonConvert.SerializeObject((IThing)theObjToSerialize, Formatting.Indented); Console.WriteLine(serialized);   I get the full object as JSON (Id + Name);
The method I use,
public class InterfaceContractResolver : DefaultContractResolver {     private readonly Type _InterfaceType;     public InterfaceContractResolver (Type InterfaceType)     {         _InterfaceType = InterfaceType;     }      protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)     {         //IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);         IList<JsonProperty> properties = base.CreateProperties(_InterfaceType, memberSerialization);         return properties;     } }  // To serialize do this: var settings = new JsonSerializerSettings() {      ContractResolver = new InterfaceContractResolver (typeof(IThing)) }); string json = JsonConvert.SerializeObject(theObjToSerialize, settings); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With