JSON.Net - Use JsonIgnoreAttribute only for serialization (but not for deserialization)

We use JSON.net and want to use a consistent way of sending and receiving data (documents).

We want the base class to receive all documents. The base class will have the DocumentType property - this is, in fact, the name of the class.

When clients send this serialized json document to the server, we want to deserialize it and make sure that the specified DocumentType specified by the client matches the ExpectedDocumentType on the server.

Then, like this, when this document is serialized by the server and sent to the client, we want the DocumentType property to be included in the JSON. The trick is that we want this value to be the same as in ExpectedDocumentType.

I tried to do it like this ... This will work if the JsonProperty and JsonIgnore attributes only affect serialization, but not deserialization, but unfortunately this is not the case.

public abstract class JsonDocument { /// <summary> /// The document type that the concrete class expects to be deserialized from. /// </summary> //[JsonProperty(PropertyName = "DocumentType")] // We substitute the DocumentType property with this ExpectedDocumentType property when serializing derived types. public abstract string ExpectedDocumentType { get; } /// <summary> /// The actual document type that was provided in the JSON that the concrete class was deserialized from. /// </summary> [JsonIgnore] // We ignore this property when serializing derived types and instead use the ExpectedDocumentType property. public string DocumentType { get; set; } } 

Does anyone know how to achieve this?

In essence, the logic is that the client can provide any type of DocumentType, so when deserializing the server, you need to make sure that it matches the ExpectedDocumentType, and then during serialization, when the server sends this document to the client, the server knows the correct DocumentType, so it needs to be filled him using ExpectedDocumentType.

+1
source share
2 answers

Use the ShouldSerialize function provided by Json.Net. So your class will look like this:

 public abstract class JsonDocument { /// <summary> /// The document type that the concrete class expects to be deserialized from. /// </summary> //[JsonProperty(PropertyName = "DocumentType")] // We substitute the DocumentType property with this ExpectedDocumentType property when serializing derived types. public abstract string ExpectedDocumentType { get; } /// <summary> /// The actual document type that was provided in the JSON that the concrete class was deserialized from. /// </summary> public string DocumentType { get; set; } //Tells json.net to not serialize DocumentType, but allows DocumentType to be deserialized public bool ShouldSerializeDocumentType() { return false; } } 
+3
source share

You can do this with Enum, I don't know if DocumentType is an enumeration, but it should.

 enum DocumentType { XML, JSON, PDF, DOC } 

When deserializing the request, it will give you an error message if the client sends you an invalid enumeration. An "InvalidEnumArgumentException" that you can catch and tell the client that it is sending an invalid DocumentType.

0
source share

All Articles