解决long序列化问题

master
lxw 6 months ago
parent b295fde2f5
commit 3dc9ed6aaf

@ -0,0 +1,18 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Common.Util;
public class LongJsonConverter : JsonConverter<long>
{
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? s = reader.GetString();
return long.Parse(s);
}
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}

@ -8,9 +8,10 @@ namespace Entity.Base
public abstract class EntityBaseId public abstract class EntityBaseId
{ {
/// <summary> /// <summary>
/// 雪花Id /// 自增
/// </summary> /// </summary>
[SugarColumn(ColumnName = "id", ColumnDescription = "主键Id", IsPrimaryKey = true, IsIdentity = false)] [SugarColumn(ColumnName = "id", ColumnDescription = "主键Id", IsPrimaryKey = true, IsIdentity = false)]
public virtual long Id { get; set; } public virtual long Id { get; set; }
} }
/// <summary> /// <summary>

@ -1,6 +1,7 @@
using System.Text; using System.Text;
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Common.Util;
using Entity.Dto.Resp; using Entity.Dto.Resp;
using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.AutoTask; using HybirdFrameworkCore.AutoTask;
@ -25,7 +26,6 @@ builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
{ {
cb.Register(c => cb.Register(c =>
{ {
var db = new SqlSugarScope(new ConnectionConfig var db = new SqlSugarScope(new ConnectionConfig
{ {
ConfigId = AppSettingsConstVars.ConfigId, ConfigId = AppSettingsConstVars.ConfigId,
@ -34,13 +34,11 @@ builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
IsAutoCloseConnection = true, IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute // 如果使用实体类的属性进行主键标识,请设置为 InitKeyType.Attr InitKeyType = InitKeyType.Attribute // 如果使用实体类的属性进行主键标识,请设置为 InitKeyType.Attr
}); });
return db; return db;
}).As<ISqlSugarClient>().SingleInstance(); }).As<ISqlSugarClient>().SingleInstance();
cb.RegisterModule(new AutofacModuleRegister()); cb.RegisterModule(new AutofacModuleRegister());
}); });
// 注册自定义映射 // 注册自定义映射
@ -67,7 +65,7 @@ builder.Services.AddCors(options =>
//redis //redis
var redisConnectionString = AppSettingsHelper.GetContent("Redis", "Connection"); var redisConnectionString = AppSettingsHelper.GetContent("Redis", "Connection");
var instanceName = AppSettingsHelper.GetContent("Redis", "InstanceName");//默认数据库 var instanceName = AppSettingsHelper.GetContent("Redis", "InstanceName"); //默认数据库
var defaultDb = int.Parse(AppSettingsHelper.GetContent("Redis", "DefaultDB") ?? "0"); var defaultDb = int.Parse(AppSettingsHelper.GetContent("Redis", "DefaultDB") ?? "0");
if (redisConnectionString != null && instanceName != null) if (redisConnectionString != null && instanceName != null)
builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, defaultDb)); builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, defaultDb));
@ -75,6 +73,7 @@ if (redisConnectionString != null && instanceName != null)
builder.Services.AddControllers().AddJsonOptions(configure => builder.Services.AddControllers().AddJsonOptions(configure =>
{ {
configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter()); configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
configure.JsonSerializerOptions.Converters.Add(new LongJsonConverter());
}); });
builder.Services.AddControllers(); builder.Services.AddControllers();
@ -89,8 +88,6 @@ builder.Services.AddSwaggerGen(c =>
c.IncludeXmlComments(Path.Combine(basePath, "WebStarter.xml"), true); c.IncludeXmlComments(Path.Combine(basePath, "WebStarter.xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "Entity.xml"), true); c.IncludeXmlComments(Path.Combine(basePath, "Entity.xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true); c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true);
}); });
builder.Services.AddControllers().AddJsonOptions(configure => builder.Services.AddControllers().AddJsonOptions(configure =>
@ -120,9 +117,6 @@ builder.Services.AddAuthentication(options =>
}); });
var app = builder.Build(); var app = builder.Build();
@ -164,7 +158,6 @@ if (AppSettingsHelper.GetBool("swap", "enable"))
} }
TaskInit.Init(); TaskInit.Init();

Loading…
Cancel
Save