C# JSON.Net-仅在序列化时使用JsonIgnoreAttribute(但在反序列化时不使用)

C# JSON.Net-仅在序列化时使用JsonIgnoreAttribute(但在反序列化时不使用),c#,json.net,C#,Json.net,我们使用的是JSON.net,希望使用一致的方式发送和接收数据(文档) 我们需要一个所有文档都将从中派生的基类。基类将有一个DocumentType属性——这本质上就是类名 当客户端将此json序列化文档发布到服务器时,我们希望对其进行反序列化,并确保客户端指定的DocumentType与服务器上的ExpectedDocumentType匹配 此外,当这个文档被服务器序列化并发送到客户机时,我们希望JSON中包含DocumentType属性——诀窍是我们希望这个值是ExpectedDocumen

我们使用的是JSON.net,希望使用一致的方式发送和接收数据(文档)

我们需要一个所有文档都将从中派生的基类。基类将有一个DocumentType属性——这本质上就是类名

当客户端将此json序列化文档发布到服务器时,我们希望对其进行反序列化,并确保客户端指定的DocumentType与服务器上的ExpectedDocumentType匹配

此外,当这个文档被服务器序列化并发送到客户机时,我们希望JSON中包含DocumentType属性——诀窍是我们希望这个值是ExpectedDocumentType的值

我试着这样做。。。如果JsonProperty和JsonIgnore属性仅在序列化过程中生效,而不是在反序列化过程中生效,那么这将起作用,但不幸的是,事实并非如此

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; }
}
公共抽象类JsonDocument
{
/// 
///具体类期望从中反序列化的文档类型。
/// 
//[JsonProperty(PropertyName=“DocumentType”)]//序列化派生类型时,我们用这个ExpectedDocumentType属性替换DocumentType属性。
公共抽象字符串ExpectedDocumentType{get;}
/// 
///从中反序列化具体类的JSON中提供的实际文档类型。
/// 
[JsonIgnore]//序列化派生类型时,我们忽略此属性,而是使用ExpectedDocumentType属性。
公共字符串DocumentType{get;set;}
}
有人知道如何做到这一点吗


本质上,逻辑是客户机可以提供任何文档类型,因此在反序列化期间,服务器需要确保这与预期的DocumentType匹配,然后在序列化期间,当服务器将此文档发送给客户机时,服务器知道正确的DocumentType,因此需要使用ExpectedDocumentType填充它。

您可以使用枚举来完成此操作,我不知道DocumentType是否是枚举,但它应该是

enum DocumentType {
    XML,
    JSON,
    PDF,
    DOC
}
当反序列化请求时,如果客户端向您发送无效的枚举,则会出现错误。“InvalidEnumArgumentException”,您可以捕获它并告诉客户端它正在发送无效的DocumentType。

使用Json.Net提供的功能。因此,基本上,您的类将如下所示:

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;
    }
}
公共抽象类JsonDocument
{
/// 
///具体类期望从中反序列化的文档类型。
/// 
//[JsonProperty(PropertyName=“DocumentType”)]//序列化派生类型时,我们用这个ExpectedDocumentType属性替换DocumentType属性。
公共抽象字符串ExpectedDocumentType{get;}
/// 
///从中反序列化具体类的JSON中提供的实际文档类型。
/// 
公共字符串DocumentType{get;set;}
//告诉json.net不要序列化DocumentType,但允许对DocumentType进行反序列化
公共bool应序列化DocumentType()
{
返回false;
}
}

您正在使用WCF吗?或者只是简单的对ASHX或ASPX的旧请求?看看不同的人如何解释不同的名称很有趣:)-这不是数据类型。对于我们来说,文档类型类似于“CreateUserDocument”、“UpdateUserDocument”、“UpdateLocationDocument”。我们使用REST,所以我们使用文档来创建/更新/删除和表示资源。好的,我每天都与REST一起创建API,很抱歉,我不理解您的问题:/。