zoukankan      html  css  js  c++  java
  • 使用自定义JavaScriptConverter返回自定义的复杂类型

    自定义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>
  • 相关阅读:
    FileUpload组件
    国际化
    dbutils
    BeanUtils
    c3p0连接池]
    JDBC代码模板
    JDBC基础与连接sql2012
    JSP以及JSP解析原理
    Tomcat使用,部署
    JAVA---反射
  • 原文地址:https://www.cnblogs.com/timy/p/1173145.html
Copyright © 2011-2022 走看看