C++,python,热爱算法和机器学习
全部博文(1214)
分类: 网络与安全
2014-10-27 19:57:45
Serialization is the process of converting a data structure or object state into a format which 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 or as a - if someone asked you to send a value of 1 across with a parameter named min_value, you'd probably (correctly) come up with something like:
Side Note: Though the examples here are GETs, try to use POST requests whenever possible as they prevent you from running into the dreaded errors.
Scalar values are simple enough, but you’re going to need to send something similar to an or . Unfortunately, the HTTP specs don’t really specify how we should do that.
For better or worse, ages ago (which we happen to use server-side) solved this, and lots of other web frameworks have followed suit – or at least support its syntax. Let’s look at a couple examples. Warning: you are going to have to 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:
[]=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:
[]=0&seq[]=1&seq[]=1&seq[]=2&seq[]=3&seq[]=5See the possible problem? Yep, there are two "1" values. So what happens? Maybe you get two "1"s or maybe a duplicate value gets swallowed up. To avoid any unexpected behavior, 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 them. So now we have:
[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. Actually, you pretty much definitely lose a value, but which one? Right, 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:
[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, darn. Now we have an array of associative arrays! First we’'ll try this:
[][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]=VetteHmm. We just ran into that problem, didn't we? And now we have multiple dimensions, so all bets are off. Instead, let's do this:
[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]=VetteMuch better. Hopefully you can now convert whatever you have into a serialized array of HTTP parameters for the web service you’re working for.