void Pack (MsgPackWriter writer, object o)
{
if (o == null) {
writer.WriteNil ();
return;
}
Type t = o.GetType ();
if (t.IsPrimitive) {
if (t.Equals (typeof (int))) writer.Write ((int)o);
else if (t.Equals (typeof (uint))) writer.Write ((uint)o);
else if (t.Equals (typeof (float))) writer.Write ((float)o);
else if (t.Equals (typeof (double))) writer.Write ((double)o);
else if (t.Equals (typeof (long))) writer.Write ((long)o);
else if (t.Equals (typeof (ulong))) writer.Write ((ulong)o);
else if (t.Equals (typeof (bool))) writer.Write ((bool)o);
else if (t.Equals (typeof (byte))) writer.Write ((byte)o);
else if (t.Equals (typeof (sbyte))) writer.Write ((sbyte)o);
else if (t.Equals (typeof (short))) writer.Write ((short)o);
else if (t.Equals (typeof (ushort))) writer.Write ((ushort)o);
else throw new NotSupportedException (); // char?
return;
}
IDictionary dic = o as IDictionary;
if (dic != null) {
writer.WriteMapHeader (dic.Count);
foreach (System.Collections.DictionaryEntry e in dic) {
Pack (writer, e.Key);
Pack (writer, e.Value);
}
return;
}
if (t.IsArray) {
Array ary = (Array)o;
Type et = t.GetElementType ();
// KeyValuePair<K,V>[] (Map Type)
if (et.IsGenericType && et.GetGenericTypeDefinition ().Equals (KeyValuePairDefinitionType)) {
PropertyInfo propKey = et.GetProperty ("Key");
PropertyInfo propValue = et.GetProperty ("Value");
writer.WriteMapHeader (ary.Length);
for (int i = 0; i < ary.Length; i ++) {
object e = ary.GetValue (i);
Pack (writer, propKey.GetValue (e, null));
Pack (writer, propValue.GetValue (e, null));
}
return;
}
// Array
writer.WriteArrayHeader (ary.Length);
for (int i = 0; i < ary.Length; i ++)
Pack (writer, ary.GetValue (i));
return;
}
}