惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
NISL@THU
NISL@THU
S
Securelist
O
OpenAI News
S
Security Affairs
Cyberwarzone
Cyberwarzone
T
Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
SecWiki News
SecWiki News
S
Secure Thoughts
GbyAI
GbyAI
I
Intezer
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
Google Online Security Blog
Google Online Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
A
About on SuperTechFans
S
Schneier on Security
P
Proofpoint News Feed
雷峰网
雷峰网
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
H
Heimdal Security Blog
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
V
V2EX
L
Lohrmann on Cybersecurity
Security Latest
Security Latest
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
H
Hacker News: Front Page
Cisco Talos Blog
Cisco Talos Blog
Webroot Blog
Webroot Blog
T
Tenable Blog
MyScale Blog
MyScale Blog
博客园 - 司徒正美
S
SegmentFault 最新的问题
Y
Y Combinator Blog
腾讯CDC
Hacker News: Ask HN
Hacker News: Ask HN
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY

博客园 - dragonpig

Html 5 Canvas绘制分形图Mandelbrot .net中反射、emit、expression和dynamic的性能比较 const string 和 static readonly string的区别 SqlServer: Top N per Group 微软的BinarySearch 通过CTE实现Split CSV 通过SQL CTE计算Fibonacci .NET线程安全泛型Singleton 跨域访问Cookie WCF JSON和AspnetCompatibility的配置 Windows安装Memcached node.js初体验 教你如何制作Silverlight Visual Tree Inspector 一道非常有趣的概率题 教你30秒打造强类型ASP.NET数据绑定 当json.js遇见dynamic.net [0] 用Silverlight做雷达图 C#运算符重载不是没有用武之地 随机排列算法
当json.js遇见dynamic.net烂尾篇
dragonpig · 2011-02-27 · via 博客园 - dragonpig

由于最近较忙,上一篇的坑估计要慢慢填了。感觉写文章介绍要比写程序还累啊。先看看测试程序,能够了解api支持哪些功能:

static void Main(string[] args)
{
//wrapper对象
dynamic data = DJson.Wrap(new
{
name
= "Jane",
male
= false,
age
= 24,
dob
= DateTime.Now,
friend
= new { name = "Jesse", male = true, age = 32, dob = DateTime.Now },
mobile
= new object[] { new[] { 86 }, 13888888888 },
});

Console.WriteLine(data.Stringfy());

//数组
data = new DJsonArray();
data[
0] = new { name = "小朋盂 1号" };
data[
1] = new { name = "小朋盂 2号" };
data[
2] = new { name = "小朋盂 3号" };

Console.WriteLine(data.Stringfy());

//反序列化
var json = data.Stringfy();
dynamic obj
= DJson.Parse(json);
//修改反序列化后的数据
obj[1] = new { name = "mike", age = 21, dob = DateTime.Now };
Console.WriteLine(obj.Stringfy());
//实际应用,将twitter statues反序列化
json = System.IO.File.ReadAllText("e:/twitter.js");
dynamic statuses
= DJson.Parse(json);
for (int i = 0; i < statuses.Length; i++)
{
var status
= statuses[i];
Console.WriteLine(
" id: {0}\nname: {1}\nfrom: {2}\ntext: {3}\n", status.user.id, status.user.name, status.user.location, status.text);
}

Console.ReadKey();
}

twitter文件请在这里下载

先放上代码,其实很简单,以后再慢慢分析

public class DJson : DynamicObject
{
protected Dictionary<string, object> _expando = new Dictionary<string, object>();
public override string ToString()
{
return this.Stringify();
}
public IEnumerable<string> Keys { get { return _expando.Keys; } }
public object this[string key] { get { return _expando[key]; } set { _expando[key] = value; } }public DJson() { }public static DJson Wrap(object obj)
{
if (!(obj is string) && (obj is IEnumerable) && !(obj is Dictionary<string, object>))
{
var array
= new DJsonArray();
int i = 0;
foreach (object v in obj as IEnumerable)
array[i
++] = DJson.Wrap(v);
return array;
}
else
{
var dj
= new DJson();
if (obj is Dictionary<string, object>)
{
foreach (var kv in obj as Dictionary<string, object>)
{
if (kv.Value == null)
dj[kv.Key]
= (object)null;
else if (IsPrimitive(kv.Value.GetType()))
dj[kv.Key]
= kv.Value;
else
dj[kv.Key]
= DJson.Wrap(kv.Value);
}
}
else
{
var t
= obj.GetType();
if (IsPrimitive(t))
throw new ArgumentException();
foreach (var p in t.GetProperties())
{
var v
= p.GetValue(obj, null);
if (IsPrimitive(p.PropertyType))
dj[p.Name]
= v;
else
dj[p.Name]
= DJson.Wrap(v);
}
}
return dj;
}
}
public static DJson Parse(string json)
{
var obj
= (new JavaScriptSerializer()).DeserializeObject(json);
return DJson.Wrap(obj);
}
public string Stringify()
{
var serializer
= new JavaScriptSerializer();
serializer.RegisterConverters(
new JavaScriptConverter[] { new DJsonConverter() });if (this is DJsonArray)
return serializer.Serialize((this as DJsonArray).ToArray());return serializer.Serialize(this);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (_expando.TryGetValue(binder.Name, out result))
return true;
return false;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (!(value is string) && (value is IEnumerable))
_expando[binder.Name]
= new DJsonArray(value as IEnumerable);
else
_expando[binder.Name]
= value;
return true;
}
internal static bool IsPrimitive(Type type)
{
return type.FullName.StartsWith("System.") && !type.FullName.StartsWith("System.Collection");
}
}
public class DJsonArray : DJson
{
Dictionary
<int, object> _array = new Dictionary<int, object>();
public object this[int index] { get { return _array[index]; } set { _array[index] = value; } }
public int Length { get { return _array.Keys.Count; } }public DJsonArray() { }
public DJsonArray(IEnumerable array)
{
int i = 0;
foreach (var obj in array)
this[i++] = obj;
}
public object[] ToArray()
{
return _array.Values.ToArray();
}
public DJson ToObject()
{
var dj
= new DJson();
foreach (string key in _expando.Keys)
dj[key]
= _expando[key];
foreach (int key in _array.Keys)
dj[key.ToString()]
= _array[key];
return dj;
}
}
internal class DJsonConverter : JavaScriptConverter
{
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var dj
= (DJson)obj;

var result

= new Dictionary<string, object>();
foreach (string key in dj.Keys)
{
if (dj[key] is DJsonArray)
result[key]
= (dj[key] as DJsonArray).ToArray();
else
result[key]
= dj[key];
}
return result;
}
public override IEnumerable<Type> SupportedTypes
{
get { return new List<Type>(new Type[] { typeof(DJson), typeof(DJsonArray) }); }
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
}

Newton的json库最近好像做了相同的事情,开始支持dynamic了

http://james.newtonking.com/archive/2011/01/03/json-net-4-0-release-1-net-4-and-windows-phone-support.aspx