Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

c# - 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>
    //[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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...