JsonHelper.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2021 raoyutian Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using Newtonsoft.Json;
  15. namespace PaddleOCRSharp
  16. {
  17. /// <summary>
  18. /// Json帮助类
  19. /// </summary>
  20. public class JsonHelper
  21. {
  22. /// <summary>
  23. /// Json序列化
  24. /// </summary>
  25. /// <param name="obj"></param>
  26. /// <returns></returns>
  27. public static string SerializeObject(object obj)
  28. {
  29. if (obj == null) return null;
  30. JsonSerializerSettings settings = new JsonSerializerSettings
  31. {
  32. TypeNameHandling = TypeNameHandling.Auto,
  33. };
  34. return JsonConvert.SerializeObject(obj, settings);
  35. }
  36. /// <summary>
  37. /// Json反序列化
  38. /// </summary>
  39. /// <typeparam name="T"></typeparam>
  40. /// <param name="json"></param>
  41. /// <returns></returns>
  42. public static T DeserializeObject<T>(string json)
  43. {
  44. if (string.IsNullOrEmpty(json)) return default(T);
  45. JsonSerializerSettings settings = new JsonSerializerSettings
  46. {
  47. TypeNameHandling = TypeNameHandling.Auto,
  48. };
  49. return (T)JsonConvert.DeserializeObject(json, typeof(T), settings);
  50. }
  51. }
  52. }