Retrofit JSON反序列化对象的$ref引用到它的原始副本

7

我正在使用Microsoft.Net和Breeze制作API,使用Retrofit获得的结果中有重复嵌套的同一对象。例如,EmployeeJob具有Customer导航属性,因此API的结果如下所示:

 {
   Id:1,
   "Customer_Id": 39,
    "Customer": {
        "$id": "2",
        "$type": "Wit.Trade.Entities.Customer, Wit.Trade",
        "CourtesyTitle": "Mr",
        "FirstName": "Ahmad"
    }
}
{
  Id:2
  "Customer_Id": 39,
    "Customer": {
        "$ref": "2" //here same customer Ahmad
    },
}

现在我得到的这个与EmployeeJobs相关的Java List只有第一条记录中有Customer,其他没有。我该如何将$ref:“2”映射到其原始值而不是这个$ref出于网络和性能方面的考虑,我不希望我的服务器API发送完整的对象,这就是为什么我想在客户端上反序列化这些$refs,就像Angularjs $resource service为我们所做的那样。

你有没有碰巧找到这个问题的答案? - Lunchbox
我正在手动为您发布答案。 - ahmadalibaloch
1个回答

1

目前我已手动解决了$ref的问题,就像这样

//========== $ref manual solution for employee jobs' customers
            List<Customer> completedCustomers = new ArrayList<>();
            for (EmployeeJob empJob : empJobs) {
                if (empJob.Customer != null && empJob.Customer.Id == null && empJob .Customer.$ref != null) {
                    for (Customer comCus : completedCustomers) {
                        if (comCus.$id.equalsIgnoreCase(empJob.Customer.$ref))
                            empJob.Customer = comCus;
                    }
                } else
                    completedCustomers.add(empJob.Customer);
            }

现在,empJobs 已经用它们相应的客户替换了 $refs。

Jesse Wilson在https://github.com/square/moshi/issues/146上回答你的问题时建议编写一个类似于Gson的GraphAdapterBuilder适配器。你没有成功吗?我无法确认它是否像他所说的那样“简单明了”。我只能部分理解,这还不够。 - The incredible Jan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接