JsonConvert.SerializeObject忽略子类型



我正在使用JsonConvert序列化一个对象,如下所示:

JsonConvert.SerializeObject(item)

Item是QuestionExtendedView的一个实例,如下所示:

public class QuestionExtendedView : AuditFullView
{
    public short QuestionNo { get; set; }
    public short TotalQuestions { get; set; }
    public short UnansQuestions { get; set; }
}

AuditFullView如下所示:

public partial class AuditFullView : EntityObject
{
    public static AuditFullView CreateAuditFullView(global::System.Int32 hAA_ID, global::System.Int16 hAA_Branch, global::System.Int32 hAA_AuditorID, global::System.DateTime hAA_ScheduledDate, global::System.Int32 hAA_TemplateVersionID, global::System.String hAA_Status, global::System.Int32 hAS_ID, global::System.Int32 hAS_AuditID, global::System.Int32 hAS_TemplateSectionID, global::System.Int32 hAE_ID, global::System.Int32 hAE_AuditID, global::System.Int32 hAE_HAS_ID, global::System.Int32 hAE_TemplateElementID, global::System.Int16 hAE_ScriptSequence, global::System.Int32 hAQ_ID, global::System.Int32 hAQ_AuditID, global::System.Int32 hAQ_HAE_ID, global::System.Int32 hAQ_TemplateQuestionID, global::System.Int16 hAQ_ScriptSequence, global::System.Int32 hTS_ID, global::System.Int32 hTS_VersionID, global::System.Int32 hTS_Sequence, global::System.String hTS_SectionName, global::System.Int32 hTE_ID, global::System.Int32 hTE_SectionID, global::System.Int32 hTE_Sequence, global::System.String hTE_Element, global::System.String hTE_Objective, global::System.String hTE_Guidance, global::System.Int32 hTQ_ID, global::System.Int32 hTQ_ElementID, global::System.Int32 hTQ_Sequence, global::System.String hTQ_Question, global::System.Boolean hTQ_WeightedQuestion, global::System.String hSU_Name, global::System.Boolean hAQ_PreviouslyAnsweredQuestion)
    {
        AuditFullView auditFullView = new AuditFullView();
        auditFullView.HAA_ID = hAA_ID;
        auditFullView.HAA_Branch = hAA_Branch;
    // Loads of properties excluded for clarity
        return auditFullView;
    }
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int32 HAA_ID
    {
        get
        {
            return _HAA_ID;
        }
        set
        {
            if (_HAA_ID != value)
            {
                OnHAA_IDChanging(value);
                ReportPropertyChanging("HAA_ID");
                _HAA_ID = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("HAA_ID");
                OnHAA_IDChanged();
            }
        }
    }
    private global::System.Int32 _HAA_ID;
    partial void OnHAA_IDChanging(global::System.Int32 value);
    partial void OnHAA_IDChanged();
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Int16 HAA_Branch
    {
        get
        {
            return _HAA_Branch;
        }
        set
        {
            if (_HAA_Branch != value)
            {
                OnHAA_BranchChanging(value);
                ReportPropertyChanging("HAA_Branch");
                _HAA_Branch = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("HAA_Branch");
                OnHAA_BranchChanged();
            }
        }
    }
    private global::System.Int16 _HAA_Branch;
    partial void OnHAA_BranchChanging(global::System.Int16 value);
    partial void OnHAA_BranchChanged();
// Loads of properties excluded for clarity
}

现在,当我运行这个序列化时,我从AuditFullView中获得所有属性,但不是从QuestionExtendedView中获得的属性,如下面的测试所示:

[TestMethod]
    public void CanSerialize()
    {
        QuestionExtendedView myView = new QuestionExtendedView
                                      {
                                          TotalQuestions = 15,
                                          QuestionNo = 10,
                                          UnansQuestions = 5,
                                          HAA_ID = 100,
                                          HAA_Branch = 213
                                        };
        string json = JsonConvert.SerializeObject(myView);
        json.Should().Contain("TotalQuestions");
    }

失败的结果是:

{"$id":"1","HAA_ID":100,"HAA_Branch":213,"EntityKey":null}

(同样,为了清晰起见,我从AuditFullView中排除了许多属性)

我甚至试过这个:

JsonConvert.SerializeObject(Convert.ChangeType(myView, typeof(QuestionExtendedView))

不过没什么区别。我似乎找不到其他人有这个问题。我是不是错过了什么?

您的AuditFullView类继承自用DataContract属性标记的EntityObject。因此,JSON.NET要求所有属性都必须选择使用DataMember属性进行序列化。任何没有该属性的属性都将被序列化程序忽略。

AuditFullView上的属性用DataMember标记,因此在序列化时包含这些属性;QuestionExtendedView上的属性没有用DataMember标记,因此它们被忽略。

因此,简单的解决方案是用DataMember属性标记QuestionExtendedView类的属性。

相关内容

  • 没有找到相关文章

最新更新