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的更多相关文章
随机推荐
- Redis的安装与使用(单节点)
IP:192.168.4.111 环境:CentOS 6.6 Redis版本:redis-3.0 (考虑到Redis3.0在集群和性能提升方面的特性,rc版为正式版的候选版,而且很快就出正式版) 安装 ...
- 第一章(认识jQuery)
1.3.2编写简单的jQuery代码 ①$是jQuery的简写 ②$("#foo") = $("#foo") ③$.ajax = jQuery.ajax ④ ...
- Linux程序设计之shell程序设计
看了<linux程序设计(第4版)>(作者:Neil Matthew ,Richard Stones ,陈建 ,宋健建译).做个笔记,以备后面查看. 首先,清楚几个概念. shell编程属 ...
- dict-字典功能介绍
叨逼叨:#字典 可变类型 意思就是修改的是自己本身#可变类型,当修改后,内存里的值也会对应着修改#不可变类型,当修改后,会在内存里开启一块新的空间,放新的值#1.清空 # name = { # 'na ...
- Java之异常处理,日期处理
Java异常处理 异常:异常就是Java程序在运行过程中出现的错误. 异常由来:问题也是现实生活中一个具体事务,也可以通过java 的类的形式进行描述,并封装成对象.其实就是Java对不正常情况进行描 ...
- 有点难度的JS面试题
自己总结了一些JS面试题 希望能够帮助正在找工作的程序猿(●´∀`●) 1.js 实现一个函数对javascript中json 对象进行克隆 var oldObject ="sdf" ...
- Java设计模式之模板方法设计模式(银行计息案例)
不知道为什么,这几天对Java中的设计模式非常感兴趣,恰巧呢这几天公司的开发任务还不算太多,趁着有时间昨天又把模板方法模式深入学习了一下,做了一个客户在不同银行计息的小案例,感触颇深,今天给各位分享一 ...
- (转)css内边距与外边距的区别,精辟啊
css内边距与外边距的区别 (2012-05-02 13:54:54) 转载▼ 标签: 杂谈 分类: css 本文也是网上看了后收藏的,忘了原地址(以后收藏文章得注意) 你真的了解margin吗?你知 ...
- VMware workstation批量创建虚拟机和自动化安装操作系统(二)
一. 简述 在上一篇<VMware workstation批量创建虚拟机和自动化安装操作系统(一)>中,主要介绍了VMware workstation自定义创建虚拟机的过程,和一些其他的有 ...
- ORACLE - 用户和角色的权限管理
在ORACLE中,创建用户后需要授权才能使用. 一.用户管理 1. 用户和角色信息查询 --查询所有用户 SQL> select * from dba_users; --经授予的用户或角色的系统 ...