I am looking for advice as to the implementation of an API
that filters the response based on the fields parameter (like the fields parameter in the youtube api - see here http://ift.tt/1mzBOzQ)
My response will contain a complex object that may look something like this
{
"Name": "test",
"Location": "north",
"Items": [
{
"Name": "item1",
"Size": "big",
"Weight": "heavy"
},
{
"Name": "item2",
"Size": "Small"
"Weight": "light"
}
],
"OrderDetail": {
"OrderNo": 12345,
"OrderCount": 5
}
}
In other words some kind of response object that will contain properties, some of which may hold other whole objects or even arrays of objects. Of course the response may also be an array of these items.
I want to add a parameter to the API which will filter the response to only include the specified properties and nested properties... So for eg if I have url param
fields=Name,Items(Name,Size),OrderDetail(OrderCount)
I will get back the following as my API response.
{
"Name": "test",
"Items": [
{
"Name": "item1",
"Size": "big"
},
{
"Name": "item2",
"Size": "Small"
}
],
"OrderDetail": {
"OrderCount": 5
}
}
I am trying to find the most efficient way for my service to perform this. Some of the ideas i have had (may or may not work) are as follows:
-
First generate an ExpandoObject based on the field param list and then just map by response data into the ExpandoObject -- Disadvantage: I dont know datatypes without reflecting out each property -- Advantage: should only have to reflect once and then use some mapper (although which probably also relies on reflection)
-
Iterate through my entire response object with reflection and null or empty fields based on the field param definition -- Disadvantage: Lots of redundant reflection
I dont know if either of these are good solutions. Please can someone give me some guidance here.
Please also keep in mind that the nested objects could even go 3 layers deep, for eg I could have had another object nested in my Item object which are returning within the array of Items Thanks
Aucun commentaire:
Enregistrer un commentaire