Merge pull request #1337 from RyanMelenaNoesis/master
[mono.git] / mcs / class / System.Web.Extensions / System.Web.Script.Serialization / JavaScriptSerializer.cs
1 //
2 // JavaScriptSerializer.cs
3 //
4 // Authors:
5 //   Konstantin Triger <kostat@mainsoft.com>
6 //   Marek Safar <marek.safar@gmail.com>
7 //
8 // (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com
9 // Copyright 2012 Xamarin Inc.
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections.Generic;
33 using System.Text;
34 using Newtonsoft.Json;
35 using System.IO;
36 using System.Collections;
37 using System.Reflection;
38 using Newtonsoft.Json.Utilities;
39 using System.ComponentModel;
40 using System.Configuration;
41 using System.Web.Configuration;
42
43 namespace System.Web.Script.Serialization
44 {
45         public class JavaScriptSerializer
46         {
47                 internal const string SerializedTypeNameKey = "__type";
48                 
49                 List<IEnumerable<JavaScriptConverter>> _converterList;
50                 int _maxJsonLength;
51                 int _recursionLimit;
52                 JavaScriptTypeResolver _typeResolver;
53                 internal static readonly JavaScriptSerializer DefaultSerializer = new JavaScriptSerializer (null, false);
54
55                 public JavaScriptSerializer () : this (null, false)
56                 {
57                 }
58
59                 public JavaScriptSerializer (JavaScriptTypeResolver resolver) : this (resolver, false)
60                 {
61                 }
62                 
63                 internal JavaScriptSerializer (JavaScriptTypeResolver resolver, bool registerConverters)
64                 {
65                         _typeResolver = resolver;
66
67                         ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection) ConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/jsonSerialization");
68                         if (section == null) {
69 #if NET_3_5
70                                 _maxJsonLength = 2097152;
71 #else
72                                 _maxJsonLength = 102400;
73 #endif
74                                 _recursionLimit = 100;
75                         } else {
76                                 _maxJsonLength = section.MaxJsonLength;
77                                 _recursionLimit = section.RecursionLimit;
78
79                                 if (registerConverters) {
80                                         ConvertersCollection converters = section.Converters;
81                                         if (converters != null && converters.Count > 0) {
82                                                 var cvtlist = new List <JavaScriptConverter> ();
83                                                 Type type;
84                                                 string typeName;
85                                                 JavaScriptConverter jsc;
86                                                 
87                                                 foreach (Converter cvt in converters) {
88                                                         typeName = cvt != null ? cvt.Type : null;
89                                                         if (typeName == null)
90                                                                 continue;
91                                                         
92                                                         type = HttpApplication.LoadType (typeName, true);
93                                                         if (type == null || !typeof (JavaScriptConverter).IsAssignableFrom (type))
94                                                                 continue;
95                                                         
96                                                         jsc = Activator.CreateInstance (type) as JavaScriptConverter;
97                                                         cvtlist.Add (jsc);
98                                                 }
99                                         
100                                                 RegisterConverters (cvtlist);
101                                         }
102                                 }
103                         }
104                 }
105
106                 
107                 public int MaxJsonLength {
108                         get {
109                                 return _maxJsonLength;
110                         }
111                         set {
112                                 _maxJsonLength = value;
113                         }
114                 }
115                 
116                 public int RecursionLimit {
117                         get {
118                                 return _recursionLimit;
119                         }
120                         set {
121                                 _recursionLimit = value;
122                         }
123                 }
124
125                 internal JavaScriptTypeResolver TypeResolver {
126                         get { return _typeResolver; }
127                 }
128                 
129                 public T ConvertToType<T> (object obj) {
130                         if (obj == null)
131                                 return default (T);
132
133                         return (T) ConvertToType (obj, typeof (T));
134                 }
135
136 #if NET_4_0
137                 public
138 #else
139                 internal
140 #endif
141                 object ConvertToType (object obj, Type targetType)
142                 {
143                         if (obj == null)
144                                 return null;
145
146                         if (obj is IDictionary<string, object>) {
147                                 if (targetType == null)
148                                         obj = EvaluateDictionary ((IDictionary<string, object>) obj);
149                                 else {
150                                         JavaScriptConverter converter = GetConverter (targetType);
151                                         if (converter != null)
152                                                 return converter.Deserialize (
153                                                         EvaluateDictionary ((IDictionary<string, object>) obj),
154                                                         targetType, this);
155                                 }
156
157                                 return ConvertToObject ((IDictionary<string, object>) obj, targetType);
158                         }
159                         if (obj is ArrayList)
160                                 return ConvertToList ((ArrayList) obj, targetType);
161
162                         if (targetType == null)
163                                 return obj;
164
165                         Type sourceType = obj.GetType ();
166                         if (targetType.IsAssignableFrom (sourceType))
167                                 return obj;
168
169                         if (targetType.IsEnum)
170                                 if (obj is string)
171                                         return Enum.Parse (targetType, (string) obj, true);
172                                 else
173                                         return Enum.ToObject (targetType, obj);
174
175                         TypeConverter c = TypeDescriptor.GetConverter (targetType);
176                         if (c.CanConvertFrom (sourceType)) {
177                                 if (obj is string)
178                                         return c.ConvertFromInvariantString ((string) obj);
179
180                                 return c.ConvertFrom (obj);
181                         }
182
183                         if ((targetType.IsGenericType) && (targetType.GetGenericTypeDefinition () == typeof (Nullable<>))) {
184                                 if (obj is String) {
185                                         /*
186                                          * Take care of the special case whereas in JSON an empty string ("") really means 
187                                          * an empty value 
188                                          * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
189                                          */
190                                         if(String.IsNullOrEmpty ((String)obj))
191                                                 return null;
192                                 } else if (c.CanConvertFrom (typeof (string))) {
193                                         TypeConverter objConverter = TypeDescriptor.GetConverter (obj);
194                                         string s = objConverter.ConvertToInvariantString (obj);
195                                         return c.ConvertFromInvariantString (s);
196                                 }
197                         }
198
199                         return Convert.ChangeType (obj, targetType);
200                 }
201
202                 public T Deserialize<T> (string input) {
203                         return ConvertToType<T> (DeserializeObjectInternal(input));
204                 }
205
206                 public object Deserialize (string input, Type targetType) {
207                         object obj = DeserializeObjectInternal (input);
208
209                         if (obj == null)
210                                 return Activator.CreateInstance (targetType);
211
212                         return ConvertToType (obj, targetType);
213                 }
214
215                 static object Evaluate (object value) {
216                         return Evaluate (value, false);
217                 }
218
219                 static object Evaluate (object value, bool convertListToArray) {
220                         if (value is IDictionary<string, object>)
221                                 value = EvaluateDictionary ((IDictionary<string, object>) value, convertListToArray);
222                         else if (value is ArrayList)
223                                 value = EvaluateList ((ArrayList) value, convertListToArray);
224                         return value;
225                 }
226
227                 static object EvaluateList (ArrayList e) {
228                         return EvaluateList (e, false);
229                 }
230
231                 static object EvaluateList (ArrayList e, bool convertListToArray) {
232                         ArrayList list = new ArrayList ();
233                         foreach (object value in e)
234                                 list.Add (Evaluate (value, convertListToArray));
235
236                         return convertListToArray ? (object) list.ToArray () : list;
237                 }
238
239                 static IDictionary<string, object> EvaluateDictionary (IDictionary<string, object> dict) {
240                         return EvaluateDictionary (dict, false);
241                 }
242
243                 static IDictionary<string, object> EvaluateDictionary (IDictionary<string, object> dict, bool convertListToArray) {
244                         Dictionary<string, object> d = new Dictionary<string, object> (StringComparer.Ordinal);
245                         foreach (KeyValuePair<string, object> entry in dict) {
246                                 d.Add (entry.Key, Evaluate (entry.Value, convertListToArray));
247                         }
248
249                         return d;
250                 }
251
252                 static readonly Type typeofObject = typeof(object);
253                 static readonly Type typeofGenList = typeof (List<>);
254
255                 object ConvertToList (ArrayList col, Type type) {
256                         Type elementType = null;
257                         if (type != null && type.HasElementType)
258                                 elementType = type.GetElementType ();
259
260                         IList list;
261                         if (type == null || type.IsArray || typeofObject == type || typeof (ArrayList).IsAssignableFrom (type))
262                                 list = new ArrayList ();
263                         else if (ReflectionUtils.IsInstantiatableType (type))
264                                 // non-generic typed list
265                                 list = (IList) Activator.CreateInstance (type, true);
266                         else if (ReflectionUtils.IsAssignable (type, typeofGenList)) {
267                                 if (type.IsGenericType) {
268                                         Type [] genArgs = type.GetGenericArguments ();
269                                         elementType = genArgs [0];
270                                         // generic list
271                                         list = (IList) Activator.CreateInstance (typeofGenList.MakeGenericType (genArgs));
272                                 } else
273                                         list = new ArrayList ();
274                         } else
275                                 throw new InvalidOperationException (String.Format ("Deserializing list type '{0}' not supported.", type.GetType ().Name));
276
277                         if (list.IsReadOnly) {
278                                 EvaluateList (col);
279                                 return list;
280                         }
281                         
282                         if (elementType == null)
283                                 elementType = typeof (object);
284
285                         foreach (object value in col)
286                                 list.Add (ConvertToType (value, elementType));
287
288                         if (type != null && type.IsArray)
289                                 list = ((ArrayList) list).ToArray (elementType);
290
291                         return list;
292                 }
293
294                 object ConvertToObject (IDictionary<string, object> dict, Type type) 
295                 {
296                         if (_typeResolver != null) {
297                                 if (dict.Keys.Contains(SerializedTypeNameKey)) {
298                                         // already Evaluated
299                                         type = _typeResolver.ResolveType ((string) dict [SerializedTypeNameKey]);
300                                 }
301                         }
302
303                         if (type.IsGenericType) {
304                                 if (type.GetGenericTypeDefinition ().IsAssignableFrom (typeof (IDictionary <,>))) {
305                                         Type[] arguments = type.GetGenericArguments ();
306                                         if (arguments == null || arguments.Length != 2 || (arguments [0] != typeof (object) && arguments [0] != typeof (string)))
307                                                 throw new InvalidOperationException (
308                                                         "Type '" + type + "' is not not supported for serialization/deserialization of a dictionary, keys must be strings or objects.");
309                                         if (type.IsAbstract) {
310                                                 Type dictType = typeof (Dictionary <,>);
311                                                 type = dictType.MakeGenericType (arguments [0], arguments [1]);
312                                         }
313                                 }
314                         } else if (type.IsAssignableFrom (typeof (IDictionary)))
315                                 type = typeof (Dictionary <string, object>);
316                         
317                         object target = Activator.CreateInstance (type, true);
318
319                         foreach (KeyValuePair<string, object> entry in dict) {
320                                 object value = entry.Value;
321                                 if (target is IDictionary) {
322                                         Type valueType = ReflectionUtils.GetTypedDictionaryValueType (type);
323                                         if (value != null && valueType == typeof (System.Object))
324                                                 valueType = value.GetType ();
325                                         
326                                         ((IDictionary) target).Add (entry.Key, ConvertToType (value, valueType));
327                                         continue;
328                                 }
329                                 MemberInfo [] memberCollection = type.GetMember (entry.Key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
330                                 if (memberCollection == null || memberCollection.Length == 0) {
331                                         //must evaluate value
332                                         Evaluate (value);
333                                         continue;
334                                 }
335
336                                 MemberInfo member = memberCollection [0];
337
338                                 if (!ReflectionUtils.CanSetMemberValue (member)) {
339                                         //must evaluate value
340                                         Evaluate (value);
341                                         continue;
342                                 }
343
344                                 Type memberType = ReflectionUtils.GetMemberUnderlyingType (member);
345
346                                 if (memberType.IsInterface) {
347                                         if (memberType.IsGenericType)
348                                                 memberType = ResolveGenericInterfaceToType (memberType);
349                                         else
350                                                 memberType = ResolveInterfaceToType (memberType);
351
352                                         if (memberType == null)
353                                                 throw new InvalidOperationException ("Unable to deserialize a member, as its type is an unknown interface.");
354                                 }
355                                 
356                                 ReflectionUtils.SetMemberValue (member, target, ConvertToType(value, memberType));
357                         }
358
359                         return target;
360                 }
361
362                 Type ResolveGenericInterfaceToType (Type type)
363                 {
364                         Type[] genericArgs = type.GetGenericArguments ();
365                         
366                         if (ReflectionUtils.IsSubClass (type, typeof (IDictionary <,>)))
367                                 return typeof (Dictionary <,>).MakeGenericType (genericArgs);
368
369                         if (ReflectionUtils.IsSubClass (type, typeof (IList <>)) ||
370                             ReflectionUtils.IsSubClass (type, typeof (ICollection <>)) ||
371                             ReflectionUtils.IsSubClass (type, typeof (IEnumerable <>))
372                         )
373                                 return typeof (List <>).MakeGenericType (genericArgs);
374
375                         if (ReflectionUtils.IsSubClass (type, typeof (IComparer <>)))
376                                 return typeof (Comparer <>).MakeGenericType (genericArgs);
377
378                         if (ReflectionUtils.IsSubClass (type, typeof (IEqualityComparer <>)))
379                                 return typeof (EqualityComparer <>).MakeGenericType (genericArgs);
380
381                         return null;
382                 }
383
384                 Type ResolveInterfaceToType (Type type)
385                 {
386                         if (typeof (IDictionary).IsAssignableFrom (type))
387                                 return typeof (Hashtable);
388
389                         if (typeof (IList).IsAssignableFrom (type) ||
390                             typeof (ICollection).IsAssignableFrom (type) ||
391                             typeof (IEnumerable).IsAssignableFrom (type))
392                                 return typeof (ArrayList);
393
394                         if (typeof (IComparer).IsAssignableFrom (type))
395                                 return typeof (Comparer);
396
397                         return null;
398                 }
399                 
400                 public object DeserializeObject (string input) {
401                         object obj = Evaluate (DeserializeObjectInternal (input), true);
402                         IDictionary dictObj = obj as IDictionary;
403                         if (dictObj != null && dictObj.Contains(SerializedTypeNameKey)){
404                                 if (_typeResolver == null) {
405                                         throw new ArgumentNullException ("resolver", "Must have a type resolver to deserialize an object that has an '__type' member");
406                                 }
407
408                                 obj = ConvertToType(obj, null);
409                         }
410                         return obj; 
411                 }
412
413                 internal object DeserializeObjectInternal (string input) {
414                         return Json.Deserialize (input, this);
415                 }
416
417                 internal object DeserializeObjectInternal (TextReader input) {
418                         return Json.Deserialize (input, this);
419                 }
420
421                 public void RegisterConverters (IEnumerable<JavaScriptConverter> converters) {
422                         if (converters == null)
423                                 throw new ArgumentNullException ("converters");
424
425                         if (_converterList == null)
426                                 _converterList = new List<IEnumerable<JavaScriptConverter>> ();
427                         _converterList.Add (converters);
428                 }
429
430                 internal JavaScriptConverter GetConverter (Type type) {
431                         if (_converterList != null)
432                                 for (int i = 0; i < _converterList.Count; i++) {
433                                         foreach (JavaScriptConverter converter in _converterList [i])
434                                                 foreach (Type supportedType in converter.SupportedTypes)
435                                                         if (supportedType.IsAssignableFrom (type))
436                                                                 return converter;
437                                 }
438
439                         return null;
440                 }
441
442                 public string Serialize (object obj) {
443                         StringBuilder b = new StringBuilder ();
444                         Serialize (obj, b);
445                         return b.ToString ();
446                 }
447
448                 public void Serialize (object obj, StringBuilder output) {
449                         Json.Serialize (obj, this, output);
450                 }
451
452                 internal void Serialize (object obj, TextWriter output) {
453                         Json.Serialize (obj, this, output);
454                 }
455         }
456 }