.Net Core 配置之long类型 前端精度丢失和时间格式设置

在很多项目中,都采用的前后端分离的方式进行开发,经常遇到后台的long精度的数据到前端丢失不准确,显示效果为long类型(19位)的后几位为000,此时需要对long的字段进行设置,改变默认的返回类型,由long类型改变为string类型。所以需要全局自定义修改long类型的返回类型

  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Serialization;
  3. using System;
  4.  
  5. namespace NetCore3WebApiTemplate.Utility
  6. {
  7. public class CustomContractResolver : CamelCasePropertyNamesContractResolver
  8. {
  9. ///// <summary>
  10. ///// 实现首字母小写
  11. ///// </summary>
  12. ///// <param name="propertyName"></param>
  13. ///// <returns></returns>
  14. //protected override string ResolvePropertyName(string propertyName)
  15. //{
  16. // return propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
  17. //}
  18.  
  19. /// <summary>
  20. /// 对长整型做处理
  21. /// </summary>
  22. /// <param name="objectType"></param>
  23. /// <returns></returns>
  24. protected override JsonConverter ResolveContractConverter(Type objectType)
  25. {
  26. if (objectType == typeof(long))
  27. {
  28. return new JsonConverterLong();
  29. }
  30. return base.ResolveContractConverter(objectType);
  31. }
  32.  
  33. ///// <summary>
  34. ///// Creates a Newtonsoft.Json.Serialization.JsonProperty for the given System.Reflection.MemberInfo.
  35. ///// </summary>
  36. ///// <param name="type"></param>
  37. ///// <param name="memberSerialization"></param>
  38. ///// <returns></returns>
  39. //protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
  40. //{
  41. // return type.GetProperties()
  42. // .Select(p =>
  43. // {
  44. // var jp = base.CreateProperty(p, memberSerialization);
  45. // jp.ValueProvider = new NullToEmptyStringValueProvider(p);
  46. // return jp;
  47. // }).ToList();
  48. //}
  49. }
  50. }

  

  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace NetCore3WebApiTemplate.Utility
  7. {
  8. /// <summary>
  9. /// Long类型Json序列化重写
  10. /// 在js中传输会导致精度丢失,故而在序列化时转换成字符类型
  11. /// </summary>
  12. public class JsonConverterLong : JsonConverter
  13. {
  14. /// <summary>
  15. /// 是否可以转换
  16. /// </summary>
  17. /// <param name="objectType"></param>
  18. /// <returns></returns>
  19. public override bool CanConvert(Type objectType)
  20. {
  21. return true;
  22. }
  23.  
  24. /// <summary>
  25. /// 读json
  26. /// </summary>
  27. /// <param name="reader"></param>
  28. /// <param name="objectType"></param>
  29. /// <param name="existingValue"></param>
  30. /// <param name="serializer"></param>
  31. /// <returns></returns>
  32. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  33. {
  34. if ((reader.ValueType == null || reader.ValueType == typeof(long?)) && reader.Value == null)
  35. {
  36. return null;
  37. }
  38. else
  39. {
  40. long.TryParse(reader.Value != null ? reader.Value.ToString() : "", out long value);
  41. return value;
  42. }
  43. }
  44.  
  45. /// <summary>
  46. /// 写json
  47. /// </summary>
  48. /// <param name="writer"></param>
  49. /// <param name="value"></param>
  50. /// <param name="serializer"></param>
  51. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  52. {
  53. if (value == null)
  54. writer.WriteValue(value);
  55. else
  56. writer.WriteValue(value + "");
  57. }
  58. }
  59. }

在startup的ConfigureServices中设置

序列化的格式

  1. options.SerializerSettings.ContractResolver = new CustomContractResolver();
  2.  
  3. 时间格式:
  1. options.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
  1. 代码如下:
  1. /// <summary>
  2. /// This method gets called by the runtime. Use this method to add services to the container.
  3. /// </summary>
  4. /// <param name="services"></param>
  5. public void ConfigureServices(IServiceCollection services)
  6. {
  7. services.AddControllers(ops =>
  8. {
  9.  
  10. }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
  11. .AddNewtonsoftJson(options =>
  12. {
  13. //设置时间格式
  14. options.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
  15. options.SerializerSettings.ContractResolver = new CustomContractResolver();
  16. });
  17. }

  

注意:

    1. AddNewtonsoftJson 方法需要应用Microsoft.AspNetCore.Mvc.NewtonsoftJson
  1. https://www.shuzhiduo.com/A/q4zVxXex5K/
posted @ 2021-05-24 17:15  China Soft  阅读(528)  评论(0编辑  收藏  举报