ProtobufPacker.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.IO;
  3. namespace ETModel
  4. {
  5. public class ProtobufPacker : IMessagePacker
  6. {
  7. public byte[] SerializeToByteArray(object obj)
  8. {
  9. return ProtobufHelper.ToBytes(obj);
  10. }
  11. public string SerializeToText(object obj)
  12. {
  13. return JsonHelper.ToJson(obj);
  14. }
  15. public object DeserializeFrom(Type type, byte[] bytes)
  16. {
  17. return ProtobufHelper.FromBytes(type, bytes);
  18. }
  19. public object DeserializeFrom(Type type, Stream stream)
  20. {
  21. return ProtobufHelper.FromStream(type, stream);
  22. }
  23. public object DeserializeFrom(Type type, byte[] bytes, int index, int count)
  24. {
  25. return ProtobufHelper.FromBytes(type, bytes, index, count);
  26. }
  27. public T DeserializeFrom<T>(byte[] bytes)
  28. {
  29. return ProtobufHelper.FromBytes<T>(bytes);
  30. }
  31. public T DeserializeFrom<T>(byte[] bytes, int index, int count)
  32. {
  33. return ProtobufHelper.FromBytes<T>(bytes, index, count);
  34. }
  35. public T DeserializeFrom<T>(string str)
  36. {
  37. return JsonHelper.FromJson<T>(str);
  38. }
  39. public object DeserializeFrom(Type type, string str)
  40. {
  41. return JsonHelper.FromJson(type, str);
  42. }
  43. }
  44. }