0

I have a simple entity like this in my ASP.NET MVC project:

public class Folder : EntityData
{
    public Folder()
    {
        IsStub = false;
    }

    [StringLength(300)]
    public string Name { get; set; }

    [JsonIgnore]
    public bool IsStub { get; set; }

    [ForeignKey("ParentFolderObj")]
    public string ParentFolder { get; set; }

    [JsonIgnore]
    public Folder ParentFolderObj { get; set; }

    [JsonIgnore]
    public virtual ICollection<DesktopFolder> DesktopFolders { get; set; }
    [JsonIgnore]
    public virtual ICollection<UserFolder> UserFolders { get; set; }
}

EntityData is the class provided by MobileAppServices SDK of Azure:

public abstract class EntityData : ITableData
{
    protected EntityData();

    [Key]
    [TableColumn(TableColumnType.Id)]
    public string Id { get; set; }
    [TableColumn(TableColumnType.Version)]
    [Timestamp]
    public byte[] Version { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Index(IsClustered = true)]
    [TableColumn(TableColumnType.CreatedAt)]
    public DateTimeOffset? CreatedAt { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    [TableColumn(TableColumnType.UpdatedAt)]
    public DateTimeOffset? UpdatedAt { get; set; }
    [TableColumn(TableColumnType.Deleted)]
    public bool Deleted { get; set; }
}

In my serialized response e.g. when I retrieve all my folders, I still see the field "IsStub" even if it is marked as [JsonIgnore].

I am using Newtonsoft.Json to (de-)serialize, with the following configurations that apply to all the project and are setup at the boot up:

HttpConfiguration config = new HttpConfiguration();
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);

As you can see there are other [JsonIgnore] fields, that are correctly not present in the response, which looks like this:

{
  "deleted": false,
  "updatedAt": null,
  "createdAt": "2018-06-03T06:13:31.66Z",
  "version": "AAAAAAAACJc=",
  "id": "042d81b8-4599-43ec-b462-8cbaf6ecd672",
  "parentFolder": null,
  "isStub": false,
  "name": "Folder636636104113385950"
}

How can I make the field "IsStub" disappear from my response?

I also tried to mark the field with [ScriptIgnore], with no luck.

Cristiano Ghersi
  • 1,944
  • 1
  • 20
  • 46

1 Answers1

0

Implement the following into your "EntityData" class:

public virtual bool ShouldSerializeIsStub()
{
  return true;
}

Overwrite the function in your "Folder" Class like this:

public override bool ShouldSerializeIsStub()
{
  return false;
}

The [JsonIgnore] "tag" (is it a tag?) is correct. The rest is okay. This works fine for me. I hope, it will do the same for you.

Nico
  • 1