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的更多相关文章
随机推荐
- [leetcode-617-Merge Two Binary Trees]
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- 【Android Developers Training】 96. 运行一个同步适配器
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- webpackage 2.x 使用
webpackage 2.x 使用 安装---(在项目目录下) //1.初始化npm的配置(添加package.json) npm init //2.安装 webpackage npm install ...
- Ext viewport的渲染
Ext viewport的渲染 1.在app.js里创建 Ext.application({ name: 'MySecurity', extend: 'MySecurity.Application', ...
- 最新的极光推送服务器端代码(java服务器后台向手机端自定义推送消息)
一共两个类 一个Jdpush 一个JpushClientUtil 代码如下 注解都写的很清楚 package com.sm.common.ajpush; import org.slf4j.Log ...
- .net入门 - Get Started with .NET
阅读原文 有很多种方式去开始使用.net.因为.net是一个巨大的平台,在这个文档里面有很多文章,告诉你如何从不同的角度去开始使用.net. 使用.NET的语言入门 C#入门文章和C#教程提供了以C# ...
- 使用three.js实现机器人手臂的运动效果
Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包括了摄影机.光影.材质等各种对象.你可以在它的主页上看到许多精彩的演示.不过,这款引擎目前还处在比较不成熟的开发阶段 ...
- docker搭建zabbix
本次使用docker搭建zabbix的组合是mysql+docker+zabix-server 1 先安装数据库mysql docker run --name zabbix-mysql-server ...
- pgsql 递归查询 分页
--向下查询 WITH RECURSIVE res AS ( union ALL SELECT t_tree.* FROM t_tree, res WHERE t_tree.pid = res.id ...
- hihocoder 1050 树中的最长路(动态规划,dfs搜索)
hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...