简体   繁体   中英

Json issue in a sub-class

I have 2 DTO files, with one of them being a sub-class of the other. The fields on the super-class are as follows:

[JsonProperty]
protected int financialFormatHeaderID;

[JsonProperty]
protected string financialFormatHeaderCode;

[JsonProperty]
protected string description = string.Empty;

[JsonProperty]
protected FinancialFormatPurpose purpose = FinancialFormatPurpose.Standard;

[JsonProperty]
protected IList<FinancialFormatDetailDto> details = new List<FinancialFormatDetailDto>();

[JsonProperty]
protected bool active = true;

The sub-class has this:

public RecoveryFormatHeaderDto()
  : base() {
  this.purpose = FinancialFormatPurpose.Recovery;
}

[JsonProperty]
private bool isPerSquareArea;

They both have the JsonObject(MemberSerialization.OptIn) and Serializable attributes above the class name (but under the namespace).

My problem is that when I try to save an instance of the sub-class I get the following error:

Newtonsoft.Json.JsonSerializationException was unhandled by user code

Message=A member with the name 'financialFormatHeaderID' already exists on 'RecoveryFormatHeaderDto'. Use the JsonPropertyAttribute to specify another name.

Source=Newtonsoft.Json

What is happening here? How do I fix this?

EDIT: Class signatures are as follows:

public class FinancialFormatHeaderDto
public class RecoveryFormatHeaderDto : FinancialFormatHeaderDto

Though I don't know Json.Net, my guess would be that the problem is somewhat related to that you have the JsonObjectAttribute(MemberSerialization.OptIn) on both classes. Perhaps you should try removing that attribute from the sub-class, and see if it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM