I want to define a contract for a DTO which a consuming API project will implement and return as JSON.
From my perspective there are three concerns here:
- The properties defined by the contract
- The implementation (for example, this might be an Entity Framework POCO)
- The serialization metadata which defines the returned JSON
I'd like to define these three parts separately, but to the best of my knowledge I have to add the serialization decoration (DataMember attributes etc) in the implemtation of my contract. Which means the implementation is coupled with the JSON representation.
Is it possible to separate this serialization metadata into a separate class?
MetadataTypeAttribute. But see JSON.NET Serializes Empty JSON for some restrictions in the implementation.MetadataTypeAttributehas been migrated and renamed - stackoverflow.com/questions/34576921/…. I'll try this later on and report back.ModelMetadataTypeis not supported by Json.NET. See How to use ASP.Net core ModelMetadata attribute and Issue #1349: Add support for ModelMetadataType for dotnetcore like supported MetadataTypeAttribute in previous versions.