dotnetcore / dotnetcore/FreeSql

PG 数据库 JsonMap Update/Insert 没有按照指定 JsonSerializerSettings 序列化

Open
#2,279 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

#### 问题描述及重现代码:

之前是 MySQL 默认不设置 `sql.UseJsonMap();` 也不会换行,PG 后怎么配置都会换行
```c#
Newtonsoft.Json.JsonConvert.DefaultSettings = () => new Newtonsoft.Json.JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.None, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore };
```
两种都试过,倒不是大问题

初始化
```c#
// c# code
sql = build.UseConnectionFactory(DataType.PostgreSQL, () => new NpgsqlConnection(com)).Build(); //请务必定义成 Singleton 单例模式
sql.UseJsonMap(new Newtonsoft.Json.JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.None, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore });
```
数据库工具中查看依旧是带格式化的
Image

更新数据
```c#
var dCamera = new Dictionary(3);
Db.sql.Update().Where(a => a.id == id).Set(a => a.cameras, dCamera).Set(a => a.cameraNum, dCamera.Count).ExecuteAffrows();
```

表实体
```c#
[Description("设备")]
public class r_device
{
///
/// PUID
///
[Column(IsPrimary = true, StringLength = 32)]
[Description("PUID")]
public string id { get; set; }

///
/// 状态
///
[Description("状态")]
public bool state { get; set; }

///
/// 名称
///
[Column(StringLength = 64)]
[Description("名称")]
public string name { get; set; }

///
/// 在线状态
///
[Description("在线状态")]
public bool online { get; set; }

///
/// 摄像头信息
///
[JsonMap]
[Description("摄像头信息")]
[Column(DbType = "text")]
public Dictionary cameras { get; set; }

///
/// 摄像头数量
///
[Description("摄像头数量")]
public int cameraNum { get; set; }
}

public class h_deviceCamera
{
///
/// 摄像头名称
///
public string name { get; set; }
///
/// 启用AI
///
public int ai { get; set; }
///
/// 拉流
///
public int pull { get; set; }
}
```

期望:

Image

#### 数据库版本
3.5.311

#### 安装的Nuget包
FreeSql.Provider.PostgreSQL

#### .net framework/. net core? 及具体版本
NET 10 `10.0.303`

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the PostgreSQL case using sql.UseJsonMap with Formatting.None and NullValueHandling.Ignore, then update the [JsonMap] cameras property through Update().Set().Start by tracing the PostgreSQL provider path behind UseJsonMap and the Update/Insert serialization entry points. Done means the stored JSON follows the supplied JsonSerializerSettings without unwanted formatting or null values.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.