0

I have an interface which contains properties of type ICommand. When I serialize my concrete class instances that implement this interface, the ICommand properties are also serialized:

public interface IMyInterface : ICloneable, IEditableObject, INotifyPropertyChanged
{
    ICommand CmdMainDbBrowseButtonClicked { get; }
    ICommand CmdAnalyticsDbBrowseButtonClicked { get; }
    ICommand CmdMediaDbBrowseButtonClicked { get; }
    ICommand CmdWebTrakDbBrowseButtonClicked { get; }
    ICommand CmdTestButtonClicked { get; }
}

JSON serialized form:

[
  {
    "CmdMainDbBrowseButtonClicked": {},
    "CmdAnalyticsDbBrowseButtonClicked": {},
    "CmdMediaDbBrowseButtonClicked": {},
    "CmdWebTrakDbBrowseButtonClicked": {},
    "CmdTestButtonClicked": {},
    ...
   }
]

This presents a problem when deserializing, as the deserializer does not know the concrete type of ICommand is RelayCommand.

How can I completely disable the serialization of the ICommand properties, or coerce their types to RelayCommand at deserialization time?

Mark Richman
  • 28,948
  • 25
  • 99
  • 159
  • Not a duplicate. That other topic refers to the .NET BCL. I'm referring specifically to JSON.NET http://james.newtonking.com/json – Mark Richman Jul 21 '14 at 15:16
  • You can selectively ignore properties across classes using a custom contract resolver. See [this answer](http://stackoverflow.com/a/18548894/10263) for an example of that approach. On deserialization, you can use a custom JsonConverter to coerce an interface or abstract class to the correct concrete type using other info in the JSON to indicate which type to create. [This answer](http://stackoverflow.com/a/19308474/10263) explains how to do that. – Brian Rogers Jul 21 '14 at 15:35
  • 1
    You could add `[JsonIgnore]` to each property. – Michael Liu Jul 23 '14 at 03:33
  • `[JsonIgnore]` is what I needed, thanks. – Mark Richman Jul 23 '14 at 12:47

0 Answers0