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;
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);
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)
triplet.Third = DeserializeObject (splitted [2]);
break;
case arrayListID:
+ case arrayID:
ArrayList list = new ArrayList ();
obj = list;
splitted = GetStringValues (enclosed);
object o = DeserializeObject (s);
list.Add (o);
}
+
+ if (input [0] == arrayID)
+ obj = list.ToArray (typeof (object));
+
break;
case hashtableID:
object key;
}
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");
}
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)
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)
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 ();
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)
}
Type t = value.GetType ();
+ if (t.IsArray)
+ t = typeof (Array);
+
if (specialTypes.Contains (t)) {
WriteObject w = (WriteObject) specialTypes [t];
w (this, output, value);