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的更多相关文章
随机推荐
- 几种MQ消息队列对比与消息队列之间的通信问题
消息队列 开发语言 协议支持 设计模式 持久化支持 事务支持 负载均衡支持 功能特点 缺点 RabbitMQ Erlang AMQP,XMPP,SMTP,STOMP 代理(Broker)模式(消息在发 ...
- 【LeetCode】205. Isomorphic Strings
题目: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the c ...
- android app调试没问题,但打包签名的apk,运行时出现闪退怎么办?
在用Eclipse编写Android app时,有时调试时没有问题,但一经打包签名,运行就出现闪退,还报错说找不到某某类.一开始以为是混淆导致的,后来我没有混淆竟然也还是这个问题.无奈只得网上寻找解决 ...
- laravel怎么创建一个简单的blog
主要功能实现:点击标题跳转 第一步:创建路由: Route::get('/articles','ArticlesController@index'); Route::get('/articles/{i ...
- Linux10分钟入门
最近打算考红帽认证,将自己学习到的和工作中常用的一些命令进行总结,供初学者和一定基础的参考. 想系统性学习的话,还是建议看书(鸟哥的Linux私房菜)和看视频(基础版,推荐马哥和老男孩,不推荐**** ...
- 原生的Ajax的实现
<script type="text/javascript"> // Ajax固定的模版 // 第一步:创建xhr对象,使用new关键字来调用内置的构造函数 var x ...
- orcle 索引的使用
2.4.3.1. 索引的概念 数据库中的索引与书籍中的索引类似,在一本书中,利用索引可以快速查找所需信息, 无须阅读整本书.在数据库中,索引使数据库程序无须对整个表进行扫描, 就可以在其中找到所需数据 ...
- 解决window7 x64位Anaconda启动报错:AttributeError: '_NamespacePath' object has no attribute 'sort'
最近论文需要用到python做数据分析,python语法简单,但是Windows下安装第三方包恶心的要命,statsmodels用pip死活安装不上,网上查了说包相互依赖windows下的pip不能下 ...
- Hibernate框架 主配置文件(Hibernate.cfg.xml) 映射配置 说明
Hibernate.cfg.xml 主配置文件中主要配置:数据库连接信息.其他参数.映射信息! 常用配置查看源码: hibernate-distribution-3.6.0.Final\project ...
- JS实现为控件添加倒计时功能
一.概述 在有些报表需求中,需要为控件添加倒计时功能,限制到某一个时间点后能进行一项操作或不能进行某项操作,比如查询,导出功能等等,又需要人性化地显示还有多少时间,即倒计时功能,比如下图中我们限制这个 ...