A WORK object is simply a way of looking of any JSON-like dictionary – it’s an “attitude”. The primary difference is in how we use that dictionary, especially in the context of using APIs. Here’s a rough overview of the thre
- the WORK is the interface – we don’t need to write specialized methods to deal with an API because we know what we’re looking for anyway. It’s not like we spend weeks looking at our API interface – typically, it’d be more minutes (once it’s coded) in a typically programming session
- what is in the WORK is defined by how we want to use it. For example, if we want an Integer, we ask for an integer for the WORK: it may be stored as a string, a boolean, an integer, or a float; it doesn’t matter. When using WORK objects we expect to the conversion to be done for us at runtime
- if you are looking for a list in the WORK and there’s another type of object, we pretend that it’s in a list of length 1
- if you are looking for an object (by key) and you find a list, look in the first object in the list
These rules are written from a pragmatic vision of using APIs: data is sometimes in lists, sometimes it isn’t. Sometimes we know the types being sent on the wire, sometimes it’s just strings.
In order to use WORK objects efficiently, we define a “dot-path” for accessing items that may be hierarchically nested. We’ll address the type coercion issue in another post. To illustrate our point, we’ll be working with the following WORK object
{ "name" : "Sally Jones", "age" : 22, "hobbies" : [ "Skiing", "Windsurfing", "Chillaxing" ], "sites" : { "facebook" : "http://www.facebook.com", "gmail" : "http://gmail.com", }, "address" : [ { "street" : "1 Main Street", "city", "Toronto", "province" : "Ontario", }, { "street" : "RR4", "city", "Bala", "province" : "Ontario", } ] }
Here’s a few dot-paths and the value they’ll retrieve:
- path:
name
value:"Sally Jones"
- path:
hobbies
value:[ "Skiing", "Windsurfing", "Chillaxing" ]
- path:
hobbies[1]
value:"Windsurfing"
- path:
address.street
value:"1 Main Street"
… this demonstrates seeing a list where we want a dictionary and just looking at the first object in the list - path:
sites[0].facebook
value:"http://www.facebook.com"
… this is an example of looking a list, not finding it and assuming there’s an list of length 1 there.sites[1].facebook
would return null.