Serialization, as defined on Wikipedia, is the process of converting a data structure or object state into a format that can be stored and "resurrected" later in the same or another computer environment.
You're probably way too familiar with taking a scalar value and sending it as an HTTP parameter in a GET query string or as a POST request - if you were asked to send a value of 1 across with a parameter named min_value, you'd likely (correctly) come up with something like:
http://submit.to/location?min_value=1side note: while we're here - try to use POST requests whenever possible as they prevent you from running into the dreaded HTTP 414 : Request-URI Too Long errors. And yes, I realize the examples here are all GETs.
Ok, so scalar values are simple enough. But you know you're going to need to send something similar to an Array or Associative Array / Hash / Map / Dict. Unfortunately the HTTP specs don't really specify how we should do that.
For better or worse, ages ago PHP (which we happen to use server side) solved this and many other web frameworks have more or less followed suit (or at least support its syntax). Let's look at a couple examples - and yes, you are going to have to be a big boy, take your thumb out of your mouth, and look at some PHP code...
Say we have this:
$data = array(3, 1, 4);and we need to pass that across as the value for the HTTP parameter pie. We'd end up with this:
http://submit.to/location?pie[]=3&pie[]=1&pie[]=4Simple enough. Now consider this data:
$data = array(0, 1, 1, 2, 3, 5);that we need to send as seq. The quick answer is this:
http://submit.to/location?seq[]=0&seq[]=1&seq[]=1&seq[]=2&seq[]=3&seq[]=5Hmm - see the possible problem? Yeah, there are two "1" values. So what's going to happen? Maybe you get two "1"s or maybe a duplicate value gets swallowed up. To avoid any unexpected behavior, now we're going to add unique keys which should mean all of the values get passed through. If the server's not looking for keys (read the docs), it should just ignore... so now we have:
http://submit.to/location?seq[0]=0&seq[1]=1&seq[2]=1&seq[3]=2&seq[4]=3&seq[5]=5If you repeat those keys, you're back in unexpected territory. Aactually, you pretty much definitely lose a value, but now more fun - which one? Yeah, so be careful.
$ages = array('Becky'=>36, 'Jane'=>24, 'Tony'=>36);
that we need to pass across as ages. Yup, just like above, those will be keys (and they still need to be unique!):
http://submit.to/location?ages[Becky]=36&ages[Jane]=24&ages[Tony]=36But what if we have something like this?
$people = array(
array('name'=>'Becky', 'age'=>36, 'car'=>'Benz'),
array('name'=>'Jane', 'age'=>24, 'car'=>'Honda'),
array('name'=>'Tony', 'age'=>36, 'car'=>'Vette')
);
Well hell, now we have an array of associative arrays! First we try:
http://submit.to/location?peeps[][name]=Becky&peeps[][age]=36&peeps[][car]=Benz
&peeps[][name]=Jane&peeps[][age]=24&peeps[][car]=Honda
&peeps[][name]=Tony&peeps[][age]=36&peeps[][car]=Vette
Hmm, we just ran into that problem, didn't we? And now we have multiple dimensions, so all bets are really off. Instead, let's do this:
http://submit.to/location?peeps[0][name]=Becky&peeps[0][age]=36&peeps[0][car]=Benz
&peeps[1][name]=Jane&peeps[1][age]=24&peeps[1][car]=Honda
&peeps[2][name]=Tony&peeps[2][age]=36&peeps[2][car]=Vette
Ah, much better. I could go on and on with more contrived examples, but hopefully you can now convert whatever you have into a serialized
array of HTTP parameters for the web service you're working for.