SerializeHelper.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ropin.Core.Common
  8. {
  9. public class SerializeHelper
  10. {
  11. /// <summary>
  12. /// 序列化
  13. /// </summary>
  14. /// <param name="item"></param>
  15. /// <returns></returns>
  16. public static byte[] Serialize(object item)
  17. {
  18. var jsonString = JsonConvert.SerializeObject(item);
  19. return Encoding.UTF8.GetBytes(jsonString);
  20. }
  21. /// <summary>
  22. /// 反序列化
  23. /// </summary>
  24. /// <typeparam name="TEntity"></typeparam>
  25. /// <param name="value"></param>
  26. /// <returns></returns>
  27. public static TEntity Deserialize<TEntity>(byte[] value)
  28. {
  29. if (value == null)
  30. {
  31. return default(TEntity);
  32. }
  33. var jsonString = Encoding.UTF8.GetString(value);
  34. return JsonConvert.DeserializeObject<TEntity>(jsonString);
  35. }
  36. }
  37. }