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的更多相关文章
随机推荐
- 页面加载的时候自动的执行js代码
<script> window.onload=MyAutoRun; function MyAutoRun(){ alert("函数自动执行哦!"); } </sc ...
- 在win7下如何设置计划任务每一分钟执行一次
- empty 和isset的差别
都可以判定一个变量是否为空:都返回boolean类型,即true或false.isset()用来检测变量是否设置,只能用于变量,因为传递任何其它参数都将造成解析错误.若想检测常量是否已设置,可使用 d ...
- Java总结之线程(1)
java线程是很重要的一项,所以作为java程序员必须要掌握的. 理解java线程必须先理解线程在java中的生命周期.. 1.java线程生命周期 1.new 创建一个线程 java中创建线程有 ...
- 【Android Developers Training】 67. 响应触摸事件
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Java程序性能优化-读书笔记(一) 单例模式
单例模式: 目的: 确保系统中一个类只产生一个实例. 好处: 1.对于频繁使用的对象,可以省略创建对象所花费的时间,这对于那些重量级对象而言,是非常可观的一笔系统开销. 2.由于new操作的次数减少, ...
- Spring 极速集成注解 redis 实录
Redis 做为基于内存的 Key-Value 数据库,用来做缓存服务器性价比相当高. 官方推出的面向 Java 的 Client Jedis,提供了很多接口和方法,可以让 Java 操作使用 Red ...
- C实现dos图文菜单程序实例
前言 公司一台服务器是novell环境,文件管理是基于dos6.22的,客户端启动需要一个图文菜单. 实现 编程环境:汉化版TC2.0 菜单基本功能:显示提示项.显示dbf中的行情信息. ...
- 【ALB学习笔记】基于.NET环境的高频RFID卡读写设备的基本操作案例
基于.NET环境的高频RFID卡读写设备的基本操作案例 广东职业技术学院 欧浩源 1.引言 RFID高频卡在我们的日常生活中随处可见,是物联网应用中不可或缺的一个重要部分,也是全国职业院校技能大赛& ...
- IDEA使用01 创建java项目、创建web项目
注意:本教程使用的开发环境是:(专业版) 1 创建javaSE项目 1.1 file -> new -> project 注意:如果是第一次使用,就需要配置 project SDK , ...