Retrofit JSON deserializing object's $ref reference to its original copy

梦想与她 提交于 2019-12-21 04:53:11

问题


I am using Microsoft.Net with Breeze for APIs and the results I get using Retrofit have nested repeated same objects. For example EmployeeJob has Customer navigation property so the APIs result looks like this

 {
   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
    },
}

Now the Java List I get of these EmployeeJobs has only Customer in the first record and others have nothing. How can I map the $ref:"2" to its original value instead of this $ref.

I don't want my server APIs to send the complete objects for network and performance reasons, that's why I want to deserialize these $refs on client side just like Angularjs $resource service does for us.


回答1:


Currently I have worked arround manually for the $ref solution like this

//========== $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);
            }

Now empJobs has the $refs replaced with their corresponding Customers.



来源:https://stackoverflow.com/questions/35771483/retrofit-json-deserializing-objects-ref-reference-to-its-original-copy

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