ConvertJsonToObject.cs 975 B

123456789101112131415161718192021222324252627
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Newtonsoft.Json;
  6. using System.IO;
  7. namespace Bowin.Common.JSON
  8. {
  9. public static class ConvertJsonToObject
  10. {
  11. public static T ToObject<T>(this string json)
  12. {
  13. return JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Local, NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore });
  14. }
  15. public static string ToJson(this object obj)
  16. {
  17. return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Local, NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore });
  18. }
  19. public static T Map<T>(this object obj) where T : class
  20. {
  21. return obj.ToJson().ToObject<T>();
  22. }
  23. }
  24. }