New tests, update.
[mono.git] / mcs / class / System.Web.Extensions / System.Web.Script.Serialization / JavaScriptSerializer.cs
1 //
2 // JavaScriptSerializer.cs
3 //
4 // Author:
5 //   Konstantin Triger <kostat@mainsoft.com>
6 //
7 // (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com
8 //
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Collections.Generic;
32 using System.Text;
33 using Newtonsoft.Json;
34 using System.IO;
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;
41
42 namespace System.Web.Script.Serialization
43 {
44         public class JavaScriptSerializer
45         {
46                 internal const string SerializedTypeNameKey = "__type";
47                 
48                 List<IEnumerable<JavaScriptConverter>> _converterList;
49                 int _maxJsonLength;
50                 int _recursionLimit;
51                 JavaScriptTypeResolver _typeResolver;
52                 internal static readonly JavaScriptSerializer DefaultSerializer = new JavaScriptSerializer ();
53
54                 public JavaScriptSerializer () 
55                         : this(null)
56                 {
57                 }
58
59                 public JavaScriptSerializer (JavaScriptTypeResolver resolver) 
60                 {
61                         _typeResolver = resolver;
62
63                         ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection) ConfigurationManager.GetSection ("system.web.extensions/scripting/webServices/jsonSerialization");
64                         if (section == null) {
65                                 _maxJsonLength = 102400;
66                                 _recursionLimit = 100;
67                         }
68                         else {
69                                 _maxJsonLength = section.MaxJsonLength;
70                                 _recursionLimit = section.RecursionLimit;
71                         }
72                 }
73
74                 public int MaxJsonLength {
75                         get {
76                                 return _maxJsonLength;
77                         }
78                         set {
79                                 _maxJsonLength = value;
80                         }
81                 }
82                 
83                 public int RecursionLimit {
84                         get {
85                                 return _recursionLimit;
86                         }
87                         set {
88                                 _recursionLimit = value;
89                         }
90                 }
91
92                 internal JavaScriptTypeResolver TypeResolver {
93                         get { return _typeResolver; }
94                 }
95                 
96                 public T ConvertToType<T> (object obj) {
97                         if (obj == null)
98                                 return default (T);
99
100                         return (T) ConvertToType (typeof (T), obj);
101                 }
102
103                 internal object ConvertToType (Type type, object obj) {
104                         if (obj == null)
105                                 return null;
106
107                         if (obj is IDictionary<string, object>) {
108                                 if (type == null)
109                                         obj = EvaluateDictionary ((IDictionary<string, object>) obj);
110                                 else {
111                                         JavaScriptConverter converter = GetConverter (type);
112                                         if (converter != null)
113                                                 return converter.Deserialize (
114                                                         EvaluateDictionary ((IDictionary<string, object>) obj),
115                                                         type, this);
116                                 }
117
118                                 return ConvertToObject ((IDictionary<string, object>) obj, type);
119                         }
120                         if (obj is ArrayList)
121                                 return ConvertToList ((ArrayList) obj, type);
122
123                         if (type == null)
124                                 return obj;
125
126                         Type sourceType = obj.GetType ();
127                         if (type.IsAssignableFrom (sourceType))
128                                 return obj;
129
130                         if (type.IsEnum)
131                                 if (obj is string)
132                                         return Enum.Parse (type, (string) obj, true);
133                                 else
134                                         return Enum.ToObject (type, obj);
135
136                         TypeConverter c = TypeDescriptor.GetConverter (type);
137                         if (c.CanConvertFrom (sourceType)) {
138                                 if (obj is string)
139                                         return c.ConvertFromInvariantString ((string) obj);
140
141                                 return c.ConvertFrom (obj);
142                         }
143
144                         /*
145                          * Take care of the special case whereas in JSON an empty string ("") really means 
146                          * an empty value 
147                          * (see: https://bugzilla.novell.com/show_bug.cgi?id=328836)
148                          */
149                         if ( (type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable<>)) )
150                         {
151                                 string s = obj as String;
152                                 if (String.IsNullOrEmpty(s))
153                                                 return null;
154                         }
155
156                         return Convert.ChangeType (obj, type);
157                 }
158
159                 public T Deserialize<T> (string input) {
160                         return ConvertToType<T> (DeserializeObjectInternal(input));
161                 }
162
163                 static object Evaluate (object value) {
164                         return Evaluate (value, false);
165                 }
166
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);
172                         return value;
173                 }
174
175                 static object EvaluateList (ArrayList e) {
176                         return EvaluateList (e, false);
177                 }
178
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));
183
184                         return convertListToArray ? (object) list.ToArray () : list;
185                 }
186
187                 static IDictionary<string, object> EvaluateDictionary (IDictionary<string, object> dict) {
188                         return EvaluateDictionary (dict, false);
189                 }
190
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));
195                         }
196
197                         return d;
198                 }
199
200                 static readonly Type typeofObject = typeof(object);
201                 static readonly Type typeofGenList = typeof (List<>);
202
203                 object ConvertToList (ArrayList col, Type type) {
204                         Type elementType = null;
205                         if (type != null && type.HasElementType)
206                                 elementType = type.GetElementType ();
207
208                         IList list;
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];
218                                         // generic list
219                                         list = (IList) Activator.CreateInstance (typeofGenList.MakeGenericType (genArgs));
220                                 } else
221                                         list = new ArrayList ();
222                         } else
223                                 throw new InvalidOperationException (String.Format ("Deserializing list type '{0}' not supported.", type.GetType ().Name));
224
225                         if (list.IsReadOnly) {
226                                 EvaluateList (col);
227                                 return list;
228                         }
229
230                         foreach (object value in col)
231                                 list.Add (ConvertToType (elementType, value));
232
233                         if (type != null && type.IsArray)
234                                 list = ((ArrayList) list).ToArray (elementType);
235
236                         return list;
237                 }
238
239                 object ConvertToObject (IDictionary<string, object> dict, Type type) 
240                 {
241                         if (_typeResolver != null) {
242                                 if (dict.Keys.Contains(SerializedTypeNameKey)) {
243                                         // already Evaluated
244                                         type = _typeResolver.ResolveType ((string) dict [SerializedTypeNameKey]);
245                                 }
246                         }
247
248                         object target = Activator.CreateInstance (type, true);
249
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 ();
256                                         
257                                         ((IDictionary) target).Add (entry.Key, ConvertToType (valueType, value));
258                                         continue;
259                                 }
260                                 MemberInfo [] memberCollection = type.GetMember (entry.Key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
261                                 if (memberCollection == null || memberCollection.Length == 0) {
262                                         //must evaluate value
263                                         Evaluate (value);
264                                         continue;
265                                 }
266
267                                 MemberInfo member = memberCollection [0];
268
269                                 if (!ReflectionUtils.CanSetMemberValue (member)) {
270                                         //must evaluate value
271                                         Evaluate (value);
272                                         continue;
273                                 }
274
275                                 Type memberType = ReflectionUtils.GetMemberUnderlyingType (member);
276
277                                 if (memberType.IsInterface) {
278                                         if (memberType.IsGenericType)
279                                                 memberType = ResolveGenericInterfaceToType (memberType);
280                                         else
281                                                 memberType = ResolveInterfaceToType (memberType);
282
283                                         if (memberType == null)
284                                                 throw new InvalidOperationException ("Unable to deserialize a member, as its type is an unknown interface.");
285                                 }
286                                 
287                                 ReflectionUtils.SetMemberValue (member, target, ConvertToType(memberType, value));
288                         }
289
290                         return target;
291                 }
292
293                 Type ResolveGenericInterfaceToType (Type type)
294                 {
295                         Type[] genericArgs = type.GetGenericArguments ();
296                         
297                         if (ReflectionUtils.IsSubClass (type, typeof (IDictionary <,>)))
298                                 return typeof (Dictionary <,>).MakeGenericType (genericArgs);
299
300                         if (ReflectionUtils.IsSubClass (type, typeof (IList <>)) ||
301                             ReflectionUtils.IsSubClass (type, typeof (ICollection <>)) ||
302                             ReflectionUtils.IsSubClass (type, typeof (IEnumerable <>))
303                         )
304                                 return typeof (List <>).MakeGenericType (genericArgs);
305
306                         if (ReflectionUtils.IsSubClass (type, typeof (IComparer <>)))
307                                 return typeof (Comparer <>).MakeGenericType (genericArgs);
308
309                         if (ReflectionUtils.IsSubClass (type, typeof (IEqualityComparer <>)))
310                                 return typeof (EqualityComparer <>).MakeGenericType (genericArgs);
311
312                         return null;
313                 }
314
315                 Type ResolveInterfaceToType (Type type)
316                 {
317                         if (typeof (IDictionary).IsAssignableFrom (type))
318                                 return typeof (Hashtable);
319
320                         if (typeof (IList).IsAssignableFrom (type) ||
321                             typeof (ICollection).IsAssignableFrom (type) ||
322                             typeof (IEnumerable).IsAssignableFrom (type))
323                                 return typeof (ArrayList);
324
325                         if (typeof (IComparer).IsAssignableFrom (type))
326                                 return typeof (Comparer);
327
328                         return null;
329                 }
330                 
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");
337                                 }
338
339                                 obj = ConvertToType(null, obj);
340                         }
341                         return obj; 
342                 }
343
344                 internal object DeserializeObjectInternal (string input) {
345                         return Json.Deserialize (input, this);
346                 }
347
348                 internal object DeserializeObjectInternal (TextReader input) {
349                         return Json.Deserialize (input, this);
350                 }
351
352                 public void RegisterConverters (IEnumerable<JavaScriptConverter> converters) {
353                         if (converters == null)
354                                 throw new ArgumentNullException ("converters");
355
356                         if (_converterList == null)
357                                 _converterList = new List<IEnumerable<JavaScriptConverter>> ();
358                         _converterList.Add (converters);
359                 }
360
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))
367                                                                 return converter;
368                                 }
369
370                         return null;
371                 }
372
373                 public string Serialize (object obj) {
374                         StringBuilder b = new StringBuilder ();
375                         Serialize (obj, b);
376                         return b.ToString ();
377                 }
378
379                 public void Serialize (object obj, StringBuilder output) {
380                         Json.Serialize (obj, this, output);
381                 }
382
383                 internal void Serialize (object obj, TextWriter output) {
384                         Json.Serialize (obj, this, output);
385                 }
386         }
387 }