* SchemaTypes.cs: Changed DataSet type for a more generic XmlSerializable.
[mono.git] / mcs / class / System.Web / System.Web.UI / LosFormatter.cs
index 338a9e5d002582604359f164b97873e5cb36cf41..d06317cfd97e93e4d052ef9f2edad187092c00a4 100644 (file)
@@ -35,6 +35,8 @@ namespace System.Web.UI
                const char arrayListID = 'L';
                const char hashtableID = 'h';
                const char binaryID = 'b';
+               const char arrayID = 'a';
+               const char dateTimeID = 'd';
                
                static Hashtable specialTypes;
                static Hashtable idToType;
@@ -48,6 +50,8 @@ namespace System.Web.UI
                        specialTypes.Add (typeof (Color), new WriteObject (WriteColor));
                        specialTypes.Add (typeof (ArrayList), new WriteObject (WriteArrayList));
                        specialTypes.Add (typeof (Hashtable), new WriteObject (WriteHashtable));
+                       specialTypes.Add (typeof (Array), new WriteObject (WriteArray));
+                       specialTypes.Add (typeof (DateTime), new WriteObject (WriteDateTime));
 
                        idToType = new Hashtable ();
                        idToType.Add (typeof (string), stringID);
@@ -61,6 +65,7 @@ namespace System.Web.UI
                        idToType.Add (typeof (Color), colorID);
                        idToType.Add (typeof (ArrayList), arrayListID);
                        idToType.Add (typeof (Hashtable), hashtableID);
+                       idToType.Add (typeof (Array), arrayID);
                }
                
                public object Deserialize (Stream stream)
@@ -215,6 +220,7 @@ namespace System.Web.UI
                                triplet.Third = DeserializeObject (splitted [2]);
                                break;
                        case arrayListID:
+                       case arrayID:
                                ArrayList list = new ArrayList ();
                                obj = list;
                                splitted = GetStringValues (enclosed);
@@ -222,6 +228,10 @@ namespace System.Web.UI
                                        object o = DeserializeObject (s);
                                        list.Add (o);
                                }
+
+                               if (input [0] == arrayID)
+                                       obj = list.ToArray (typeof (object));
+
                                break;
                        case hashtableID:
                                object key;
@@ -241,11 +251,14 @@ namespace System.Web.UI
                                }
                                break;
                        case binaryID:
-                               byte [] buffer = WebEncoding.Encoding.GetBytes (enclosed);
+                               byte [] buffer = Convert.FromBase64String (enclosed);
                                MemoryStream ms = new MemoryStream (buffer);
                                BinaryFormatter fmt = new BinaryFormatter ();
                                obj = fmt.Deserialize (ms);
                                break;
+                       case dateTimeID:
+                               obj = new DateTime (Int64.Parse (enclosed));
+                               break;
                        default:
                                throw new ArgumentException ("input");
                        }
@@ -346,6 +359,22 @@ namespace System.Web.UI
                        output.Write('>');
                }
 
+               private static void WriteArray (LosFormatter formatter, TextWriter output, object value)
+               {
+                       if (value == null)
+                               return;
+                       
+                       output.Write (arrayID);
+                       output.Write ('<');
+                       Array array = (Array) value;
+                       for (int i = 0; i < array.Length; i++) {
+                               formatter.SerializeObject (output, array.GetValue (i));
+                               if (i != array.Length - 1)
+                                       output.Write (';');
+                       }
+                       output.Write('>');
+               }
+
                private static void WriteHashtable (LosFormatter formatter, TextWriter output, object value)
                {
                        if (value == null)
@@ -366,6 +395,17 @@ namespace System.Web.UI
                        output.Write('>');
                }
 
+               private static void WriteDateTime (LosFormatter formatter, TextWriter output, object value)
+               {
+                       if (value == null)
+                               return;
+                       
+                       output.Write (dateTimeID);
+                       output.Write ('<');
+                       output.Write (((DateTime) value).Ticks);
+                       output.Write('>');
+               }
+
                private static string EscapeSpecialChars (string str)
                {
                        if (str.IndexOfAny (specialChars) == -1)
@@ -380,6 +420,20 @@ namespace System.Web.UI
                
                private void SerializeBinary (TextWriter output, object value)
                {
+                       WebTrace.PushContext ("LosFormatter.SerializeBinary");
+                       /* This is just for debugging purposes */
+                       /*if (value is Array) {
+                               Array array = (Array) value;
+                               for (int i = 0; i < array.Length; i++) {
+                                       object o = array.GetValue (i);
+                                       if (o == null)
+                                               WebTrace.WriteLine ("\t{0} is null", i);
+                                       else
+                                               WebTrace.WriteLine ("\t{0} {1} {2}", i, o.GetType (), o);
+                               }
+                       }
+                       */
+                       
                        BinaryFormatter fmt = new BinaryFormatter ();
                        MemoryStream stream = new MemoryStream ();
 
@@ -387,8 +441,10 @@ namespace System.Web.UI
                        output.Write (binaryID);
                        output.Write ('<');
                        byte [] buffer = stream.GetBuffer ();
-                       output.Write (Convert.ToBase64String (stream.GetBuffer ()));
+                       output.Write (Convert.ToBase64String (buffer));
                        output.Write ('>');
+                       
+                       WebTrace.PopContext ();
                }
 
                private void SerializeObject (TextWriter output, object value)
@@ -401,6 +457,9 @@ namespace System.Web.UI
                        }
 
                        Type t = value.GetType ();
+                       if (t.IsArray)
+                               t = typeof (Array);
+
                        if (specialTypes.Contains (t)) {
                                WriteObject w = (WriteObject) specialTypes [t];
                                w (this, output, value);