OData响应在json的根级别上保留原始值

时间:2017-05-03 21:48:47

标签: c# serialization odata

我将OData实现为GET调用,以便从更大的返回对象扩展单个对象类型,以降低序列化成本。不幸的是,ODataQueryOptions并没有以我期望的格式返回序列化对象。也就是说,它保留了在" root"中定义的任何原始类型值。当我没有在展开中指定它们时,对象的级别(序列化时)。

服务器端代码:

public async Task<HttpResponseMessage> RetrieveDocument(int id, ODataQueryOptions<Contract.EstimateDocument> oDataQuery)
    {
        var cacheKey = EstimateDocumentCacheKeyGenerator.GetCacheKeyForEstimateDocument(id, CurrentUserSession.UserOrgCode, oDataQuery.RawValues.Filter, oDataQuery.RawValues.Expand);
        string serializedEstimateDocument;

        if (!_serializedEstimateDocumentCache.TryGet(cacheKey, out serializedEstimateDocument))
        {
            var estimateDocument = _estimateDocumentService.RetrieveDocument(CurrentUserSession, id);
            if (oDataQuery.SelectExpand != null||
                oDataQuery.Filter != null)
            {
                IQueryable<Contract.EstimateDocument> estimateDocumentQueryable =
                    new List<Contract.EstimateDocument>() {estimateDocument}.AsQueryable();
                var result = oDataQuery.ApplyTo(estimateDocumentQueryable);
                serializedEstimateDocument = CacheResponseObject(result, cacheKey);
            }
            else
            {
                serializedEstimateDocument = CacheResponseObject(estimateDocument, cacheKey);
            }
        }

        var responseMessage = CreateMessage(serializedEstimateDocument, HttpStatusCode.OK);

        //LogApiCall(id, responseMessage);
        return responseMessage;
    }

当我扩展psudo-object类型时,我得到了什么:

[{
    "psudoObject" : { /*contents*/ },
    "primitive1": value1,
    "primitive2": value2,
    "primitive3": value3
    etc...
}]

预期:

[{
    "psudoObject": { /*contents*/ }
}]

是否有OData设置我可以改变以获得我的预期结果?如果失败了,有没有办法删除那些不涉及改变序列化对象字符串的对象?

0 个答案:

没有答案