0

So it happens that you can prevent breeze json serialization of some properties using data annotations on your model by like this(well if you are using EF6 with JSON.NET on the backend)...

[Table("Project")]
public partial class Project
{
    public Project()
    {

    }

    public int id { get; set; }

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

By doing so the property becomes invisible on this endpoint used by breeze

public IQueryable<Project> Projects()
    {
        return _db.Context.Projects.Where(o => o.NoLongerExist == true);
    }

Can i apply [JsonIgnore] based on a certain condition like an authenticated user or a random if from this endpoint?

konzo
  • 1,973
  • 22
  • 32
  • Perhaps adding the necessary logic to the current endpoint to return a copy of the Project class without the JsonIgnore decorator? you can also try this... [link](http://www.newtonsoft.com/json/help/html/ConditionalProperties.htm) – Tavitos Jul 08 '16 at 15:35
  • Code in my `ShouldSerializeContractResolver` seems to be ignored (breakpoints in it are not hit and properties dont get serialized).. how should i hook it up? – konzo Jul 08 '16 at 16:07
  • I have tried `jsonFormatter.SerializerSettings.ContractResolver = new jsonFormatter.SerializerSettings.ContractResolver = new ShouldSerializeContractResolver();` in webapi HttpConfiguration but that didn't trigger it. @Tavitos – konzo Jul 08 '16 at 16:15
  • 1
    another similar approach you can try ... [link](http://www.geekytidbits.com/conditional-serialization-with-json-net/) and this one without the contract resolver. – Tavitos Jul 08 '16 at 16:18
  • Well this works... but it does not seem seem effective to members of the base class, i mean `public bool ShouldSerializeBaseProperty()` have no impact.. any way to make it work? I have also posted this comment on the site you linked me, – konzo Jul 08 '16 at 16:40
  • There is a solution [here](http://stackoverflow.com/q/9360633/41557410), too bad i dont have access to the base class, so it won't work for me – konzo Jul 08 '16 at 17:09
  • looks like you need to alter your base class member with the ShouldSerialize and set it as virtual, then on the inherited type member override it and use ShouldSerialize as well. – Tavitos Jul 08 '16 at 17:09
  • I have no access to the base class.. i have hit a dead end, i have to study how to make contract resolver work with breeze. – konzo Jul 08 '16 at 17:15
  • 1
    If you're looking for how to set up `JsonSerializerSettings` for breeze, try [here](https://stackoverflow.com/questions/20133320/saving-a-toisodate-gets-deserialized-as-a-non-iso-string) or [here](https://stackoverflow.com/questions/33150080/error-getting-value-from-wellknownvalue-on-system-data-entity-spatial-dbgeogr). – dbc Jul 08 '16 at 19:06
  • Thanks @Tavitos i really needed that!. I surprisingly found a way to make all methods work for me the `ContractResolver`(thanks @dbc), `[JsonIgnore]` attribute and `bool ShouldSerializePropertyName()` all of them. i will pick one that works well with my authentication system. – konzo Jul 09 '16 at 03:27
  • @konzo Can you update this question with an answer based on what you decided to do? – trenthaynes Jul 22 '16 at 18:25
  • @whipdancer I have answered many of my own questions here and it starting to feel like my problems never actually face anyone else... I will however post my entire solution here if at least one person marks this question useful – konzo Jul 23 '16 at 02:15

0 Answers0