Is there a way to change name of Data property during serialization, so I can reuse this class in my WEB Api.
For an example, if i am returning paged list of users, Data property should be serialized as "users", if i'm returning list of items, should be called "items", etc.
Is something like this possible:
public class PagedData
{
[JsonProperty(PropertyName = "Set from constructor")]??
public IEnumerable<T> Data { get; private set; }
public int Count { get; private set; }
public int CurrentPage { get; private set; }
public int Offset { get; private set; }
public int RowsPerPage { get; private set; }
public int? PreviousPage { get; private set; }
public int? NextPage { get; private set; }
}
EDIT:
I would like to have a control over this functionality, such as passing name to be used if possible. If my class
is called UserDTO
, I still want serialized property to be called Users
, not UserDTOs
.
Example
var usersPagedData = new PagedData("Users", params...);
have a look here: How to rename JSON key
Its not done during serialization but with a string operation.
Not very nice (in my eyes) but at least a possibility.
Cheers Thomas
Another option with no need to play with json formatters or use string replacements - only inheritance and overriding (still not very nice solution, imo):
You can do this with a custom
ContractResolver
. The resolver can look for a custom attribute which will signal that you want the name of the JSON property to be based on the class of the items in the enumerable. If the item class has another attribute on it specifying its plural name, that name will then be used for the enumerable property, otherwise the item class name itself will be pluralized and used as the enumerable property name. Below is the code you would need.First let's define some custom attributes:
And then the resolver:
Now you can decorate the variably-named property in your
PagedData<T>
class with the[JsonPropertyNameBasedOnItemClass]
attribute:And decorate your DTO classes with the
[JsonPluralName]
attribute:Finally, to serialize, create an instance of
JsonSerializerSettings
, set theContractResolver
property, and pass the settings toJsonConvert.SerializeObject
like so:Fiddle: https://dotnetfiddle.net/GqKBnx
If you're using Web API (looks like you are), then you can install the custom resolver into the pipeline via the
Register
method of theWebApiConfig
class (in theApp_Start
folder).Another Approach
Another possible approach uses a custom
JsonConverter
to handle the serialization of thePagedData
class specifically instead using the more general "resolver + attributes" approach presented above. The converter approach requires that there be another property on yourPagedData
class which specifies the JSON name to use for the enumerableData
property. You could either pass this name in thePagedData
constructor or set it separately, as long as you do it before serialization time. The converter will look for that name and use it when writing out JSON for the enumerable property.Here is the code for the converter:
To use this converter, first add a string property called
DataPropertyName
to yourPagedData
class (it can be private if you like), then add a[JsonConverter]
attribute to the class to tie it to the converter:And that's it. As long as you've set the
DataPropertyName
property, it will be picked up by the converter on serialization.Fiddle: https://dotnetfiddle.net/8E8fEE
Here is a solution that doesn't require any change in the way you use the Json serializer. In fact, it should also work with other serializers. It uses the cool DynamicObject class.
The usage is just like you wanted:
The following is another solution tested in .NET Standard 2.
I am using Humanizer for pluralization.