Reputation: 4956
I'm writing an API for my application and I've modified the default serializer to use the ServiceStack libraries version 3.9.71. I have two endpoints that return similar data, but in one case I want to exclude certain properties of the object. Here's the object I'm using:
public class Folder
{
public int ID { get; set; }
public string Name { get; set; }
public int? ParentID { get; set; }
public virtual ICollection<Folder> Children { get; set; }
public virtual ICollection<File> Files { get; set; }
}
In one API endpoint, I want to return the entire object including the collections Children and Files. In another, I want to return everything except the collections Children and Files in order to reduce the amount of data that gets set to the client.
Using [DataContract] with [DatMember] or just [IgnoreDataMember] doesn't seem to work here because those properties seem to be ignored during deserialization even if they are included in the JSON.
Likewise, JsConfig.ExcludePropertyNames does seem like it would work either due to the fact that it's a static property and I have no way to reset the list after sending the JSON response and before the next call is made.
I've also tried nulling out the collections prior to serialization, but the objects are attached to a DbContext so it doesn't seem to have any affect.
Is there anyway to return different serializations of the same object like I'm trying?
Upvotes: 0
Views: 280
Reputation: 3068
Duplicate?
How to omit Get only properties in servicestack json serializer?
But to answer your question, I'd recommend having the service set the unwanted properties to null before returning it on the response object. I don't understand why you'd attach a permanent reference to the DbContext (assuming this refers to Entity Framework), as the DbContext is rather ephemeral and should be used like database connection.
Upvotes: 1