2 // JavaScriptSerializer.cs
5 // Konstantin Triger <kostat@mainsoft.com>
7 // (C) 2007 Mainsoft, Inc. http://www.mainsoft.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
33 using Newtonsoft.Json;
35 using System.Collections;
36 using System.Reflection;
37 using Newtonsoft.Json.Utilities;
38 using System.ComponentModel;
39 using System.Configuration;
40 using System.Web.Configuration;
42 namespace System.Web.Script.Serialization
44 public class JavaScriptSerializer
46 internal const string SerializedTypeNameKey = "__type";
48 List<IEnumerable<JavaScriptConverter>> _converterList;
51 JavaScriptTypeResolver _typeResolver;
52 internal static readonly JavaScriptSerializer DefaultSerializer = new JavaScriptSerializer ();
54 public JavaScriptSerializer ()
59 public JavaScriptSerializer (JavaScriptTypeResolver resolver)
61 _typeResolver = resolver;
63 ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection) ConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/jsonSerialization");
64 if (section == null) {
65 _maxJsonLength = 102400;
66 _recursionLimit = 100;
69 _maxJsonLength = section.MaxJsonLength;
70 _recursionLimit = section.RecursionLimit;
74 public int MaxJsonLength {
76 return _maxJsonLength;
79 _maxJsonLength = value;
83 public int RecursionLimit {
85 return _recursionLimit;
88 _recursionLimit = value;
92 internal JavaScriptTypeResolver TypeResolver {
93 get { return _typeResolver; }
96 public T ConvertToType<T> (object obj) {
100 return (T) ConvertToType (typeof (T), obj);
103 internal object ConvertToType (Type type, object obj) {
107 if (obj is IDictionary<string, object>) {
109 obj = EvaluateDictionary ((IDictionary<string, object>) obj);
111 JavaScriptConverter converter = GetConverter (type);
112 if (converter != null)
113 return converter.Deserialize (
114 EvaluateDictionary ((IDictionary<string, object>) obj),
118 return ConvertToObject ((IDictionary<string, object>) obj, type);
120 if (obj is ArrayList)
121 return ConvertToList ((ArrayList) obj, type);
126 Type sourceType = obj.GetType ();
127 if (type.IsAssignableFrom (sourceType))
132 return Enum.Parse (type, (string) obj, true);
134 return Enum.ToObject (type, obj);
136 TypeConverter c = TypeDescriptor.GetConverter (type);
137 if (c.CanConvertFrom (sourceType)) {
139 return c.ConvertFromInvariantString ((string) obj);
141 return c.ConvertFrom (obj);
145 * Take care of the special case whereas in JSON an empty string ("") really means
147 * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
149 if ( (type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable<>)) )
151 string s = obj as String;
152 if (String.IsNullOrEmpty(s))
156 return Convert.ChangeType (obj, type);
159 public T Deserialize<T> (string input) {
160 return ConvertToType<T> (DeserializeObjectInternal(input));
163 static object Evaluate (object value) {
164 return Evaluate (value, false);
167 static object Evaluate (object value, bool convertListToArray) {
168 if (value is IDictionary<string, object>)
169 value = EvaluateDictionary ((IDictionary<string, object>) value, convertListToArray);
170 else if (value is ArrayList)
171 value = EvaluateList ((ArrayList) value, convertListToArray);
175 static object EvaluateList (ArrayList e) {
176 return EvaluateList (e, false);
179 static object EvaluateList (ArrayList e, bool convertListToArray) {
180 ArrayList list = new ArrayList ();
181 foreach (object value in e)
182 list.Add (Evaluate (value, convertListToArray));
184 return convertListToArray ? (object) list.ToArray () : list;
187 static IDictionary<string, object> EvaluateDictionary (IDictionary<string, object> dict) {
188 return EvaluateDictionary (dict, false);
191 static IDictionary<string, object> EvaluateDictionary (IDictionary<string, object> dict, bool convertListToArray) {
192 Dictionary<string, object> d = new Dictionary<string, object> (StringComparer.Ordinal);
193 foreach (KeyValuePair<string, object> entry in dict) {
194 d.Add (entry.Key, Evaluate (entry.Value, convertListToArray));
200 static readonly Type typeofObject = typeof(object);
201 static readonly Type typeofGenList = typeof (List<>);
203 object ConvertToList (ArrayList col, Type type) {
204 Type elementType = null;
205 if (type != null && type.HasElementType)
206 elementType = type.GetElementType ();
209 if (type == null || type.IsArray || typeofObject == type || typeof (ArrayList).IsAssignableFrom (type))
210 list = new ArrayList ();
211 else if (ReflectionUtils.IsInstantiatableType (type))
212 // non-generic typed list
213 list = (IList) Activator.CreateInstance (type, true);
214 else if (ReflectionUtils.IsAssignable (type, typeofGenList)) {
215 if (type.IsGenericType) {
216 Type [] genArgs = type.GetGenericArguments ();
217 elementType = genArgs [0];
219 list = (IList) Activator.CreateInstance (typeofGenList.MakeGenericType (genArgs));
221 list = new ArrayList ();
223 throw new InvalidOperationException (String.Format ("Deserializing list type '{0}' not supported.", type.GetType ().Name));
225 if (list.IsReadOnly) {
230 foreach (object value in col)
231 list.Add (ConvertToType (elementType, value));
233 if (type != null && type.IsArray)
234 list = ((ArrayList) list).ToArray (elementType);
239 object ConvertToObject (IDictionary<string, object> dict, Type type)
241 if (_typeResolver != null) {
242 if (dict.Keys.Contains(SerializedTypeNameKey)) {
244 type = _typeResolver.ResolveType ((string) dict [SerializedTypeNameKey]);
248 object target = Activator.CreateInstance (type, true);
250 foreach (KeyValuePair<string, object> entry in dict) {
251 object value = entry.Value;
252 if (target is IDictionary) {
253 Type valueType = ReflectionUtils.GetTypedDictionaryValueType (type);
254 if (value != null && valueType == typeof (System.Object))
255 valueType = value.GetType ();
257 ((IDictionary) target).Add (entry.Key, ConvertToType (valueType, value));
260 MemberInfo [] memberCollection = type.GetMember (entry.Key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
261 if (memberCollection == null || memberCollection.Length == 0) {
262 //must evaluate value
267 MemberInfo member = memberCollection [0];
269 if (!ReflectionUtils.CanSetMemberValue (member)) {
270 //must evaluate value
275 Type memberType = ReflectionUtils.GetMemberUnderlyingType (member);
277 if (memberType.IsInterface) {
278 if (memberType.IsGenericType)
279 memberType = ResolveGenericInterfaceToType (memberType);
281 memberType = ResolveInterfaceToType (memberType);
283 if (memberType == null)
284 throw new InvalidOperationException ("Unable to deserialize a member, as its type is an unknown interface.");
287 ReflectionUtils.SetMemberValue (member, target, ConvertToType(memberType, value));
293 Type ResolveGenericInterfaceToType (Type type)
295 Type[] genericArgs = type.GetGenericArguments ();
297 if (ReflectionUtils.IsSubClass (type, typeof (IDictionary <,>)))
298 return typeof (Dictionary <,>).MakeGenericType (genericArgs);
300 if (ReflectionUtils.IsSubClass (type, typeof (IList <>)) ||
301 ReflectionUtils.IsSubClass (type, typeof (ICollection <>)) ||
302 ReflectionUtils.IsSubClass (type, typeof (IEnumerable <>))
304 return typeof (List <>).MakeGenericType (genericArgs);
306 if (ReflectionUtils.IsSubClass (type, typeof (IComparer <>)))
307 return typeof (Comparer <>).MakeGenericType (genericArgs);
309 if (ReflectionUtils.IsSubClass (type, typeof (IEqualityComparer <>)))
310 return typeof (EqualityComparer <>).MakeGenericType (genericArgs);
315 Type ResolveInterfaceToType (Type type)
317 if (typeof (IDictionary).IsAssignableFrom (type))
318 return typeof (Hashtable);
320 if (typeof (IList).IsAssignableFrom (type) ||
321 typeof (ICollection).IsAssignableFrom (type) ||
322 typeof (IEnumerable).IsAssignableFrom (type))
323 return typeof (ArrayList);
325 if (typeof (IComparer).IsAssignableFrom (type))
326 return typeof (Comparer);
331 public object DeserializeObject (string input) {
332 object obj = Evaluate (DeserializeObjectInternal (input), true);
333 IDictionary dictObj = obj as IDictionary;
334 if (dictObj != null && dictObj.Contains(SerializedTypeNameKey)){
335 if (_typeResolver == null) {
336 throw new ArgumentNullException ("resolver", "Must have a type resolver to deserialize an object that has an '__type' member");
339 obj = ConvertToType(null, obj);
344 internal object DeserializeObjectInternal (string input) {
345 return Json.Deserialize (input, this);
348 internal object DeserializeObjectInternal (TextReader input) {
349 return Json.Deserialize (input, this);
352 public void RegisterConverters (IEnumerable<JavaScriptConverter> converters) {
353 if (converters == null)
354 throw new ArgumentNullException ("converters");
356 if (_converterList == null)
357 _converterList = new List<IEnumerable<JavaScriptConverter>> ();
358 _converterList.Add (converters);
361 internal JavaScriptConverter GetConverter (Type type) {
362 if (_converterList != null)
363 for (int i = 0; i < _converterList.Count; i++) {
364 foreach (JavaScriptConverter converter in _converterList [i])
365 foreach (Type supportedType in converter.SupportedTypes)
366 if (supportedType.IsAssignableFrom (type))
373 public string Serialize (object obj) {
374 StringBuilder b = new StringBuilder ();
376 return b.ToString ();
379 public void Serialize (object obj, StringBuilder output) {
380 Json.Serialize (obj, this, output);
383 internal void Serialize (object obj, TextWriter output) {
384 Json.Serialize (obj, this, output);