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的更多相关文章
随机推荐
- 基于TFS的.net技术路线的云平台DevOps实践
DevOps是近几年非常流行的系统研发管理模式,很多公司都或多或少在践行DevOps.那么,今天就说说特来电云平台在DevOps方面的实践吧. 说DevOps,不得不说DevOps的具体含义.那么,D ...
- My new life
第一次开始写博客,也是在学长的建议下想要正式的写的.有点小激动,这篇博客标志着一个新的开始,它将记录下我学习编程的生活,也象征着我将向着自己渴望的方向发展.不过这篇博客就真的是一篇随笔哈哈. 希望我的 ...
- 关于Client_Abort_Exception异常的分析和解决
1.什么情况下会出现“ClientAbortException: java.net.socketException: Broken pipe”? 答:客户端非正常(标准握手协议)退出连接,体现在h ...
- 3.ubuntu如何安装搜狗输入法
1.http://blog.csdn.net/qq_21792169/article/details/53152700 2.http://jingyan.baidu.com/article/54b6b ...
- ReactiveObjC使用
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Menlo; color: #78492a; background-color: #fffff ...
- Loadrunner--自动关联和手动关联
2017-06-09 15:32:45个人也属于刚刚开始学习,有什么不对的地方敬请指导:qq:389791447 一开始的时候,准备去学习怎么去关联.一时也毛不着头脑,就在网上找了一些视频看,有的人说 ...
- 编写高质量代码改善程序的157个建议:第87个建议之区分WPF和WinForm的线程模型
今天有时间了,继续<编写高质量代码改善程序的157个建议>的阅读,当我阅读到建议87的时候,里面的一些代码示例和文中所说的不一致了,是不是我现在用的是NetFramework 4.0的缘故 ...
- h1b期间回国须知
今天才搞明白几点 1. visa 和 status 是两个不同的东西,status能保证合法在美国.visa能保证合法进入美国 所以,h1b十月份的身份转换时status的转换,如果回国还需要重新办h ...
- 错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server 是无法给远程连接的用户权限问题
错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server 是无法给远程连接的用 ...
- 【ALB技术笔记】基于多线程方式的串行通信接口数据接收案例
基于多线程方式的串行通信接口数据接收案例 广东职业技术技术学院 欧浩源 1.案例背景 在本博客的<[CC2530入门教程-06]CC2530的ADC工作原理与应用>中实现了电压数据采集的 ...