2

I'm trying to write my own attribute. Every Property that has this attribute should be ignored when i serialize or deserialize it. This is my Poco

    public class Fren
{
    [JsonIgnoreInDatabase]
    public string Name { get; set; }
    
    public int Age { get; set; }
}

The output should be like this

{"Age":0}

What i tried

JsonIgnoreInDatabase : JsonIgnore

I was hoping that i could take the inherit from JsonIgnore but its sealed What i read was also to write your own serializer that ignores custom attributes Is there any better way?

Edit: Changed MyAttribute to JsonIgnoreInDatabase

Tanyel
  • 61
  • 7
  • 3
    (edit: removed some bits that no longer apply after the edit) Can you clarify what you're trying to do, and how this is different to just putting `[JsonIgnore]` on the things that you want to ignore? "What i read was also to write your own serializer that ignores custom attributes" - where did you read that, and what was the context? It may matter. Is the context anything like yours? – Marc Gravell Jun 24 '20 at 09:12
  • Oops i edited it. And i dont want to use jsonignore because sometimes I create json files and there for i use jsonignore, but i want it to differentiate – Tanyel Jun 24 '20 at 09:16
  • 2
    Between what two scenarios do you want to differentiate? it *really* isn't obvious to me - sorry if I'm seeming dense, but the details often matter, and I want to make sure we're talking about the same thing. So: you've spoken about the scenario where `JsonIgnore` is fine - when *isn't* it fine? can you clarify that? – Marc Gravell Jun 24 '20 at 09:18
  • I read it here https://stackoverflow.com/questions/13588022/exclude-property-from-serialization-via-custom-attribute-json-net – Tanyel Jun 24 '20 at 09:18
  • So sometimes I want to create a json file and store it on the pc like a normal file. Then it should ignore not the Name but if iwant to store the json file in a database i want to ignore the name. – Tanyel Jun 24 '20 at 09:20
  • 1
    just an opinion (but one informed by a *lot* of serialization experience): it sounds to me like you're trying to have one type serve two different purposes; I have a maxim that has served me very well: if you are using a serialization type in two different ways and it works, great; the moment it stops working: don't fight the serializer - instead, create two types - one that serves each job perfectly - and just map between them – Marc Gravell Jun 24 '20 at 09:40

1 Answers1

2

Attributes don't do anything of themselves, they just get compiled into your types as metadata. Attributes generally don't "run" or "do something".

JSON.NET looks for its own JsonIgnore attribute (and others) on type members using reflection.

So while you're asking for "best practices" and "better way" about inheriting JsonIgnoreAttribute, but there simply is no way to do what you want, other than modify JSON.NET, issue a pull request and hope it gets accepted (it likely won't).

There are other ways to affect serialization though. You may research that. Why would you want to do this anyway, what's wrong with [JsonIgnore]?

The question you link to, does sketch just that: add a contract resolver (a built-in extension point for affecting serialization in JSON.NET) which inspects your types for your custom attributes. That should work. And then for different serialization scenarios, you pass different contract resolvers. If you need help getting that to work, then ask about that, and not about inheriting sealed attributes.

And as @MarcGravell indicates, if you have different output types, use different DTO classes and something like AutoMapper to populate your DTOs. And perhaps try and use inheritance where relevant. Letting one type serialize differently based on some condition will bite you in the back sooner or later. Have you tested deserialization, for example?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272