DynamicJSONserializer
|
https://github.com/ststeiger/DynamicJSONserializer/blob/master/DynamicJSONserializer/Program.cs namespace DynamicJSONserializer |
|
| { | |
| class MainClass | |
| { | |
| public class DynamicArguments | |
| { | |
| System.Collections.Generic.Dictionary<string, object> dict; | |
| public System.Collections.IEnumerable Keys | |
| { | |
| get{ | |
| return dict.Keys; | |
| } | |
| } | |
| public DynamicArguments() | |
| { | |
| dict = new System.Collections.Generic.Dictionary<string, object>(); | |
| } | |
| public DynamicArguments(string json) | |
| { | |
| dict = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, object>>(json); | |
| } | |
| public T GetValue<T>(string key) | |
| { | |
| bool bIsNullable = false; | |
| bool hasValue = dict.ContainsKey(key); | |
| System.Type t = typeof(T); | |
| if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(System.Nullable<>)) | |
| { | |
| t = System.Nullable.GetUnderlyingType(t); | |
| bIsNullable = true; | |
| } | |
| if (hasValue) | |
| { | |
| string value = System.Convert.ToString( dict[key], System.Globalization.CultureInfo.InvariantCulture); | |
| if (t == typeof(string)) | |
| return (T)(object) value; | |
| if (!string.IsNullOrEmpty(value)) | |
| { | |
| if (t == typeof(System.Int32)) | |
| return (T)(object) System.Int32.Parse(value); | |
| if (t == typeof(System.UInt32)) | |
| return (T)(object) System.UInt32.Parse(value); | |
| if (t == typeof(System.Int64)) | |
| return (T)(object) System.Int64.Parse(value); | |
| if (t == typeof(System.UInt64)) | |
| return (T)(object) System.UInt64.Parse(value); | |
| if (t == typeof(double)) | |
| return (T)(object) double.Parse(value); | |
| if (t == typeof(float)) | |
| return (T)(object) float.Parse(value); | |
| if (t == typeof(System.Guid)) | |
| return (T)(object)new System.Guid(value); | |
| if (t == typeof(bool)) | |
| { | |
| bool bReturnValue = false; | |
| if (bool.TryParse(value, out bReturnValue)) | |
| return (T)(object)bReturnValue; | |
| if (value == "0") | |
| return (T)(object)false; | |
| if(System.StringComparer.OrdinalIgnoreCase.Equals(value,"YES")) | |
| return (T)(object)true; | |
| if(System.StringComparer.OrdinalIgnoreCase.Equals(value,"NO")) | |
| return (T)(object)false; | |
| System.Int64 lng; | |
| if (System.Int64.TryParse(value, out lng)) | |
| return (T) (object) true; | |
| double dbl; | |
| if (double.TryParse(value, out dbl)) | |
| return (T)(object) true; | |
| return (T)(object)false; | |
| } | |
| if (t == typeof(System.DateTime)) | |
| { | |
| if((value.IndexOf('T') != -1) || (value.IndexOf('/') != -1 )) | |
| return (T)(object) System.DateTime.Parse(value, System.Globalization.CultureInfo.InvariantCulture); | |
| if(value.IndexOf('.') != -1) | |
| return (T) (object) System.DateTime.ParseExact(value, "dd.MM.yyyy", new System.Globalization.CultureInfo("de-CH", false)); | |
| return (T)(object) System.DateTime.Parse(value, System.Globalization.CultureInfo.InvariantCulture); | |
| } // End if (t == typeof(DateTime)) | |
| if(t == typeof(System.Enum)) | |
| return (T) (object) System.Enum.Parse(t, value); | |
| if (t == typeof(DynamicArguments)) | |
| return (T)(object) (new DynamicArguments(value)); | |
| } // End if (!string.IsNullOrEmpty(value)) | |
| } // End if (hasValue) | |
| if (bIsNullable) | |
| return (T) (object) null; | |
| T val = default(T); | |
| return (T) (object) val; | |
| } // End Function GetValue<T>(key) | |
| } // End Class | |
| public static void Main (string[] args) | |
| { | |
| Newtonsoft.Json.JsonSerializerSettings jss = new Newtonsoft.Json.JsonSerializerSettings(); | |
| jss.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; | |
| string str = Newtonsoft.Json.JsonConvert.SerializeObject(new System.DateTime(2013, 12, 31), jss); | |
| System.Console.WriteLine(str); | |
| string json = @"{ ""testobj"": {""foo"": ""bar"", ""lol"": ""rofl""}, ""stichtag_msft"": ""\/Date(1388444400000+0100)\/"", ""stichtag_deCH"": ""15.03.2015"", ""stichtag_jsondate"": ""2015-06-22T18:02:00.725Z"", ""gb_uid"":""2ba62b36-8b30-457c-8946-82fa452c99fb"",""key2"":""value2"", ""key3"" : 123, ""TSK_DatumVon"" : ""2013-01-01"", ""key5"": true, ""so_uid"": null }"; | |
| DynamicArguments da = new DynamicArguments(json); | |
| foreach(var k in da.Keys) | |
| { | |
| System.Console.WriteLine(k); | |
| } | |
| string testobj = da.GetValue<string>("testobj"); | |
| System.Console.WriteLine(testobj); | |
| DynamicArguments da2 = da.GetValue<DynamicArguments>("testobj"); | |
| string foo = da2.GetValue<string>("foo"); | |
| System.Console.WriteLine(foo); | |
| System.DateTime dt = da.GetValue<System.DateTime>("stichtag"); | |
| System.Console.WriteLine(dt); | |
| // Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json); | |
| System.Collections.Generic.Dictionary<string, dynamic> values = | |
| Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, dynamic>>(json); | |
| foreach (System.Collections.Generic.KeyValuePair<string, dynamic> kvp in values) | |
| { | |
| System.Console.WriteLine(kvp.Key); | |
| System.Console.WriteLine(kvp.Value.GetType()); | |
| } | |
| System.Console.WriteLine(" --- Press any key to continue --- "); | |
| } | |
| } | |
| } |
DynamicJSONserializer的更多相关文章
随机推荐
- zend studio里面这块注释是用什么快捷键按出来的?
写完类或函数(注意必须写完,不然出现的信息会不完整)后,在其上方空行输入/**,然后回车 /** * * @param string $a * @param string $b * @param st ...
- linux中怎么进入root用户
如果你是第一次使用root用户,需要设置root用户密码:passwd root 根据提示输入然后切换到root用户:su root回车输入密码 回车
- php索引数组转成关联数组
foreach($revenue_data as $k3=>$v3){ $temps[$v3['_id']['date']]= array( '_id'=>$v3['_id'], 'tot ...
- 关于cookie与session的理解
服务器端并不能捕获客户端的浏览器关闭事件,因此你关闭浏览器以后,服务器端那个Session还是存在的,要超时以后才被回收,启动一个新的浏览器会启动一个新的session,所以他不认你了session永 ...
- php后台数组foreach嵌套循环
<?php foreach($list as $key=>$val){ ?> <tr class="over_odd"> <td align=& ...
- memcached使用文档
使用memcached进行内存缓存 通常的网页缓存方式有动态缓存和静态缓存等几种,在ASP.NET中已经可以实现对页面局部进行缓 存,而使用memcached的缓存比ASP.NET的局部缓存更加灵活, ...
- 移动端布局,C3新增属性
<html5拖拽> 1.给元素设置 draggable="true" 属性,这个元素就可以被拖拽了 <拖拽元素事件> 2.ondragstart 拖拽前触发 ...
- php实现人员的权限管理
权限是指不同的人员登录以后会用不同的页面. 一.想好这个权限是什么? 肯定要有用户表.还有用户所用的角色.然后就是权限功能表:可是在这里面有关联也就 是会另外有两张相互关联的表,这样也就是5张表 在数 ...
- VB6之调整任务栏按钮的位置
好无聊,睡前一更~ XP的任务栏没办法像win7那样随意拖动交换顺序,偶觉不爽,遂写程序搞之.这个不算什么新东西,参考了很多别人写的东东. 程序启动后,会在右下角托盘区显示钢铁侠的图标.右键击之,可选 ...
- rownumber和rowid伪劣用法
select rownum ,deptno,dname loc from dept; select deptno,dname,loc from dept where rownum=1; select ...