A few weeks back I decided to convert the APIs I use on my website to a schema-based format. I knew I wanted to use a rich JSON structure, but was this before I knew anything about the existence of JSON schema--all I knew was that I wanted to throw out my current crude approach and do something more similar to what ebay does (
http://developer.ebay.com/DevZone/finding/CallRef/findItemsAdvanced.html), only without XML. On Friday I discovered JSON schema, fell in love, and have been madly coding since.
I've been having a ball with it, and everything is fitting together wonderfully on back end calls and ajax calls, but there's one thing that puzzles me: how do query parameters factor into this?
I'll give you an example of what I mean. In my main schema I have a "sort" object:
"sort":{
"type":"object",
"description":"Specify a sort and a direction",
"properties":{
"type":{
"enum":[
"alpha",
"productId",
"releaseDate"
],
"description":"The property to sort on",
"default":"alpha"
},
"direction":{
"enum":[
"ascending",
"descending"
],
"description":"Choose a direction to sort",
"default":"ascending"
}
}
}
I have the usual little sort widget on my site, and in my old model, whenever it changes, I would add its value (appending ".d" for a descending direction) to the query params and refresh the page (try it yourself:
http://www.shmax.com/products/1?sort=release_date.d).
Unless I change what I'm doing with my sort query parameter, I now have to write glue code to sort of convert what appears in the query parameters into a valid sort schema for my new schema-based API call on the back end. Is this what you guys generally do?
Or do you actually encode schema snippets into your query strings? Like:
Looks sort of ugly, but it does lessen the need for glue code.
Just wondering if you guys have any suggestions or insights from your own experience with something similar. Thanks much!