•JavaScriptConverter:自定义序列化与反序列化能力
•JavaScriptTypeResolver:建立数据类型与标识符的映射
•不是真正的JSON序列化
–时间日期对象
–Sys.Serialization.JavaScriptSerializer
aspx
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
var serializer = Sys.Serialization.JavaScriptSerializer;
// debugger;
var date = serializer.deserialize('<%= this.SerializedDateTime %>');
// debugger;
var intern = serializer.deserialize('<%= this.GetSerializedEmployee(0) %>');
var vendor = serializer.deserialize('<%= this.GetSerializedEmployee(1) %>');
var fte = serializer.deserialize('<%= this.GetSerializedEmployee(2) %>');
</script>
</form>
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
var serializer = Sys.Serialization.JavaScriptSerializer;
// debugger;
var date = serializer.deserialize('<%= this.SerializedDateTime %>');
// debugger;
var intern = serializer.deserialize('<%= this.GetSerializedEmployee(0) %>');
var vendor = serializer.deserialize('<%= this.GetSerializedEmployee(1) %>');
var fte = serializer.deserialize('<%= this.GetSerializedEmployee(2) %>');
</script>
</form>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Web.Script.Serialization;
using ComplexType;
using TypeResolver;
public partial class _6_Serialization : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected string SerializedDateTime
{
get
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(DateTime.Now);
}
}
protected string GetSerializedEmployee(int id)
{
Employee emp = null;
switch (id)
{
case 0: emp = new Intern(); break;
case 1: emp = new Vendor(); break;
default: emp = new FulltimeEmployee(); break;
}
JavaScriptSerializer serializer = new JavaScriptSerializer(new CustomizeTypeResolver());
return serializer.Serialize(emp);
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Web.Script.Serialization;
using ComplexType;
using TypeResolver;
public partial class _6_Serialization : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected string SerializedDateTime
{
get
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(DateTime.Now);
}
}
protected string GetSerializedEmployee(int id)
{
Employee emp = null;
switch (id)
{
case 0: emp = new Intern(); break;
case 1: emp = new Vendor(); break;
default: emp = new FulltimeEmployee(); break;
}
JavaScriptSerializer serializer = new JavaScriptSerializer(new CustomizeTypeResolver());
return serializer.Serialize(emp);
}
}