简单实体Json序列化(输出JSON的属性可变)
一、先看效果
可以看出 , 我们在序列化一个对像时, 只给出了 我们想要 输出的两个字段名, 实际实体有5个属性, 经过可变属性序列化后的JSON 结果,也是我们想要的。
二、正式上程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;namespace TaskConsoleApplication
{
class Program
{
static void Main(string[] args)
{ var p = new Person { Age = , Name = "jinshuai", Meney = , Tex = , Berthday = DateTime.Now }; var s = new PropertyVariableJsonSerializer(); var result = s.Serialize<Person>(p, new List<string>() { "Name", "Age" }); Console.WriteLine(result); Console.WriteLine("----------------------------"); Console.Read(); }
}
public class Person
{
public string Name { get; set; } public int Age { get; set; } public double Meney { get; set; } public double Tex { get; set; } public DateTime Berthday { get; set; } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization; namespace TaskConsoleApplication
{ /// <summary>
///简单实体 可变属性序列化器
/// </summary>
public class PropertyVariableJsonSerializer
{
readonly JavaScriptSerializer _serializer = new JavaScriptSerializer(); /// <summary>
/// json 序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="propertys"></param>
/// <returns></returns>
public string Serialize<T>(T obj,List<string> propertys)
{
_serializer.RegisterConverters(new[] { new PropertyVariableConveter(typeof(T), propertys) }); return _serializer.Serialize(obj); }
} public class PropertyVariableConveter : JavaScriptConverter
{
private readonly List<Type> _supportedTypes = new List<Type>(); public PropertyVariableConveter(Type supportedType, List<string> propertys)
{
_supportedTypes.Add(supportedType);
Propertys = propertys; } private List<string> Propertys { get; set; } public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{ throw new Exception(" 这个暂时不支持 , 谢谢 "); } public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var dic = new Dictionary<string, object>(); var t = obj.GetType();
var properties= t.GetProperties(); foreach (var ite in properties)
{
string key = ite.Name;
var v = t.GetProperty(key).GetValue(obj, null); if (Propertys == null || Propertys.Count<=)
{
dic.Add(key, v);
continue;
} if (Propertys.Contains(key))
{
dic.Add(key, v);
}
} return dic; } public override IEnumerable<Type> SupportedTypes
{
get { return _supportedTypes; }
}
}
}
嗯。 就这些吧。 只是玩玩而以。
这次 源代码 就不上了。
附:
使用 JavaScriptConverter 类可以为 JavaScriptSerializer 类本身不支持的托管类型实现序列化和反序列化进程。此外,如果需要对序列化和反序列化进程进行更多控制,还可以使用 JavaScriptConverter。
SupportedTypes 属性指示自定义转换器为其提供转换器服务的类型。
若要指示 JavaScriptSerializer 实例必须使用自定义转换器,必须向该实例注册该转换器。如果要直接使用 JavaScriptSerializer 类,则应使用RegisterConverters 方法注册该转换器。否则,如果要从 ECMAScript (JavaScript) 调用 Web 方法并且希望使用自定义转换器,则可以通过在配置文件中添加 converters 元素来注册该转换器。有关详细信息,请参阅How to: Configure ASP.NET Services in Microsoft Ajax。
当 JavaScriptSerializer 实例序列化自身注册有自定义转换器的类型时,序列化程序将调用 Serialize 方法。同样,如果 JavaScriptSerializer 实例正在反序列化 JavaScript 对象符号 (JSON) 字符串并且识别出该 JSON 字符串中的某个类型具有与其关联的自定义转换器,序列化程序也将调用Deserialize 方法。
初始化 JavaScriptConverter 类的新实例。
当在派生类中重写时,获取受支持类型的集合。
Deserialize(IDictionary<String, Object>, Type, JavaScriptSerializer)
当在派生类中重写时,将所提供的字典转换为指定类型的对象。
确定指定的对象是否等于当前对象。(从 Object 继承。)
在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(从 Object 继承。)
作为默认哈希函数。(从 Object 继承。)
当在派生类中重写时,生成名称/值对的字典。
返回表示当前对象的字符串。(从 Object 继承。)
这次够 150 字了吧。