使用自定义JavaScriptConverter返回自定义的复杂类型
2008-04-27 15:12
525 查看
自定义JavaScriptConverter
•用于处理复杂类型
–处理循环引用
–简化默认的复杂序列化和反序列化行为
•定义并使用一个JavaScriptConverter
–定义一个Converter继承JavaScriptConverter类
–实现SupportedTypes
–实现Serialize方法用于序列化复杂数据
–实现Deserialize方法用于反序列化复杂数据
–在web.config内注册该Converter
aspx
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="BoyGirlService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>
<input type="button" value="Get Boy" onclick="getBoy()" />
<input type="button" value="Set Boy" onclick="setBoy()" />
<script language="javascript" type="text/javascript">
function getBoy()
{
BoyGirlService.GetBoyWithGirlFriend(onGetBoySucceeded, onFailed);
}
function onGetBoySucceeded(result)
{
// result.GirlFriend.BoyFriend = result;
alert(String.format(
"It's {0}, his girlfriend is {1}",
result.Name,
result.GirlFriend.Name));
}
function onFailed(error)
{
alert(error.get_message());
}
function setBoy()
{
var boy = new Object();
boy.Name = "Terry";
var girl = new Object();
girl.Name = "Mary";
boy.GirlFriend = girl;
BoyGirlService.SetBoyWithGirlFriend(boy, onSetBoySucceeded, onFailed);
}
function onSetBoySucceeded(result)
{
alert(result);
}
</script>
</form>
BoyGirlService.asmx
<%@ WebService Language="C#" Class="BoyGirlService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using ComplexType;
using System.Diagnostics;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class BoyGirlService : System.Web.Services.WebService {
[WebMethod]
public Boy GetBoyWithGirlFriend()
{
Boy boy = new Boy();
boy.Name = "Terry";
Girl girl = new Girl();
girl.Name = "Marry";
boy.GirlFriend = girl;
girl.BoyFriend = boy;
return boy;
}
[WebMethod]
public string SetBoyWithGirlFriend(Boy boy)
{
Debug.Assert(boy == boy.GirlFriend.BoyFriend);
return String.Format(
"It's {0}, his girlfriend is {1}",
boy.Name, boy.GirlFriend.Name);
}
}
BoyAndGirl.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace ComplexType
{
/// <summary>
/// Summary description for Boy
/// </summary>
public class Boy
{
public string Name;
public Girl GirlFriend;
}
public class Girl
{
public string Name;
public Boy BoyFriend;
}
}
web.config中的</system.web>后面加入以下代码,指定要用到的Converter,这里其中的一个“BoyConverter”是一个自定义的Converter
<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
<jsonSerialization>
<converters>
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview" />
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview" />
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview" />
<add name="BoyConverter" type="Converter.BoyConverter, App_Code" />
</converters>
</jsonSerialization>
<!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
<!--
<authenticationService enabled="true" requireSSL = "true|false"/>
-->
<!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
writeAccessProperties attributes. -->
<!--
<profileService enabled="true"
readAccessProperties="propertyname1,propertyname2"
writeAccessProperties="propertyname1,propertyname2" />
-->
</webServices>
<!--
<scriptResourceHandler enableCompression="true" enableCaching="true" />
-->
</scripting>
</system.web.extensions>
•用于处理复杂类型
–处理循环引用
–简化默认的复杂序列化和反序列化行为
•定义并使用一个JavaScriptConverter
–定义一个Converter继承JavaScriptConverter类
–实现SupportedTypes
–实现Serialize方法用于序列化复杂数据
–实现Deserialize方法用于反序列化复杂数据
–在web.config内注册该Converter
aspx
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="BoyGirlService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>
<input type="button" value="Get Boy" onclick="getBoy()" />
<input type="button" value="Set Boy" onclick="setBoy()" />
<script language="javascript" type="text/javascript">
function getBoy()
{
BoyGirlService.GetBoyWithGirlFriend(onGetBoySucceeded, onFailed);
}
function onGetBoySucceeded(result)
{
// result.GirlFriend.BoyFriend = result;
alert(String.format(
"It's {0}, his girlfriend is {1}",
result.Name,
result.GirlFriend.Name));
}
function onFailed(error)
{
alert(error.get_message());
}
function setBoy()
{
var boy = new Object();
boy.Name = "Terry";
var girl = new Object();
girl.Name = "Mary";
boy.GirlFriend = girl;
BoyGirlService.SetBoyWithGirlFriend(boy, onSetBoySucceeded, onFailed);
}
function onSetBoySucceeded(result)
{
alert(result);
}
</script>
</form>
BoyGirlService.asmx
<%@ WebService Language="C#" Class="BoyGirlService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using ComplexType;
using System.Diagnostics;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class BoyGirlService : System.Web.Services.WebService {
[WebMethod]
public Boy GetBoyWithGirlFriend()
{
Boy boy = new Boy();
boy.Name = "Terry";
Girl girl = new Girl();
girl.Name = "Marry";
boy.GirlFriend = girl;
girl.BoyFriend = boy;
return boy;
}
[WebMethod]
public string SetBoyWithGirlFriend(Boy boy)
{
Debug.Assert(boy == boy.GirlFriend.BoyFriend);
return String.Format(
"It's {0}, his girlfriend is {1}",
boy.Name, boy.GirlFriend.Name);
}
}
BoyAndGirl.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace ComplexType
{
/// <summary>
/// Summary description for Boy
/// </summary>
public class Boy
{
public string Name;
public Girl GirlFriend;
}
public class Girl
{
public string Name;
public Boy BoyFriend;
}
}
web.config中的</system.web>后面加入以下代码,指定要用到的Converter,这里其中的一个“BoyConverter”是一个自定义的Converter
<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
<jsonSerialization>
<converters>
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview" />
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview" />
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview" />
<add name="BoyConverter" type="Converter.BoyConverter, App_Code" />
</converters>
</jsonSerialization>
<!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
<!--
<authenticationService enabled="true" requireSSL = "true|false"/>
-->
<!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
writeAccessProperties attributes. -->
<!--
<profileService enabled="true"
readAccessProperties="propertyname1,propertyname2"
writeAccessProperties="propertyname1,propertyname2" />
-->
</webServices>
<!--
<scriptResourceHandler enableCompression="true" enableCaching="true" />
-->
</scripting>
</system.web.extensions>
相关文章推荐
- javascript中创建自定义类型的最常见的方式_组合使用构造函数模型和原型模式,动态原型模式
- 使用Hibernate返回自定义类型时报错的问题
- 使用Hibernate返回自定义类型时报错的问题
- Spring.Net -- 如何使用用户自定义类型转换器 (Custom Type Converter )
- webwok中使用自定义的返回类型(result_type)
- SpringMVC使用fastjson自定义Converter支持返回jsonp格式
- 深入Atlas系列:Web Sevices Access in Atlas示例(5) - 自定义TypeConverter把基础类型转换为复杂类型
- 深入Atlas系列:Web Sevices Access in Atlas示例(5) - 自定义TypeConverter把基础类型转换为复杂类型
- 使用VS2012 MFC自定义消息时,处理函数不能返回void类型。
- 深入Atlas系列:Web Sevices Access in Atlas示例(5) - 自定义TypeConverter把基础类型转换为复杂类型
- J2EE下使用AJAX(六) 后记以及如何处理复杂的返回值类型
- 使用JavaScriptConverter实现返回DataTable对象
- Nhibernate使用HQL返回自定义类型
- SpringMVC使用fastjson自定义Converter支持返回jsonp格式(转)
- JavaScriptSerializer序列化和反序列化JSON:使用自定义JavaScriptConverter JSON的序列化和反序列化已经成为Web开发必不可少的知识。现在常用的有Syste
- 深入剖析Axis2中返回值为复杂自定义类型值【步步逼近实践篇】
- 32-35. Struts2_类型转换错误消息的显示和定制&自定义类型转换器&类型转换器与复杂属性&集合协同使用
- Android 使用Retrofit自定义Converter解析相同接口返回不同数据
- SP2010开发和VS2010专家"食谱"--第四章节—列表定义和内容类型(3)--使用对象模型创建自定义内容类型
- multipart/form-data 类型的form使用request.getParameter()会返回null