Esempio n. 1
0
        // Writes

        internal void Write(MProto proto, Mio meta, object data)
        {
            if (meta == null)
            {
                return;
            }
            switch (meta.Type)
            {
            case MType.Binary: proto.WriteBinary((byte[])data); break;

            case MType.Bool: proto.WriteBool(Convert.ToBoolean(data)); break;

            case MType.Byte: proto.WriteByte((sbyte)Convert.ToByte(data)); break;

            case MType.Double: proto.WriteDouble(Convert.ToDouble(data)); break;

            case MType.Float: proto.WriteFloat(Convert.ToSingle(data)); break;

            case MType.I16: proto.WriteI16(Convert.ToInt16(data)); break;

            case MType.I32: proto.WriteI32(Convert.ToInt32(data)); break;

            case MType.I64: proto.WriteI64(Convert.ToInt64(data)); break;

            case MType.List: WriteList(proto, meta as MioList, data as List <object>); break;

            case MType.Map: WriteMap(proto, meta as MioMap, data as Dictionary <object, object>); break;

            case MType.Null: break;

            case MType.Set: WriteList(proto, meta as MioList, data as List <object>); break;

            case MType.String: proto.WriteString(Convert.ToString(data)); break;

            case MType.Struct: WriteStruct(proto, meta as MioStruct, data as MioObject); break;
            }
        }
Esempio n. 2
0
 // Writes
 internal void Write(MProto proto, Mio meta, object data)
 {
     if (meta == null)
         return;
     switch (meta.Type)
     {
         case MType.Binary: proto.WriteBinary((byte[])data); break;
         case MType.Bool: proto.WriteBool(Convert.ToBoolean(data)); break;
         case MType.Byte: proto.WriteByte((sbyte)Convert.ToByte(data)); break;
         case MType.Double: proto.WriteDouble(Convert.ToDouble(data)); break;
         case MType.Float: proto.WriteFloat(Convert.ToSingle(data)); break;
         case MType.I16: proto.WriteI16(Convert.ToInt16(data)); break;
         case MType.I32: proto.WriteI32(Convert.ToInt32(data)); break;
         case MType.I64: proto.WriteI64(Convert.ToInt64(data)); break;
         case MType.List: WriteList(proto, meta as MioList, data as List<object>); break;
         case MType.Map: WriteMap(proto, meta as MioMap, data as Dictionary<object,object>); break;
         case MType.Null: break;
         case MType.Set: WriteList(proto, meta as MioList, data as List<object>); break;
         case MType.String: proto.WriteString(Convert.ToString(data)); break;
         case MType.Struct: WriteStruct(proto, meta as MioStruct, data as MioObject); break;
     }
 }
Esempio n. 3
0
 internal void Write(MProto target, object data, Type type, MMeta meta)
 {
     if (type == null)
     {
         type = data.GetType();
     }
     if (meta == null)
     {
         meta = MMeta.GetMMeta(type);
     }
     if (meta == null || MType.Null == meta.MType)
     {
         throw new MException("Unsupported Type: " + type);
     }
     else if (MType.Bool == meta.MType)
     {
         target.WriteBool((bool)data);
     }
     else if (MType.Byte == meta.MType)
     {
         target.WriteByte((sbyte)data);
     }
     else if (MType.I16 == meta.MType)
     {
         target.WriteI16((short)data);
     }
     else if (MType.I32 == meta.MType)
     {
         target.WriteI32((int)data);
     }
     else if (MType.I64 == meta.MType)
     {
         target.WriteI64((long)data);
     }
     else if (MType.Float == meta.MType)
     {
         target.WriteFloat((float)data);
     }
     else if (MType.Double == meta.MType)
     {
         target.WriteDouble((double)data);
     }
     else if (MType.Binary == meta.MType)
     {
         if (meta.MainType.IsArray)
         {
             target.WriteBinary((byte[])data);
         }
         else
         {
             var seg = (ArraySegment <byte>)data;
             target.WriteBinary(seg.Array, seg.Offset, seg.Count);
         }
     }
     else if (MType.String == meta.MType)
     {
         target.WriteString((string)data);
     }
     else if (MType.Struct == meta.MType)
     {
         MStruct struc = new MStruct();
         struc.Name = type.Name;
         var fields = GetStructFields(type);
         target.WriteStructBegin(struc);
         //
         foreach (var entry in fields)
         {
             var proptype  = entry.Value.PropertyType;
             var fieldmeta = MMeta.GetMMeta(proptype);
             var value     = entry.Value.GetValue(data, null);
             if (value == null)
             {
                 continue;
             }
             var field = new MField(entry.Value.Name, fieldmeta.MType, entry.Key);
             target.WriteFieldBegin(field);
             Write(target, value, proptype, fieldmeta);
         }
         target.WriteFieldStop();
     }
     else if (MType.Map == meta.MType)
     {
         var keymeta = MMeta.GetMMeta(meta.KeyType);
         var valmeta = MMeta.GetMMeta(meta.ValueType);
         var dict    = data as IDictionary;
         var map     = new MMap(keymeta.MType, valmeta.MType, dict.Count);
         target.WriteMapBegin(map);
         foreach (var key in dict.Keys)
         {
             Write(target, key, meta.KeyType, keymeta);
             var value = dict[key];
             Write(target, value, meta.ValueType, valmeta);
         }
     }
     else if (MType.List == meta.MType)
     {
         var valmeta = MMeta.GetMMeta(meta.ValueType);
         var arr     = data as IEnumerable;
         int count   = 0;
         if (meta.MainType.IsArray)
         {
             count = (data as Array).Length;
         }
         else
         {
             count = (data as IList).Count;
         }
         //
         var list = new MList(valmeta.MType, count);
         target.WriteListBegin(list);
         foreach (var entry in arr)
         {
             Write(target, entry, meta.ValueType, valmeta);
         }
     }
     else if (MType.Set == meta.MType)
     {
         var valmeta = MMeta.GetMMeta(meta.ValueType);
         int count   = (int)meta.ContainerType.GetProperty("Count").GetValue(data, null);
         var arr     = data as IEnumerable;
         var list    = new MList(valmeta.MType, count);
         target.WriteListBegin(list);
         foreach (var entry in arr)
         {
             Write(target, entry, meta.ValueType, valmeta);
         }
     }
 }