You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求可省略属性名的C#序列化/反序列化协议以压缩API响应

缩减大API响应体积的序列化方案需求

我想缩减大API调用的JSON响应体积,知道可以用MessagePack但不想处理它的一些缺点。我的大部分场景里,超过一半的数据都是重复出现的属性名,因此有以下需求:

  • 找一种能省略属性名的序列化/反序列化协议
  • 最好支持数据压缩
  • 必须是C#实现
  • 希望能在响应中提供上下文/Schema,让客户端解析更轻松灵活

我准备了一个示例,模拟返回100000辆汽车和约425000个车轮的API调用,灵感来自OpenAPI规范,看下面JSON里的data属性:

C#实体类

public class Car
{
    public int Year { get; set; }
    public string Model { get; set; }
    public List<Wheel> Wheels { get; set; }
}

public class Wheel
{
    public double Diameter { get; set;}
    public string TireSize { get; set; }
    public string BoltPattern { get; set; }
}

期望的响应结构

{
    "responseDataType": {// 这个属性固定存在,说明"data"属性的类型
        "type": "array",
        "items": {
            "$ref": "#/schemas/Car"
        }
    },
    "schemas": {// "data"属性的模板/类型定义
        "Car": {
            "type": "object",
            "properties": {
                "year": {
                    "type": "integer"
                },
                "model": {
                    "type": "string"
                },
                "wheels": {
                    "type": "array",
                    "items": {
                        "$ref": "#/schemas/Wheel"
                    }
                }
            }
        },
        "Wheel": {
            "type": "object",
            "properties": {
                "diameter": {
                    "type": "number"
                },
                "tireSize": {
                    "type": "string"
                },
                "boltPattern": {
                    "type": "string"
                }
            }
        }
    },
    "data":[
         {/* 99998条数据... */},
         // 可以看到属性名没有重复,反序列化器会根据提供的"schemas"/"responseDataType"解析
         // 还可以去掉缩进节省更多空间,甚至压缩?
        {
            2000,
            "Toyota",
            [
                {16,"275/40r16","4x100"},
                {16,"275/40r16","4x100"},
                {16,"275/40r16","4x100"},
                {16,"275/40r16","4x100"}            
            ]
        },
        {
            2005,
            "Jeep",
            [
                {17,"33x12.5","5x127"},
                {17,"33x12.5","5x127"},
                {17,"33x12.5","5x127"},
                {17,"33x12.5","5x127"}, 
                {17,"33x12.5","5x127"}          
            ]
        },
        {/* 99998条数据... */}
    ]
}

内容的提问来源于stack exchange,提问作者Iannick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 07:17:02