ProtobufPacker.cs 1016 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using Newtonsoft.Json;
  3. namespace Model
  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 JsonConvert.SerializeObject(obj);
  14. }
  15. public object DeserializeFrom(Type type, byte[] bytes)
  16. {
  17. return ProtobufHelper.FromBytes(type, bytes);
  18. }
  19. public object DeserializeFrom(Type type, byte[] bytes, int index, int count)
  20. {
  21. return ProtobufHelper.FromBytes(type, bytes, index, count);
  22. }
  23. public T DeserializeFrom<T>(byte[] bytes)
  24. {
  25. return ProtobufHelper.FromBytes<T>(bytes);
  26. }
  27. public T DeserializeFrom<T>(byte[] bytes, int index, int count)
  28. {
  29. return ProtobufHelper.FromBytes<T>(bytes, index, count);
  30. }
  31. public T DeserializeFrom<T>(string str)
  32. {
  33. return JsonConvert.DeserializeObject<T>(str);
  34. }
  35. public object DeserializeFrom(Type type, string str)
  36. {
  37. return JsonConvert.DeserializeObject(str);
  38. }
  39. }
  40. }