Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4447229
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: 网络与安全

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.

Plain-Jane Arrays

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[]=4
Simple 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[]=5
See 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]=5
If 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.



Associative Arrays

So our plain-jane arrays are useful, but only so useful if we want to have some  (see "options" there). First, some simple data:
$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]=36
But 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]=Vette
Hmm. 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]=Vette
Much better. Hopefully you can now convert whatever you have into a serialized array of HTTP parameters for the web service you’re working for.
阅读(1377) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~