public override object Read(BinaryReader buffer, Type resultType, TypeDescription typeDescriptor, byte code, BinaryJSONReader binaryJsonReader)
 {
     var count = buffer.ReadInt32();
     object result = resultType != null ? Activator.CreateInstance(resultType, count) : null;
     var array = result as Array;
     var collection = result as IList;
     Type elemenType = resultType != null ? resultType.GetElementType() : null;
     if (elemenType == null && resultType != null)
     {
         elemenType = resultType.GetGenericArguments()[0];
     }
     for (int i = 0; i < count; i++)
     {
         var current = binaryJsonReader.Read(elemenType, buffer);
         if (array != null)
         {
             array.SetValue(current, i);
         }
         else if (collection != null)
         {
             collection.Add(current);
         }
     }
     return result;
 }
 public override object Read(BinaryReader buffer, Type resultType, TypeDescription typeDescriptor, byte code,
     BinaryJSONReader binaryJsonReader)
 {
     var value = binaryJsonReader.Read(Enum.GetUnderlyingType(resultType), buffer);
     var result = Enum.ToObject(resultType, value);
     return result;
 }
 public override object Read(BinaryReader buffer, Type resultType, TypeDescription typeDescriptor, byte code, BinaryJSONReader binaryJsonReader)
 {
     var count = buffer.ReadInt32();
     var keyCode = buffer.ReadByte();
     IDictionary result = null;
     Type keyType = null;
     Type valueType = null;
     if (IsValidDictionary(resultType))
     {
         result = (IDictionary)Activator.CreateInstance(resultType);
         var args = resultType.GetGenericArguments();
         keyType = args[0];
         valueType = args[1];
     }
     for (int i = 0; i < count; i++)
     {
         var key = ReadField(buffer, keyCode);
         var value = binaryJsonReader.Read(valueType, buffer);
         if (result != null)
         {
             result.Add(GetKey(key, keyType), GetValue(value, valueType));
         }
     }
     return result;
 }
 public override object Read(BinaryReader buffer, Type resultType, TypeDescription typeDescriptor, byte code, BinaryJSONReader binaryJsonReader)
 {
     switch (code)
     {
         case BinaryValue.BYTE:
             return Convert.ChangeType(buffer.ReadByte(), resultType);
         case BinaryValue.SBYTE:
             return Convert.ChangeType(buffer.ReadSByte(), resultType);
         case BinaryValue.INT16:
             return Convert.ChangeType(buffer.ReadInt16(), resultType);
         case BinaryValue.UINT16:
             return Convert.ChangeType(buffer.ReadUInt16(), resultType);
         case BinaryValue.INT32:
             return Convert.ChangeType(buffer.ReadInt32(), resultType);
         case BinaryValue.UINT32:
             return Convert.ChangeType(buffer.ReadUInt32(), resultType);
         case BinaryValue.INT64:
             return Convert.ChangeType(buffer.ReadInt64(), resultType);
         case BinaryValue.UINT64:
             return Convert.ChangeType(buffer.ReadUInt64(), resultType);
         case BinaryValue.FLOAT:
             return Convert.ChangeType(buffer.ReadSingle(), resultType);
         case BinaryValue.BOOLEAN:
             return Convert.ChangeType(buffer.ReadBoolean(), resultType);
         case BinaryValue.DOUBLE:
             return Convert.ChangeType(buffer.ReadDouble(), resultType);
         case BinaryValue.DECIMAL:
             return Convert.ChangeType(buffer.ReadDecimal(), resultType);
         case BinaryValue.CHAR:
             return Convert.ChangeType(buffer.ReadChar(), resultType);
     }
     return null;
 }
Пример #5
0
 public TypeInfo(TypeDescription typeDescription, Type type)
 {
     TypeDescriptions = typeDescription;
     Type = type;
     Type generic;
     if (type == null)
     {
         TypeValue = TypeValue.Null;
     }
     else if (type.IsPrimitive)
     {
         TypeValue = TypeValue.Primitive;
     }
     else if (type.IsArray)
     {
         TypeValue = TypeValue.Array;
     }
     else if (type.IsEnum)
     {
         TypeValue = TypeValue.Enum;
         EnumUnderlyingType = Enum.GetUnderlyingType(type);
     }
     else if (type == typeof(string))
     {
         TypeValue = TypeValue.String;
     }
     else if (typeof(IList).IsAssignableFrom(type))
     {
         TypeValue = TypeValue.List;
     }
     else if (typeof(IDictionary).IsAssignableFrom(type) && type.IsGenericType && ((generic = type.GetGenericArguments()[0]).IsPrimitive || generic.IsEnum || generic == typeof(string)))
     {
         TypeValue = TypeValue.Dictionary;
     }
     else
     {
         TypeValue = TypeValue.Object;
         Fields = type.GetFields(BindingFlags.Instance | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.Public);
         var count = Fields.Length;
         _fields = new Dictionary<string, FieldInfo>(count);
         FieldInfo field;
         for (int i = 0; i < count; i++)
         {
             field = Fields[i];
             _fields.Add(field.Name, field);
         }
     }
 }
 public override object Read(BinaryReader buffer, Type resultType, TypeDescription typeDescriptor, byte code, BinaryJSONReader binaryJsonReader)
 {
     var count = buffer.ReadInt32();
     object result = resultType != null? Activator.CreateInstance(resultType) : null;
     for (int i = 0; i < count; i++)
     {
         var fieldName = ReadField(buffer);
         var info = typeDescriptor.GetType(resultType);
         var field = info.GetField(fieldName);
         Type type = field != null ? field.FieldType : null;
         var value = binaryJsonReader.Read(type, buffer);
         if (field != null)
         {
             info.SetValue(fieldName, result, value);
         }
     }
     return result;
 }
Пример #7
0
 public BinaryJSONWriter(TypeDescription typeDescriptor = null)
 {
     _typeDescriptor = typeDescriptor ?? new TypeDescription();
 }
 public override object Read(BinaryReader buffer, Type resultType, TypeDescription typeDescriptor, byte code, BinaryJSONReader binaryJsonReader)
 {
     return null;
 }
 public override object Read(BinaryReader buffer, Type resultType, TypeDescription typeDescriptor, byte code, BinaryJSONReader binaryJsonReader)
 {
     var count = buffer.ReadInt32();
     var bytes = buffer.ReadBytes(count);
     return Encoding.UTF8.GetString(bytes);
 }