Newtonsoft.Json serialization of PagedList<T> is not including some properties

廉价感情. 提交于 2019-12-05 02:39:21

问题


I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like this:

PagedList<Product> pagedList = new PagedList<Product>(products, (page - 1), pageSize);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(pagedList);

If I use the above code, in the result I get an array of Product objects serialized properly. However the properties below (of PagedList) are not being included in the Json result:

    public bool HasNextPage { get; }
    public bool HasPreviousPage { get; }
    public bool IsFirstPage { get; }
    public bool IsLastPage { get; }
    public int ItemEnd { get; }
    public int ItemStart { get; }
    public int PageCount { get; }
    public int PageIndex { get; }
    public int PageNumber { get; }
    public int PageSize { get; }
    public int TotalItemCount { get; }

They are not being serialized but they are part of PagedList.

Does anyone know why? And how could I include those properties in the serialization?

Thanks


回答1:


The serializer sees that PagedList is enumerable, so it serializes it to a JavaScript array. To make this easier to deal with I expose a GetMetaData() function on the PagedList object that will return a MetaData object containing exactly the fields you mentioned above. This means you can serialize your pagedlist like so:

string json = Newtonsoft.Json.JsonConvert.SerializeObject(new{
  items = pagedList,
  metaData = pagedList.GetMetaData()
});

This should result in a JSON object like so:

{
    "Items": [
        { ... },
        { ... },
        { ... }
    ],
    "MetaData": {
        "PageSize": 1,
        "PageCount": 2,
        "HasNextPage": true,
        ...
    }
}



回答2:


there is a jquery plugin which is used to excitely to do this :

https://github.com/turhancoskun/pagify.mvc

<script type="text/javascript">
$(function() {
    $('#users').pagify({
        dataUrl: '/User/UserLis',
        callBack: function(){
           // Ajax remove preloader and some other callbacks  
        },
        beforeSend: function(){
           // Ajax show preloader and some other function before start
        }
    });
}
</script>

readme.md file contains an example




回答3:


To serialize, just use the Troy's implementation. But to deserialize create 2 new classes:

public class PaginationEntity<TEntity> where TEntity : class 
{
    public PaginationEntity()
    {
        Items = new List<TEntity>();
    }
    public IEnumerable<TEntity> Items { get; set; }
    public PaginationMetaData MetaData { get; set; }
}

public class PaginationMetaData
{
    public int Count { get; set; }
    public int FirstItemOnPage { get; set; }
    public bool HasNextPage { get; set; }
    public bool HasPreviousPage { get; set; }
    public bool IsFirstPage { get; set; }
    public bool IsLastPage { get; set; }
    public int LastItemOnPage { get; set; }
    public int PageCount { get; set; }
    public int PageNumber { get; set; }
    public int PageSize { get; set; }
    public int TotalItemCount { get; set; }
}

and deserialize this way:

PaginationEntity<TEntity> listPaginated = JsonConvert.DeserializeObject<PaginationEntity<TEntity>>(json)

and you are good to go.




回答4:


By default asp.core uses DataContractSerializer, so anyone could set [DataContract] and [DataMember] to Product class and members and get needed data with following action:

 public IActionResult Products(int page=1,int countPerPage=10)
    {
        var result = _dbContext.Products.AsNoTracking().ToPagedList(page, countPerPage);

        return Ok( new{result, total=result.TotalItemCount });
    }

with no need to use Newtonsoft.Json



来源:https://stackoverflow.com/questions/10870618/newtonsoft-json-serialization-of-pagedlistt-is-not-including-some-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!