开发者

JSON.Net - Use JsonIgnoreAttribute only on serialization (But not when deserialzing)

We're using JSON.net and want to use a consistent way to send and receive data (documents).

We want a base class that all documents will be derived from. The base class would have a DocumentType property - This is essentially the class name.

When clients post this json-serialized document to the server, we want to deserialize it and ensure the DocumentType specified by the client matches the ExpectedDocumentType on the server.

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

I've attempted to do this like so... This would work if the JsonProperty and JsonIgnore attributes only took affect during serialization but not deserialization however unfortunately that is not the case.

public abstract class JsonDocument
{
    /// <summary>
    /// The document type that the concrete class expects to be deserialized from.
    /// </summary>
    //[JsonPrope开发者_StackOverflow社区rty(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?

Essentially the logic is that a client may provide any DocumentType so during deserialization the server needs to ensure this matches the ExpectedDocumentType, and then during serialization when the server sends this document to the client, the server knows the correct DocumentType so needs to populate it with the ExpectedDocumentType.


Use the ShouldSerialize feature provided by Json.Net. So basically, your class will look like:

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;
    }
}


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

enum DocumentType {
    XML,
    JSON,
    PDF,
    DOC
}

When deserializing the request it will give you an error if the client sends you an invalid enum. The "InvalidEnumArgumentException" which you can catch and tell the client that it's sending an non valid DocumentType.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜