Exemple #1
0
 public void writeValue(WriteBuffer buffer, object value)
 {
     if (value == null)
     {
         buffer.putUint8(_valueNull);
     }
     else if (value is bool b)
     {
         buffer.putUint8(b ? _valueTrue : _valueFalse);
     }
     else if (value is float f)
     {
         buffer.putUint8(_valueFloat32);
         buffer.putFloat32(f);
     }
     else if (value is int i)
     {
         buffer.putUint8(_valueInt32);
         buffer.putInt32(i);
     }
     else if (value is long l)
     {
         buffer.putUint8(_valueInt64);
         buffer.putInt64(l);
     }
     else if (value is string s)
     {
         buffer.putUint8(_valueString);
         byte[] bytes = Encoding.UTF8.GetBytes(s);
         writeSize(buffer, bytes.Length);
         buffer.putUint8List(bytes);
     }
     else if (value is byte[] bytes)
     {
         buffer.putUint8(_valueUint8List);
         writeSize(buffer, bytes.Length);
         buffer.putUint8List(bytes);
     }
     else if (value is int[] ints)
     {
         buffer.putUint8(_valueInt32List);
         writeSize(buffer, ints.Length);
         buffer.putInt32List(ints);
     }
     else if (value is long[] longs)
     {
         buffer.putUint8(_valueInt64List);
         writeSize(buffer, longs.Length);
         buffer.putInt64List(longs);
     }
     else if (value is float[] floats)
     {
         buffer.putUint8(_valueFloat32List);
         writeSize(buffer, floats.Length);
         buffer.putFloat32List(floats);
     }
     else if (value is IList list)
     {
         buffer.putUint8(_valueList);
         writeSize(buffer, list.Count);
         foreach (object item in list)
         {
             writeValue(buffer, item);
         }
     }
     else if (value is IDictionary dict)
     {
         buffer.putUint8(_valueMap);
         writeSize(buffer, dict.Count);
         foreach (DictionaryEntry entry in dict)
         {
             writeValue(buffer, entry.Key);
             writeValue(buffer, entry.Value);
         }
     }
     else
     {
         throw new ArgumentException(value.ToString());
     }
 }