public override void Write(TypeInfo info, BinaryWriter buffer, object value, BinaryJSONWriter binaryJsonWriter)
 {
     buffer.Write(BinaryValue.ENUM);
     var result = Convert.ChangeType(value, info.EnumUnderlyingType);
     var infoType = info.TypeDescriptions.GetType(info.EnumUnderlyingType);
     binaryJsonWriter.Write(infoType, result, buffer);
 }
        public override void Write(TypeInfo info, BinaryWriter buffer, object value, BinaryJSONWriter binaryJsonWriter)
        {
            if (value == null)
            {
                buffer.Write(BinaryValue.NULL);
            }
            else
            {
                buffer.Write(BinaryValue.DICTIONARY);
                var dictionary = (IDictionary)value;
                var count = dictionary.Count;
                buffer.Write((int)count);

                var keyType = dictionary.GetType().GetGenericArguments()[0];
                if (keyType.IsEnum) keyType = Enum.GetUnderlyingType(keyType);
                var code = info.TypeDescriptions.GetCodeByPrimitiveType(keyType);
                buffer.Write(code);

                foreach (var key in dictionary.Keys)
                {
                    WriteField(key, buffer, code);
                    var keyValue = dictionary[key];
                    binaryJsonWriter.Write(keyValue, buffer);
                }
            }
        }
 public override void Write(TypeInfo info, BinaryWriter buffer, object value, BinaryJSONWriter binaryJsonWriter)
 {
     if (value == null)
     {
         buffer.Write(BinaryValue.NULL);
     }
     else
     {
         buffer.Write(BinaryValue.STRING);
         var bytes = Encoding.UTF8.GetBytes((string)value);
         buffer.Write((int)bytes.Length);
         buffer.Write(bytes);
     }
 }
 public override void Write(TypeInfo info, BinaryWriter buffer, object value, BinaryJSONWriter binaryJsonWriter)
 {
     if (value == null)
     {
         buffer.Write(BinaryValue.NULL);
     }
     else
     {
         buffer.Write(BinaryValue.OBJECT);
         buffer.Write((int)info.Fields.Length);
         foreach (var field in info.Fields)
         {
             WriteField(buffer, field.Name);
             var typeInfo = info.TypeDescriptions.GetType(field.FieldType);
             binaryJsonWriter.Write(typeInfo, field.GetValue(value), buffer);
         }
     }
 }
        public override void Write(TypeInfo info, BinaryWriter buffer, object value, BinaryJSONWriter binaryJsonWriter)
        {
            if (value == null)
            {
                buffer.Write(BinaryValue.NULL);
            }
            else
            {
                var arr = value as Array;
                IEnumerable enumerable = null;
                int count = 0;
                if (arr != null)
                {
                    count = arr.Length;
                    enumerable = arr;
                }
                else
                {
                    var coll = value as IList;
                    if (coll != null)
                    {
                        enumerable = coll;
                        count = coll.Count;
                    }
                }
                if (enumerable != null)
                {
                    buffer.Write(BinaryValue.ARRAY);
                    buffer.Write((int)count);

                    foreach (var item in enumerable)
                    {
                        binaryJsonWriter.Write(item, buffer);
                    }
                }
            }
        }
 public override void Write(TypeInfo info, BinaryWriter buffer, object value, BinaryJSONWriter binaryJsonWriter)
 {
     buffer.Write(BinaryValue.NULL);
 }
 public virtual void Write(TypeInfo info, BinaryWriter buffer, object value, BinaryJSONWriter binaryJsonWriter)
 {
 }
 public override void Write(TypeInfo info, BinaryWriter buffer, object value, BinaryJSONWriter binaryJsonWriter)
 {
     var type = info.Type;
     if (type == TypeInfo.Int32)
     {
         int i = (int) value;
         if (i <= byte.MaxValue && i >= byte.MinValue)
         {
             buffer.Write(BinaryValue.BYTE);
             buffer.Write((byte)i);
         }
         else if (i <= sbyte.MaxValue && i >= sbyte.MinValue)
         {
             buffer.Write(BinaryValue.SBYTE);
             buffer.Write((sbyte)i);
         }
         else if (i <= Int16.MaxValue && i >= Int16.MinValue)
         {
             buffer.Write(BinaryValue.INT16);
             buffer.Write((Int16)i);
         }
         else if (i <= UInt16.MaxValue && i >= UInt16.MinValue)
         {
             buffer.Write(BinaryValue.UINT16);
             buffer.Write((UInt16)i);
         }
         else
         {
             buffer.Write(BinaryValue.INT32);
             buffer.Write(i);
         }
     }
     else if (type == TypeInfo.Float)
     {
         buffer.Write(BinaryValue.FLOAT);
         buffer.Write((float)value);
     }
     else if (type == TypeInfo.Bool)
     {
         buffer.Write(BinaryValue.BOOLEAN);
         buffer.Write((bool)value);
     }
     else if (type == TypeInfo.Double)
     {
         buffer.Write(BinaryValue.DOUBLE);
         buffer.Write((double)value);
     }
     else if (type == TypeInfo.Int16)
     {
         Int16 i = (Int16)value;
         if (i <= byte.MaxValue && i >= byte.MinValue)
         {
             buffer.Write(BinaryValue.BYTE);
             buffer.Write((byte)i);
         }
         else if (i <= sbyte.MaxValue && i >= sbyte.MinValue)
         {
             buffer.Write(BinaryValue.SBYTE);
             buffer.Write((sbyte)i);
         }
         else
         {
             buffer.Write(BinaryValue.INT16);
             buffer.Write(i);
         }
     }
     else if (type == TypeInfo.Byte)
     {
         buffer.Write(BinaryValue.BYTE);
         buffer.Write((byte)value);
     }
     else if (type == TypeInfo.SByte)
     {
         buffer.Write(BinaryValue.SBYTE);
         buffer.Write((sbyte)value);
     }
     else if (type == TypeInfo.UInt32)
     {
         UInt32 i = (UInt32)value;
         if (i <= byte.MaxValue)
         {
             buffer.Write(BinaryValue.BYTE);
             buffer.Write((byte)i);
         }
         else if (i <= UInt16.MaxValue)
         {
             buffer.Write(BinaryValue.UINT16);
             buffer.Write((UInt16)i);
         }
         else
         {
             buffer.Write(BinaryValue.UINT32);
             buffer.Write(i);
         }
     }
     else if (type == TypeInfo.UInt16)
     {
         UInt16 i = (UInt16)value;
         if (i <= byte.MaxValue)
         {
             buffer.Write(BinaryValue.BYTE);
             buffer.Write((byte)i);
         }
         else
         {
             buffer.Write(BinaryValue.UINT16);
             buffer.Write(i);
         }
     }
     else if (type == TypeInfo.Int64)
     {
         Int64 i = (Int64)value;
         if (i <= byte.MaxValue && i >= byte.MinValue)
         {
             buffer.Write(BinaryValue.BYTE);
             buffer.Write((byte)i);
         }
         else if (i <= sbyte.MaxValue && i >= sbyte.MinValue)
         {
             buffer.Write(BinaryValue.SBYTE);
             buffer.Write((sbyte)i);
         }
         else if (i <= Int16.MaxValue && i >= Int16.MinValue)
         {
             buffer.Write(BinaryValue.INT16);
             buffer.Write((Int16)i);
         }
         else if (i <= UInt16.MaxValue && i >= UInt16.MinValue)
         {
             buffer.Write(BinaryValue.UINT16);
             buffer.Write((UInt16)i);
         }
         else if (i <= Int32.MaxValue && i >= Int32.MinValue)
         {
             buffer.Write(BinaryValue.INT32);
             buffer.Write((int)i);
         }
         else if (i <= UInt32.MaxValue && i >= UInt32.MaxValue)
         {
             buffer.Write(BinaryValue.UINT32);
             buffer.Write((UInt32)i);
         }
         else
         {
             buffer.Write(BinaryValue.INT64);
             buffer.Write(i);
         }
     }
     else if (type == TypeInfo.UInt64)
     {
         UInt64 i = (UInt64)value;
         if (i <= byte.MaxValue)
         {
             buffer.Write(BinaryValue.BYTE);
             buffer.Write((byte)i);
         }
         else if (i <= UInt16.MaxValue)
         {
             buffer.Write(BinaryValue.UINT16);
             buffer.Write((UInt16)i);
         }
         else if (i <= Int32.MaxValue)
         {
             buffer.Write(BinaryValue.INT32);
             buffer.Write((int)i);
         }
         else if (i <= UInt32.MaxValue)
         {
             buffer.Write(BinaryValue.UINT32);
             buffer.Write((UInt32)i);
         }
         else
         {
             buffer.Write(BinaryValue.UINT64);
             buffer.Write(i);
         }
     }
     else if (type == TypeInfo.Decimal)
     {
         buffer.Write(BinaryValue.DECIMAL);
         buffer.Write((decimal)value);
     }
     else if (type == TypeInfo.Char)
     {
         buffer.Write(BinaryValue.CHAR);
         buffer.Write((char)value);
     }
 }