Пример #1
0
        public static byte[] Serialize <TObj>(TObj obj, Endian endian = Endian.BIG_ENDIAN, Encoding?textEncoding = null)
            where TObj : class
        {
            using var bodyWriter = BytesFactory.GetWriter(endian);
            // Write Body
            var desc = ByteProto.GetTargetDescriptor(typeof(TObj));

            WriteObject(desc, obj, bodyWriter, textEncoding ?? Encoding.UTF8);

            return(bodyWriter.Bytes.ToArray());
        }
Пример #2
0
        public static object Deserialize(Type outType, byte[] rawData, Endian endian = Endian.BIG_ENDIAN, Encoding?textEncoding = null)
        {
            if (!ByteProto.TargetHasDescriptor(outType, out var desc) || desc == null)
            {
                throw new Exception($"cannot find a class for type: {outType} ");
            }

            var reader = BytesFactory.GetReader(rawData, endian);

            return(ReadObject(reader, desc, textEncoding ?? Encoding.UTF8));
        }