WebAPI项目从Newtonsoft.Json迁移到System.Text.Json踩坑备忘

1.控制器层方法返回类型不能为元组

控制器层方法返回类型为元组时,序列化结果为空。

因为元组没有属性只有field,除非使用IncludeFields参数专门指定,否则使用System.Text.Json进行序列化时不会序列化field

cs 复制代码
var options = new JsonSerializerOptions
{
    IncludeFields = true,
};
var json = JsonSerializer.Serialize(tuple1, options);

2.控制器层方法返回类型不能为Newtonsoft.Json专用类型

控制器层方法返回类型如果为JToken/JObject/JArray等Newtonsoft.Json专用类型,无法被System.Text.Json序列化,需替换为JsonNode/JsonObject/JsonArray等System.Text.Json库专用类型

3.System.Text.Json暂不支持JsonPath,需引入第三方类库

4.redis使用注意事项

如果项目内使用的是CSRedisCore库,应注意其依赖的是Newtonsoft.Json,调用Redis读写方法时应注意数据类型不能是JsonNode/JsonObject/JsonArray等System.Text.Json库专用类型

相关推荐
cykaw259010 小时前
QT-JSON
qt·json
CCI34418 小时前
报错SvelteKitError: Not found: /.well-known/appspecific/com.chrome.devtools.json
javascript·chrome·json
椰椰椰耶1 天前
[网页五子棋][匹配模块]前后端交互接口(消息推送机制)、客户端开发(匹配页面、匹配功能)
java·spring boot·json·交互·html5·web
梁小呆瓜1 天前
掌握Jackson的灵活扩展:@JsonAnyGetter与@JsonAnySetter详解
java·spring boot·json
西京刀客2 天前
python常用库-pandas、Hugging Face的datasets库(大模型之JSONL(JSON Lines))
python·json·数据集·pandas·模型训练·datasets
Blue桃之夭夭2 天前
HTML、XML、JSON 是什么?有什么区别?又是做什么的?
xml·html·json
赴前尘3 天前
Go 中 `json.NewEncoder/Decoder` 与 `json.Marshal/Unmarshal` 的区别与实践
开发语言·golang·json
颯沓如流星3 天前
大规模JSON反序列化性能优化实战:Jackson vs FastJSON深度对比与定制化改造
性能优化·json
come112343 天前
全面解析:npm 命令、package.json 结构与 Vite 详解
arcgis·npm·json
Blue桃之夭夭3 天前
Python进阶【四】:XML和JSON文件处理
xml·python·json