2015-11-09 59 views
1

我使用JsonConvert序列化,像這樣的對象:JsonConvert.SerializeObject忽略亞型

JsonConvert.SerializeObject(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的所有屬性,b UT斯達康不從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)) 

沒有區別,但。我似乎無法找到其他人遇到此問題。我錯過了什麼嗎?

+1

你能提供一個[最小的,完整的例子](http://stackoverflow.com/help/mcve)來演示這個問題嗎? – LukeH

+0

當然,雖然我認爲在這種情況下它會分散問題本身。給我15分鐘把它放在一起 –

回答

1

您的AuditFullView類繼承自EntityObject,它標有DataContract屬性。因此,JSON.NET要求所有屬性必須使用DataMember屬性選擇加入序列化。沒有屬性的任何屬性將被序列化程序忽略。

AuditFullView上的屬性標有DataMember,所以它們包含在序列化時; QuestionExtendedView上的屬性沒有標記爲DataMember,所以它們被忽略。

所以直接的解決方案是用DataMember屬性標記QuestionExtendedView類的屬性。

+0

獲勝者!完美,非常感謝 –