Advertisement
Guest User

JSON.cs diff

a guest
Apr 16th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 16.54 KB | None | 0 0
  1. diff --git "a/C:\\Users\\user\\AppData\\Local\\Temp\\JSO9D79.tmp\\JSON-901a0de-left.cs" "b/C:\\Mount\\B\\SourceCode\\_json\\fastjson\\fastJSON\\JSON.cs"
  2. index 8ea0a9d..4a27678 100644
  3. --- "a/C:\\Users\\user\\AppData\\Local\\Temp\\JSO9D79.tmp\\JSON-901a0de-left.cs"
  4. +++ "b/C:\\Mount\\B\\SourceCode\\_json\\fastjson\\fastJSON\\JSON.cs"
  5. @@ -1,4 +1,5 @@
  6. -using System;
  7. +#define MY_PROP_INFO_USE_ENUMS
  8. +using System;
  9.  using System.Collections;
  10.  using System.Collections.Generic;
  11.  #if !SILVERLIGHT
  12. @@ -68,6 +69,7 @@ namespace fastJSON
  13.  
  14.      public sealed class JSON
  15.      {
  16. +       //static JSON() { Console.WriteLine("myPropInfo: {0}", System.Runtime.InteropServices.Marshal.SizeOf(typeof(myPropInfo)).ToString("X8")); }
  17.          //public readonly static JSON Instance = new JSON();
  18.          [ThreadStatic]
  19.          private static JSON _instance;
  20. @@ -229,39 +231,95 @@ namespace fastJSON
  21.  
  22.          #region [   JSON specific reflection   ]
  23.  
  24. +#if MY_PROP_INFO_USE_ENUMS
  25. +       private enum myPropInfoType
  26. +       {
  27. +           Int,
  28. +           Long,
  29. +           String,
  30. +           Bool,
  31. +           DateTime,
  32. +           Enum,
  33. +           Guid,
  34. +
  35. +           Array,
  36. +           ByteArray,
  37. +           Dictionary,
  38. +           StringDictionary,
  39. +#if !SILVERLIGHT
  40. +           Hashtable,
  41. +           DataSet,
  42. +           DataTable,
  43. +#endif
  44. +#if CUSTOMTYPE
  45. +           Custom,
  46. +#endif
  47. +
  48. +           Unknown,
  49. +       };
  50. +       [Flags]
  51. +       private enum myPropInfoFlags
  52. +       {
  53. +           Filled = 1<<0,
  54. +           CanWrite = 1<<1,
  55. +           Class = 1<<2,
  56. +           ValueType = 1<<3,
  57. +           GenericType = 1<<4,
  58. +       };
  59. +#else
  60. +       private static class myPropInfoType
  61. +       {
  62. +           public const int Int = 0;
  63. +           public const int Long = Int + 1;
  64. +           public const int String = Long + 1;
  65. +           public const int Bool = String + 1;
  66. +           public const int DateTime = Bool + 1;
  67. +           public const int Enum = DateTime + 1;
  68. +           public const int Guid = Enum + 1;
  69. +
  70. +           public const int Array = Guid + 1;
  71. +           public const int ByteArray = Array + 1;
  72. +           public const int Dictionary = ByteArray + 1;
  73. +           public const int StringDictionary = Dictionary + 1;
  74. +//#if !SILVERLIGHT
  75. +           public const int Hashtable = StringDictionary + 1;
  76. +           public const int DataSet = Hashtable + 1;
  77. +           public const int DataTable = DataSet + 1;
  78. +//#endif
  79. +//#if CUSTOMTYPE
  80. +           public const int Custom = DataTable + 1;
  81. +//#endif
  82. +
  83. +           public const int Unknown = Custom + 1;
  84. +       };
  85. +       private static class myPropInfoFlags
  86. +       {
  87. +           public const int Filled = 1<<0;
  88. +           public const int CanWrite = 1<<1;
  89. +           public const int Class = 1<<2;
  90. +           public const int ValueType = 1<<3;
  91. +           public const int GenericType = 1<<4;
  92. +       };
  93. +#endif
  94.          private struct myPropInfo
  95.          {
  96. -            public bool filled;
  97.              public Type pt;
  98.              public Type bt;
  99.              public Type changeType;
  100. -            public bool isDictionary;
  101. -            public bool isValueType;
  102. -            public bool isGenericType;
  103. -            public bool isArray;
  104. -            public bool isByteArray;
  105. -            public bool isGuid;
  106. -#if !SILVERLIGHT
  107. -            public bool isDataSet;
  108. -            public bool isDataTable;
  109. -            public bool isHashtable;
  110. -#endif
  111. -            public Reflection.GenericSetter setter;
  112. -            public bool isEnum;
  113. -            public bool isDateTime;
  114. -            public Type[] GenericTypes;
  115. -            public bool isInt;
  116. -            public bool isLong;
  117. -            public bool isString;
  118. -            public bool isBool;
  119. -            public bool isClass;
  120. -            public Reflection.GenericGetter getter;
  121. -            public bool isStringDictionary;
  122. -            public string Name;
  123. -#if CUSTOMTYPE
  124. -            public bool isCustomType;
  125. +           public Reflection.GenericSetter setter;
  126. +           public Reflection.GenericGetter getter;
  127. +           public Type[] GenericTypes;
  128. +           public string Name;
  129. +#if MY_PROP_INFO_USE_ENUMS
  130. +           public myPropInfoType Type;
  131. +           public myPropInfoFlags Flags;
  132. +#else
  133. +           public int Type, Flags;
  134.  #endif
  135. -            public bool CanWrite;
  136. +
  137. +           public bool IsClass { get { return (Flags & myPropInfoFlags.Class) != 0; } }
  138. +           public bool IsValueType { get { return (Flags & myPropInfoFlags.ValueType) != 0; } }
  139. +           public bool IsGenericType { get { return (Flags & myPropInfoFlags.GenericType) != 0; } }
  140.          }
  141.  
  142.          SafeDictionary<string, SafeDictionary<string, myPropInfo>> _propertycache = new SafeDictionary<string, SafeDictionary<string, myPropInfo>>();
  143. @@ -279,7 +337,7 @@ namespace fastJSON
  144.                  foreach (PropertyInfo p in pr)
  145.                  {
  146.                      myPropInfo d = CreateMyProp(p.PropertyType, p.Name);
  147. -                    d.CanWrite = p.CanWrite;
  148. +                    d.Flags |= myPropInfoFlags.CanWrite;
  149.                      d.setter = Reflection.CreateSetMethod(type, p);
  150.                      d.getter = Reflection.CreateGetMethod(type, p);
  151.                      sd.Add(p.Name, d);
  152. @@ -301,44 +359,55 @@ namespace fastJSON
  153.          private myPropInfo CreateMyProp(Type t, string name)
  154.          {
  155.              myPropInfo d = new myPropInfo();
  156. -            d.filled = true;
  157. -            d.CanWrite = true;
  158. -            d.pt = t;
  159. -            d.Name = name;
  160. -            d.isDictionary = t.Name.Contains("Dictionary");
  161. -            if (d.isDictionary)
  162. -                d.GenericTypes = t.GetGenericArguments();
  163. -            d.isValueType = t.IsValueType;
  164. -            d.isGenericType = t.IsGenericType;
  165. -            d.isArray = t.IsArray;
  166. -            if (d.isArray)
  167. -                d.bt = t.GetElementType();
  168. -            if (d.isGenericType)
  169. -                d.bt = t.GetGenericArguments()[0];
  170. -            d.isByteArray = t == typeof(byte[]);
  171. -            d.isGuid = (t == typeof(Guid) || t == typeof(Guid?));
  172. +           var d_type = myPropInfoType.Unknown;
  173. +           var d_flags = myPropInfoFlags.Filled | myPropInfoFlags.CanWrite;
  174. +
  175. +                if (t == typeof(int) || t == typeof(int?))             d_type = myPropInfoType.Int;
  176. +           else if (t == typeof(long) || t == typeof(long?))           d_type = myPropInfoType.Long;
  177. +           else if (t == typeof(string))                               d_type = myPropInfoType.String;
  178. +           else if (t == typeof(bool) || t == typeof(bool?))           d_type = myPropInfoType.Bool;
  179. +           else if (t == typeof(DateTime) || t == typeof(DateTime?))   d_type = myPropInfoType.DateTime;
  180. +           else if (t.IsEnum)                                          d_type = myPropInfoType.Enum;
  181. +           else if (t == typeof(Guid) || t == typeof(Guid?))           d_type = myPropInfoType.Guid;
  182. +           else if (t.IsArray)
  183. +           {
  184. +               d.bt = t.GetElementType();
  185. +               if (t == typeof(byte[]))
  186. +                   d_type = myPropInfoType.ByteArray;
  187. +               else
  188. +                   d_type = myPropInfoType.Array;
  189. +           }
  190. +           else if (t.Name.Contains("Dictionary"))
  191. +           {
  192. +               d.GenericTypes = t.GetGenericArguments();
  193. +               if(d.GenericTypes.Length > 0 && d.GenericTypes[0] == typeof(string))
  194. +                   d_type = myPropInfoType.StringDictionary;
  195. +               else
  196. +                   d_type = myPropInfoType.Dictionary;
  197. +           }
  198.  #if !SILVERLIGHT
  199. -            d.isHashtable = t == typeof(Hashtable);
  200. -            d.isDataSet = t == typeof(DataSet);
  201. -            d.isDataTable = t == typeof(DataTable);
  202. +           else if (t == typeof(Hashtable))                            d_type = myPropInfoType.Hashtable;
  203. +           else if (t == typeof(DataSet))                              d_type = myPropInfoType.DataSet;
  204. +           else if (t == typeof(DataTable))                            d_type = myPropInfoType.DataTable;
  205. +#endif
  206. +#if CUSTOMTYPE
  207. +            else if (IsTypeRegistered(t))                              d_type = myPropInfoType.Custom;
  208.  #endif
  209.  
  210. -            d.changeType = GetChangeType(t);
  211. -            d.isEnum = t.IsEnum;
  212. -            d.isDateTime = t == typeof(DateTime) || t == typeof(DateTime?);
  213. -            d.isInt = t == typeof(int) || t == typeof(int?);
  214. -            d.isLong = t == typeof(long) || t == typeof(long?);
  215. -            d.isString = t == typeof(string);
  216. -            d.isBool = t == typeof(bool) || t == typeof(bool?);
  217. -            d.isClass = t.IsClass;
  218. +           if (t.IsClass)          d_flags |= myPropInfoFlags.Class;
  219. +           if (t.IsValueType)      d_flags |= myPropInfoFlags.ValueType;
  220. +           if (t.IsGenericType)
  221. +           {
  222. +               d_flags |= myPropInfoFlags.GenericType;
  223. +               d.bt = t.GetGenericArguments()[0];
  224. +           }
  225.  
  226. -            if (d.isDictionary && d.GenericTypes.Length > 0 && d.GenericTypes[0] == typeof(string))
  227. -                d.isStringDictionary = true;
  228. +            d.pt = t;
  229. +            d.Name = name;
  230. +            d.changeType = GetChangeType(t);
  231. +           d.Type = d_type;
  232. +           d.Flags = d_flags;
  233.  
  234. -#if CUSTOMTYPE
  235. -            if (IsTypeRegistered(t))
  236. -                d.isCustomType = true;
  237. -#endif
  238.              return d;
  239.          }
  240.  
  241. @@ -469,74 +538,59 @@ namespace fastJSON
  242.                  myPropInfo pi;
  243.                  if (props.TryGetValue(name, out pi) == false)
  244.                      continue;
  245. -                if (pi.filled && pi.CanWrite)
  246. +                if ((pi.Flags & (myPropInfoFlags.Filled|myPropInfoFlags.CanWrite)) != 0)
  247.                  {
  248.                      object v = d[name];
  249.  
  250.                      if (v != null)
  251.                      {
  252. -                        object oset = null;
  253. -
  254. -                        if (pi.isInt)
  255. -                            oset = (int)((long)v);
  256. -#if CUSTOMTYPE
  257. -                        else if (pi.isCustomType)
  258. -                            oset = CreateCustom((string)v, pi.pt);
  259. -#endif
  260. -                        else if (pi.isLong)
  261. -                            oset = (long)v;
  262. -
  263. -                        else if (pi.isString)
  264. -                            oset = (string)v;
  265. -
  266. -                        else if (pi.isBool)
  267. -                            oset = (bool)v;
  268. -
  269. -                        else if (pi.isGenericType && pi.isValueType == false && pi.isDictionary == false && v is List<object>)
  270. -                            oset = CreateGenericList((List<object>)v, pi.pt, pi.bt, globaltypes);
  271. -
  272. -                        else if (pi.isByteArray)
  273. -                            oset = Convert.FromBase64String((string)v);
  274. -
  275. -                        else if (pi.isArray && pi.isValueType == false)
  276. -                            oset = CreateArray((List<object>)v, pi.pt, pi.bt, globaltypes);
  277. -
  278. -                        else if (pi.isGuid)
  279. -                            oset = CreateGuid((string)v);
  280. +                       object oset = null;
  281. +
  282. +                       switch (pi.Type)
  283. +                       {
  284. +                           case myPropInfoType.Int: oset = (int)((long)v); break;
  285. +                           case myPropInfoType.Long: oset = (long)v; break;
  286. +                           case myPropInfoType.String: oset = (string)v; break;
  287. +                           case myPropInfoType.Bool: oset = (bool)v; break;
  288. +                           case myPropInfoType.DateTime: oset = CreateDateTime((string)v); break;
  289. +                           case myPropInfoType.Enum: oset = CreateEnum(pi.pt, (string)v); break;
  290. +                           case myPropInfoType.Guid: oset = CreateGuid((string)v); break;
  291. +
  292. +                           case myPropInfoType.Array:
  293. +                               if(!pi.IsValueType)
  294. +                                   oset = CreateArray((List<object>)v, pi.pt, pi.bt, globaltypes);
  295. +                               // what about 'else'?
  296. +                               break;
  297. +                           case myPropInfoType.ByteArray: oset = Convert.FromBase64String((string)v); break;
  298.  #if !SILVERLIGHT
  299. -                        else if (pi.isDataSet)
  300. -                            oset = CreateDataset((Dictionary<string, object>)v, globaltypes);
  301. -
  302. -                        else if (pi.isDataTable)
  303. -                            oset = this.CreateDataTable((Dictionary<string, object>)v, globaltypes);
  304. +                           case myPropInfoType.DataSet: oset = CreateDataset((Dictionary<string, object>)v, globaltypes); break;
  305. +                           case myPropInfoType.DataTable: oset = this.CreateDataTable((Dictionary<string, object>)v, globaltypes); break;
  306. +                           case myPropInfoType.Hashtable: // same case as Dictionary
  307.  #endif
  308. -
  309. -                        else if (pi.isStringDictionary)
  310. -                            oset = CreateStringKeyDictionary((Dictionary<string, object>)v, pi.pt, pi.GenericTypes, globaltypes);
  311. -#if !SILVERLIGHT
  312. -                        else if (pi.isDictionary || pi.isHashtable)
  313. -#else
  314. -                        else if (pi.isDictionary)
  315. +                           case myPropInfoType.Dictionary: oset = CreateDictionary((List<object>)v, pi.pt, pi.GenericTypes, globaltypes); break;
  316. +                           case myPropInfoType.StringDictionary: oset = CreateStringKeyDictionary((Dictionary<string, object>)v, pi.pt, pi.GenericTypes, globaltypes); break;
  317. +#if CUSTOMTYPE
  318. +                           case myPropInfoType.Custom: oset = CreateCustom((string)v, pi.pt); break;
  319.  #endif
  320. -                            oset = CreateDictionary((List<object>)v, pi.pt, pi.GenericTypes, globaltypes);
  321. -
  322. -                        else if (pi.isEnum)
  323. -                            oset = CreateEnum(pi.pt, (string)v);
  324. -
  325. -                        else if (pi.isDateTime)
  326. -                            oset = CreateDateTime((string)v);
  327. +                           default:
  328. +                               {
  329. +                                   if (pi.IsGenericType && pi.IsValueType == false && v is List<object>)
  330. +                                       oset = CreateGenericList((List<object>)v, pi.pt, pi.bt, globaltypes);
  331.  
  332. -                        else if (pi.isClass && v is Dictionary<string, object>)
  333. -                            oset = ParseDictionary((Dictionary<string, object>)v, globaltypes, pi.pt, pi.getter(o));
  334. +                                   else if (pi.IsClass && v is Dictionary<string, object>)
  335. +                                       oset = ParseDictionary((Dictionary<string, object>)v, globaltypes, pi.pt, pi.getter(o));
  336.  
  337. -                        else if (pi.isValueType)
  338. -                            oset = ChangeType(v, pi.changeType);
  339. +                                   else if (v is List<object>)
  340. +                                       oset = CreateArray((List<object>)v, pi.pt, typeof(object), globaltypes);
  341.  
  342. -                        else if (v is List<object>)
  343. -                            oset = CreateArray((List<object>)v, pi.pt, typeof(object), globaltypes);
  344. +                                   else if (pi.IsValueType)
  345. +                                       oset = ChangeType(v, pi.changeType);
  346.  
  347. -                        else
  348. -                            oset = v;
  349. +                                   else
  350. +                                       oset = v;
  351. +                               }
  352. +                               break;
  353. +                       }
  354.  
  355.                          o = pi.setter(o, oset);
  356.                      }
  357. @@ -566,6 +620,72 @@ namespace fastJSON
  358.              }
  359.          }
  360.  
  361. +       static int CreateInteger(out int num, string s, int index, int count)
  362. +       {
  363. +           num = 0;
  364. +           bool neg = false;
  365. +           for (int x = 0; x < count; x++, index++)
  366. +           {
  367. +               char cc = s[index];
  368. +
  369. +               if (cc == '-')
  370. +                   neg = true;
  371. +               else if (cc == '+')
  372. +                   neg = false;
  373. +               else
  374. +               {
  375. +                   num *= 10;
  376. +                   num += (int)(cc - '0');
  377. +               }
  378. +           }
  379. +           if(neg) num = -num;
  380. +
  381. +           return num;
  382. +       }
  383. +       static long CreateInteger(out long num, string s, int index, int count)
  384. +       {
  385. +           num = 0;
  386. +           bool neg = false;
  387. +           for (int x = 0; x < count; x++, index++)
  388. +           {
  389. +               char cc = s[index];
  390. +
  391. +               if (cc == '-')
  392. +                   neg = true;
  393. +               else if (cc == '+')
  394. +                   neg = false;
  395. +               else
  396. +               {
  397. +                   num *= 10;
  398. +                   num += (int)(cc - '0');
  399. +               }
  400. +           }
  401. +           if (neg) num = -num;
  402. +
  403. +           return num;
  404. +       }
  405. +       internal static long CreateInteger(out long num, char[] s, int index, int count)
  406. +       {
  407. +           num = 0;
  408. +           bool neg = false;
  409. +           for (int x = 0; x < count; x++, index++)
  410. +           {
  411. +               char cc = s[index];
  412. +
  413. +               if (cc == '-')
  414. +                   neg = true;
  415. +               else if (cc == '+')
  416. +                   neg = false;
  417. +               else
  418. +               {
  419. +                   num *= 10;
  420. +                   num += (int)(cc - '0');
  421. +               }
  422. +           }
  423. +           if (neg) num = -num;
  424. +
  425. +           return num;
  426. +       }
  427.          private long CreateLong(string s)
  428.          {
  429.              long num = 0;
  430. @@ -609,14 +729,21 @@ namespace fastJSON
  431.              bool utc = false;
  432.              //                   0123456789012345678
  433.              // datetime format = yyyy-MM-dd HH:mm:ss
  434. -            int year = (int)CreateLong(value.Substring(0, 4));
  435. -            int month = (int)CreateLong(value.Substring(5, 2));
  436. -            int day = (int)CreateLong(value.Substring(8, 2));
  437. -            int hour = (int)CreateLong(value.Substring(11, 2));
  438. -            int min = (int)CreateLong(value.Substring(14, 2));
  439. -            int sec = (int)CreateLong(value.Substring(17, 2));
  440. -
  441. -            if (value.EndsWith("Z"))
  442. +            int year;// = (int)CreateLong(value.Substring(0, 4));
  443. +            int month;// = (int)CreateLong(value.Substring(5, 2));
  444. +            int day;// = (int)CreateLong(value.Substring(8, 2));
  445. +            int hour;// = (int)CreateLong(value.Substring(11, 2));
  446. +            int min;// = (int)CreateLong(value.Substring(14, 2));
  447. +            int sec;// = (int)CreateLong(value.Substring(17, 2));
  448. +           CreateInteger(out year, value, 0, 4);
  449. +           CreateInteger(out month, value, 5, 2);
  450. +           CreateInteger(out day, value, 8, 2);
  451. +           CreateInteger(out hour, value, 11, 2);
  452. +           CreateInteger(out min, value, 14, 2);
  453. +           CreateInteger(out sec, value, 17, 2);
  454. +
  455. +            //if (value.EndsWith("Z"))
  456. +           if (value[value.Length-1] == 'Z')
  457.                  utc = true;
  458.  
  459.              if (_params.UseUTCDateTime == false && utc == false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement