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; } }
private void Pack(MsgPackWriter writer, object o) { if (o == null) { writer.WriteNil(); } else { Type type = o.GetType(); if (type.IsPrimitive) { if (type.Equals(typeof(int))) { writer.Write((int)o); } else if (type.Equals(typeof(uint))) { writer.Write((uint)o); } else if (type.Equals(typeof(float))) { writer.Write((float)o); } else if (type.Equals(typeof(double))) { writer.Write((double)o); } else if (type.Equals(typeof(long))) { writer.Write((long)o); } else if (type.Equals(typeof(ulong))) { writer.Write((ulong)o); } else if (type.Equals(typeof(bool))) { writer.Write((bool)o); } else if (type.Equals(typeof(byte))) { writer.Write((byte)o); } else if (type.Equals(typeof(sbyte))) { writer.Write((sbyte)o); } else if (type.Equals(typeof(short))) { writer.Write((short)o); } else { if (!type.Equals(typeof(ushort))) { throw new NotSupportedException(); } writer.Write((ushort)o); } } else { IDictionary dictionary = o as IDictionary; if (dictionary != null) { writer.WriteMapHeader(dictionary.Count); IDictionaryEnumerator enumerator = dictionary.GetEnumerator(); try { while (enumerator.MoveNext()) { DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current; Pack(writer, dictionaryEntry.Key); Pack(writer, dictionaryEntry.Value); } } finally { (enumerator as IDisposable)?.Dispose(); } } else if (type.IsArray) { Array array = (Array)o; Type elementType = type.GetElementType(); if (elementType.IsGenericType && elementType.GetGenericTypeDefinition().Equals(KeyValuePairDefinitionType)) { PropertyInfo property = elementType.GetProperty("Key"); PropertyInfo property2 = elementType.GetProperty("Value"); writer.WriteMapHeader(array.Length); for (int i = 0; i < array.Length; i++) { object value = array.GetValue(i); Pack(writer, property.GetValue(value, null)); Pack(writer, property2.GetValue(value, null)); } } else { writer.WriteArrayHeader(array.Length); for (int j = 0; j < array.Length; j++) { Pack(writer, array.GetValue(j)); } } } } } }
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 if (t.Equals(typeof(char))) { writer.Write((ushort)(char)o); } else { throw new NotSupportedException(); } return; } PackDelegate packer; if (PackerMapping.TryGetValue(t, out packer)) { packer(this, writer, o); return; } if (t.IsArray) { Array ary = (Array)o; writer.WriteArrayHeader(ary.Length); for (int i = 0; i < ary.Length; i++) { Pack(writer, ary.GetValue(i)); } return; } ReflectionCacheEntry entry = ReflectionCache.Lookup(t); writer.WriteMapHeader(entry.FieldMap.Count); foreach (KeyValuePair <string, FieldInfo> pair in entry.FieldMap) { writer.Write(pair.Key, _buf); object v = pair.Value.GetValue(o); if (pair.Value.FieldType.IsInterface && v != null) { writer.WriteArrayHeader(2); writer.Write(v.GetType().FullName); } Pack(writer, v); } }
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; } }
private void Pack(MsgPackWriter writer, object o) { if (o == null) { writer.WriteNil(); return; } if (o is string s) { writer.Write(Encoding.UTF8.GetBytes((string)o)); return; } Type type = o.GetType(); if (type.IsPrimitive) { if (type.Equals(typeof(int))) { writer.Write((int)o); return; } if (type.Equals(typeof(uint))) { writer.Write((uint)o); return; } if (type.Equals(typeof(float))) { writer.Write((float)o); return; } if (type.Equals(typeof(double))) { writer.Write((double)o); return; } if (type.Equals(typeof(long))) { writer.Write((long)o); return; } if (type.Equals(typeof(ulong))) { writer.Write((ulong)o); return; } if (type.Equals(typeof(bool))) { writer.Write((bool)o); return; } if (type.Equals(typeof(byte))) { writer.Write((byte)o); return; } if (type.Equals(typeof(sbyte))) { writer.Write((sbyte)o); return; } if (type.Equals(typeof(short))) { writer.Write((short)o); return; } if (type.Equals(typeof(ushort))) { writer.Write((ushort)o); return; } throw new NotSupportedException(); } IDictionary dictionary = o as IDictionary; if (dictionary != null) { writer.WriteMapHeader(dictionary.Count); foreach (DictionaryEntry item in dictionary) { Pack(writer, item.Key); Pack(writer, item.Value); } } else { if (!type.IsArray) { return; } Array array = (Array)o; Type elementType = type.GetElementType(); if (elementType.IsGenericType && elementType.GetGenericTypeDefinition().Equals(KeyValuePairDefinitionType)) { PropertyInfo property = elementType.GetProperty("Key"); PropertyInfo property2 = elementType.GetProperty("Value"); writer.WriteMapHeader(array.Length); for (int i = 0; i < array.Length; i++) { object value = array.GetValue(i); Pack(writer, property.GetValue(value, null)); Pack(writer, property2.GetValue(value, null)); } } else { writer.WriteArrayHeader(array.Length); for (int j = 0; j < array.Length; j++) { Pack(writer, array.GetValue(j)); } } } }
private void Pack(MsgPackWriter writer, object o) { if (o == null) { writer.WriteNil(); return; } Type type = o.GetType(); PackDelegate value; if (type.IsPrimitive) { if (type.Equals(typeof(int))) { writer.Write((int)o); return; } if (type.Equals(typeof(uint))) { writer.Write((uint)o); return; } if (type.Equals(typeof(float))) { writer.Write((float)o); return; } if (type.Equals(typeof(double))) { writer.Write((double)o); return; } if (type.Equals(typeof(long))) { writer.Write((long)o); return; } if (type.Equals(typeof(ulong))) { writer.Write((ulong)o); return; } if (type.Equals(typeof(bool))) { writer.Write((bool)o); return; } if (type.Equals(typeof(byte))) { writer.Write((byte)o); return; } if (type.Equals(typeof(sbyte))) { writer.Write((sbyte)o); return; } if (type.Equals(typeof(short))) { writer.Write((short)o); return; } if (type.Equals(typeof(ushort))) { writer.Write((ushort)o); return; } if (!type.Equals(typeof(char))) { throw new NotSupportedException(); } writer.Write((ushort)(char)o); } else if (PackerMapping.TryGetValue(type, out value)) { value(this, writer, o); } else if (type.IsArray) { Array array = (Array)o; writer.WriteArrayHeader(array.Length); for (int i = 0; i < array.Length; i++) { Pack(writer, array.GetValue(i)); } } else { ReflectionCacheEntry reflectionCacheEntry = ReflectionCache.Lookup(type); writer.WriteMapHeader(reflectionCacheEntry.FieldMap.Count); foreach (KeyValuePair <string, FieldInfo> item in reflectionCacheEntry.FieldMap) { writer.Write(item.Key, _buf); object value2 = item.Value.GetValue(o); if (item.Value.FieldType.IsInterface && value2 != null) { writer.WriteArrayHeader(2); writer.Write(value2.GetType().FullName); } Pack(writer, value2); } } }
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 if (t.Equals(typeof(char))) { writer.Write((ushort)(char)o); } else { throw new NotSupportedException(); } return; } PackDelegate packer; if (PackerMapping.TryGetValue(t, out packer)) { packer(this, writer, o); return; } if (o is Packable) { Packable p = o as Packable; p.Pack(this, writer); return; } if (t.IsArray) { Array ary = (Array)o; writer.WriteArrayHeader(ary.Length); for (int i = 0; i < ary.Length; i++) { Pack(writer, ary.GetValue(i)); } return; } if (typeof(IList).IsAssignableFrom(t)) { IList list = (IList)o; writer.WriteArrayHeader(list.Count); for (int i = 0; i < list.Count; i++) { Pack(writer, list[i]); } return; } if (t.IsGenericType) { var setType = typeof(ISet <>).MakeGenericType(t.GetGenericArguments()[0]); if (setType.IsAssignableFrom(t)) { IEnumerable list = (IEnumerable)o; writer.WriteArrayHeader((int)t.GetProperty("Count").GetValue(o, null)); foreach (object e in list) { Pack(writer, e); } return; } } if (typeof(IDictionary).IsAssignableFrom(t)) { IDictionary dict = (IDictionary)o; writer.WriteArrayHeader(dict.Count); IDictionaryEnumerator enumerator = dict.GetEnumerator(); while (enumerator.MoveNext()) { Pack(writer, enumerator.Key); Pack(writer, enumerator.Value); } return; } ReflectionCacheEntry entry = ReflectionCache.Lookup(t); writer.WriteMapHeader(entry.FieldMap.Count); foreach (KeyValuePair <string, FieldInfo> pair in entry.FieldMap) { writer.Write(pair.Key, _buf); object v = pair.Value.GetValue(o); if (pair.Value.FieldType.IsInterface && v != null) { writer.WriteArrayHeader(2); writer.Write(v.GetType().FullName); } Pack(writer, v); } }
void Pack(MsgPackWriter writer, object o) { if (o == null) { writer.WriteNil (); return; } if (o is int) writer.Write ((int)o); else if (o is uint) writer.Write ((uint)o); else if (o is float) writer.Write ((float)o); else if (o is double) writer.Write ((double)o); else if (o is long) writer.Write ((long)o); else if (o is ulong) writer.Write ((ulong)o); else if (o is bool) writer.Write ((bool)o); else if (o is byte) writer.Write ((byte)o); else if (o is sbyte) writer.Write ((sbyte)o); else if (o is short) writer.Write ((short)o); else if (o is ushort) writer.Write ((ushort)o); else if (o is string) writer.Write(o as string); else if (o is Dictionary<object, object>) { writer.WriteMapHeader((o as Dictionary<object, object>).Count); foreach (var pair in (o as Dictionary<object, object>)) { Pack(writer, pair.Key); Pack(writer, pair.Value); } } else throw new NotSupportedException (); }