{"id":77,"date":"2013-03-29T17:14:56","date_gmt":"2013-03-29T17:14:56","guid":{"rendered":"http:\/\/chalakanth.wordpress.com\/?p=77"},"modified":"2013-04-20T22:43:11","modified_gmt":"2013-04-20T22:43:11","slug":"how-to-avoid-sending-nullempty-fields-between-a-restful-service-and-a-client","status":"publish","type":"post","link":"https:\/\/www.seonthemon.com\/wp351\/2013\/03\/29\/how-to-avoid-sending-nullempty-fields-between-a-restful-service-and-a-client\/","title":{"rendered":"How to avoid sending null\/empty fields between a RESTful service, and a client?"},"content":{"rendered":"<p>By it strictest definition, a RESTful service sends and receives &#8216;<em>resources<\/em>&#8216;, which when seen from an object-oriented perspective, are largely the objects of your domain model. \u00a0An insurance policy, a bill, a payment, a car, a truck, a driver, a patient, a nurse, a clinic, etc.<\/p>\n<p>Often, an instance of the object that you are putting on the wire has null fields. \u00a0 Here is a patient at a clinic, whose middle name and date of birth, we don&#8217;t have.<\/p>\n<pre style=\"padding-left: 60px;\">{\r\n       \"patientId\": \"THX1138\",\r\n       \"firstName\": \"John\",\r\n       \"middleName\": null,\r\n       \"lastName\": Patient,\r\n       \"dateOfBirth\": null,\r\n       \"address\": \"1, \u00a0Illness From, \u00a0Bankruptcy, HA-00000, USA\"\r\n}<\/pre>\n<p>In the mobile world, where we now pay for data usage, and where connections can be slow, it might be useful to minimize the amount of data we throw on the wire. \u00a0 When the mobile app retrieves the above person, John Patient, I want to drop the fields (middle name, and date of birth) that are null, and send just this.<\/p>\n<pre style=\"padding-left: 60px;\">{\r\n       \"patientId\": \"THX113\",\r\n       \"firstName\": \"John\",\r\n       \"lastName\": Patient,\r\n       \"address\": \"1, \u00a0Way Ticket To, \u00a0Bankruptcy, HA-00000, USA\"\r\n}<\/pre>\n<p>Further, I want the mobile app to be similarly discriminating when it sends data back to the RESTful service at the server.<\/p>\n<p>In effect, the marshalling mechanism at both ends should be able to create the patient object with just the data that is available, and leave the rest of the fields null.<\/p>\n<p>Javascript works that way natively. \u00a0 If I was running Javascript at both endpoints, the core language itself would pretty much give us this behavior (Ah, <a title=\"Node.js\" href=\"http:\/\/nodejs.org\/\" target=\"_blank\">node.js<\/a>!).<\/p>\n<p>I expect this is possible in other environments too. \u00a0 In particular, I will need to look into this for the following.<\/p>\n<ul>\n<li><span style=\"line-height: 13px;\">RESTful service in Java, talking to\u00a0<\/span>\n<ul>\n<li>Android App in Java<\/li>\n<li>iOS App in Objective-C<\/li>\n<li>Javascript MVC<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>68 dollar question<\/h4>\n<p><em>Is this really worth the trouble?<\/em> \u00a0 Will this really produce a lot of efficiency?<\/p>\n<p>Is it even worth pursuing this question?<\/p>\n<p>When data usage costs money, and connections can be slow, and intermittent, why would you want to carry unnecessary baggage? \u00a0Even in the best of circumstances, why would you want to? \u00a0This sounds like it ought to be a first principle for any mobile app.<\/p>\n<p>But, we do want to watch out for &#8216;premature optimization&#8217; &#8211; getting clever too soon, or even unnecessarily.<\/p>\n<p>Hence, we will need to know how to test for this? \u00a0<em>How do you simulate, and test spotty mobile connections<\/em>?<\/p>\n<h4>Related questions<\/h4>\n<ul>\n<li>What other techniques exist for dealing minimizing the data usage of your mobile app?<\/li>\n<\/ul>\n<ul>\n<li>What other techniques exist for dealing with intermittent, or slow connections?<\/li>\n<\/ul>\n<p>These just seem a more severe variation of a problem that has been around for longer than the mobile world &#8211; how to build a UI around large-grained, stateless APIs. \u00a0 So there are answers out there &#8230;..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By it strictest definition, a RESTful service sends and receives &#8216;resources&#8216;, which when seen from an object-oriented perspective, are largely the objects of your domain model. \u00a0An insurance policy, a bill, a payment, a car, a truck, a driver, a patient, a nurse, a clinic, etc. Often, an instance of the object that you are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[11,12,10],"_links":{"self":[{"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":1,"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":125,"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/posts\/77\/revisions\/125"}],"wp:attachment":[{"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.seonthemon.com\/wp351\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}