PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/Json/JSONParser.cs

https://bitbucket.org/digitalizarte/vici.core
C# | 287 lines | 194 code | 69 blank | 24 comment | 65 complexity | 3a2f5009a9f57848a8e536b23d74fe13 MD5 | raw file
  1. #region License
  2. //=============================================================================
  3. // Vici Core - Productivity Library for .NET 3.5
  4. //
  5. // Copyright (c) 2008-2011 Philippe Leybaert
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. // IN THE SOFTWARE.
  24. //=============================================================================
  25. #endregion
  26. using System;
  27. using System.Collections;
  28. using System.Collections.Generic;
  29. using System.Globalization;
  30. using System.Reflection;
  31. using Vici.Core.Parser;
  32. #if WINDOWS_PHONE || SILVERLIGHT
  33. using Vici.Core.CompatibilityLayer;
  34. #endif
  35. namespace Vici.Core.Json
  36. {
  37. public class JsonParser
  38. {
  39. private Token[] _tokens;
  40. private int _currentToken;
  41. static JsonParser()
  42. {
  43. }
  44. public T Parse<T>(string json) where T:class, new()
  45. {
  46. Tokenize(json);
  47. _currentToken = 0;
  48. return (T) ParseObject(typeof(T));
  49. }
  50. public object Parse(string json)
  51. {
  52. Tokenize(json);
  53. _currentToken = 0;
  54. return ParseObject(typeof (object));
  55. }
  56. private void Tokenize(string json)
  57. {
  58. Tokenizer tokenizer = new JSONTokenizer();
  59. List<Token> tokens = new List<Token>(tokenizer.Tokenize(json, TokenPosition.Unknown));
  60. tokens.RemoveAll(token => token.TokenMatcher is WhiteSpaceMatcher);
  61. _tokens = tokens.ToArray();
  62. }
  63. private Token CurrentToken
  64. {
  65. get { return _tokens[_currentToken]; }
  66. }
  67. private object ParseObject(Type objectType)
  68. {
  69. if (CurrentToken.TokenMatcher is NullTokenMatcher)
  70. {
  71. _currentToken++;
  72. return null;
  73. }
  74. object obj;
  75. bool isDictionary = false;
  76. if (objectType == typeof(object))
  77. {
  78. obj = new Dictionary<string, object>();
  79. isDictionary = true;
  80. }
  81. else
  82. obj = Activator.CreateInstance(objectType);
  83. if (!(CurrentToken.TokenMatcher is ObjectStartTokenMatcher))
  84. throw new Exception("Expected {");
  85. _currentToken++;
  86. for (; ; )
  87. {
  88. if (!(CurrentToken.TokenMatcher is StringTokenMatcher))
  89. throw new Exception("Expected property name");
  90. string propName = CurrentToken.Text.Substring(1, CurrentToken.Text.Length - 2);
  91. _currentToken++;
  92. if (!(CurrentToken.TokenMatcher is ColonTokenMatcher))
  93. throw new Exception("Expected colon");
  94. _currentToken++;
  95. if (!isDictionary)
  96. {
  97. PropertyInfo property = objectType.GetProperty(propName);
  98. FieldInfo field = objectType.GetField(propName);
  99. if (property != null || field != null)
  100. {
  101. Type fieldType = (property != null) ? property.PropertyType : field.FieldType;
  102. object fieldvalue = ParseValue(fieldType);
  103. if (property != null)
  104. property.SetValue(obj, fieldvalue, null);
  105. else
  106. field.SetValue(obj, fieldvalue);
  107. }
  108. else
  109. {
  110. ParseValue(typeof (object));
  111. }
  112. }
  113. else
  114. {
  115. ((Dictionary<string, object>) obj)[propName] = ParseValue(typeof (object));
  116. }
  117. if (!(CurrentToken.TokenMatcher is CommaTokenMatcher))
  118. break;
  119. _currentToken++;
  120. }
  121. if (!(CurrentToken.TokenMatcher is ObjectEndTokenMatcher))
  122. throw new Exception("Expected }");
  123. _currentToken++;
  124. return obj;
  125. }
  126. private static bool IsArray(Type type)
  127. {
  128. if (typeof(IList).IsAssignableFrom(type))
  129. return true;
  130. Type[] interfaces = type.FindInterfaces(
  131. (t, criteria) => (t.IsGenericType && t.GetGenericTypeDefinition() == typeof (IList<>))
  132. ,
  133. null);
  134. return interfaces.Length > 0;
  135. }
  136. private object ParseValue(Type type)
  137. {
  138. if (type == typeof(object))
  139. {
  140. if (CurrentToken.TokenMatcher is StringTokenMatcher)
  141. type = typeof(string);
  142. else if (CurrentToken.TokenMatcher is IntegerTokenMatcher)
  143. type = typeof(Int64);
  144. else if (CurrentToken.TokenMatcher is FloatTokenMatcher)
  145. type = typeof(double);
  146. else if (CurrentToken.TokenMatcher is TrueTokenMatcher || CurrentToken.TokenMatcher is FalseTokenMatcher)
  147. type = typeof(bool);
  148. else if (CurrentToken.TokenMatcher is ArrayStartTokenMatcher)
  149. type = typeof(object[]);
  150. else if (CurrentToken.TokenMatcher is ObjectStartTokenMatcher || CurrentToken.TokenMatcher is NullTokenMatcher)
  151. type = typeof (object);
  152. else
  153. throw new Exception("Unexpected token " + CurrentToken);
  154. }
  155. if (type == typeof(string))
  156. return ParseString();
  157. else if (type == typeof(int) || type == typeof(short) || type == typeof(long) || type == typeof(double) || type == typeof(float) || type == typeof(decimal))
  158. return ParseNumber(type);
  159. else if (IsArray(type))
  160. return ParseArray(type);
  161. else
  162. return ParseObject(type);
  163. }
  164. private object ParseNumber(Type type)
  165. {
  166. if (!(CurrentToken.TokenMatcher is IntegerTokenMatcher) && !(CurrentToken.TokenMatcher is FloatTokenMatcher))
  167. throw new Exception("Number expected");
  168. object n;
  169. if (CurrentToken.TokenMatcher is IntegerTokenMatcher)
  170. {
  171. n = Int64.Parse(CurrentToken.Text, NumberFormatInfo.InvariantInfo);
  172. }
  173. else
  174. {
  175. n = Double.Parse(CurrentToken.Text, NumberFormatInfo.InvariantInfo);
  176. }
  177. _currentToken++;
  178. return Convert.ChangeType(n, type, null);
  179. }
  180. private object ParseString()
  181. {
  182. if (!(CurrentToken.TokenMatcher is StringTokenMatcher))
  183. throw new Exception("Expected string");
  184. string s = CurrentToken.Text.Substring(1,CurrentToken.Text.Length-2);
  185. //TODO: parse dates
  186. _currentToken++;
  187. return s;
  188. }
  189. private object ParseArray(Type type)
  190. {
  191. Type elementType = null;
  192. if (type.IsArray)
  193. {
  194. elementType = type.GetElementType();
  195. }
  196. if (!(CurrentToken.TokenMatcher is ArrayStartTokenMatcher))
  197. throw new Exception("Expected [");
  198. _currentToken++;
  199. List<object> list = new List<object>();
  200. for(;;)
  201. {
  202. if (CurrentToken.TokenMatcher is ArrayEndTokenMatcher)
  203. break;
  204. list.Add(ParseValue(elementType));
  205. if (!(CurrentToken.TokenMatcher is CommaTokenMatcher))
  206. break;
  207. _currentToken++;
  208. }
  209. if (!(CurrentToken.TokenMatcher is ArrayEndTokenMatcher))
  210. throw new Exception("Expected ]");
  211. _currentToken++;
  212. Array array = Array.CreateInstance(elementType, list.Count);
  213. for (int i = 0; i < array.Length; i++ )
  214. array.SetValue(list[i],i);
  215. return array;
  216. }
  217. }
  218. }