使用JsonProperty Attribute修改返回json

使用JsonProperty Attribute修改返回 json 值的name

本例使用JsonPropertyAttribute在序列化为JSON时更改属性的名称。

public class Videogame
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("release_date")]
    public DateTime ReleaseDate { get; set; }
}
Videogame starcraft = new Videogame
{
    Name = "Starcraft",
    ReleaseDate = new DateTime(1998, 1, 1)
};

string json = JsonConvert.SerializeObject(starcraft, Formatting.Indented);

Console.WriteLine(json);
// {
//   "name": "Starcraft",
//   "release_date": "1998-01-01T00:00:00"
// }

排序

public class Account
{
    public string EmailAddress { get; set; }

    // appear last
    [JsonProperty(Order = 1)]
    public bool Deleted { get; set; }

    [JsonProperty(Order = 2)]
    public DateTime DeletedDate { get; set; }

    public DateTime CreatedDate { get; set; }
    public DateTime UpdatedDate { get; set; }

    // appear first
    [JsonProperty(Order = -2)]
    public string FullName { get; set; }
}
Account account = new Account
{
    FullName = "Aaron Account",
    EmailAddress = "aaron@example.com",
    Deleted = true,
    DeletedDate = new DateTime(2013, 1, 25),
    UpdatedDate = new DateTime(2013, 1, 25),
    CreatedDate = new DateTime(2010, 10, 1)
};

string json = JsonConvert.SerializeObject(account, Formatting.Indented);

Console.WriteLine(json);
// {
//   "FullName": "Aaron Account",
//   "EmailAddress": "aaron@example.com",
//   "CreatedDate": "2010-10-01T00:00:00",
//   "UpdatedDate": "2013-01-25T00:00:00",
//   "Deleted": true,
//   "DeletedDate": "2013-01-25T00:00:00"
// }

在反序列化期间使用的Required,以验证是否存在所需的JSON属性

public class Videogame
{
    [JsonProperty(Required = Required.Always)]
    public string Name { get; set; }

    [JsonProperty(Required = Required.AllowNull)]
    public DateTime? ReleaseDate { get; set; }
}
string json = @"{
  'Name': 'Starcraft III',
  'ReleaseDate': null
}";

Videogame starcraft = JsonConvert.DeserializeObject<Videogame>(json);

Console.WriteLine(starcraft.Name);
// Starcraft III

Console.WriteLine(starcraft.ReleaseDate);
// null

JsonIgnoreAttribute

使用JsonIgnoreAttribute从序列化中排除属性

public class Account
{
    public string FullName { get; set; }
    public string EmailAddress { get; set; }

    [JsonIgnore]
    public string PasswordHash { get; set; }
}

详情请参考 https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm

转载于:https://www.cnblogs.com/WNpursue/p/10755011.html

### 改进YOLOv8模型性能的方法 #### Extea DW方法概述 Extea DW是一种专注于提升卷积神经网络(CNN)计算效率的技术,通过深度可分离卷积(DW Convolution),减少计算量的同时保持较高的精度。对于YOLOv8这样的实时目标检测框架来说,应用Extea DW可以显著提高处理速度和资源利用率。 #### 实现细节 为了将Extea DW应用于YOLOv8,主要涉及以下几个方面: - **替换标准卷积层** 将原有的标准卷积层替换成深度可分离卷积结构。这种转换可以在不改变原有架构的情况下降低运算复杂度[^1]。 ```python import torch.nn as nn class DepthwiseSeparableConv(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1): super().__init__() self.depthwise = nn.Conv2d(in_channels, in_channels, kernel_size, stride=stride, groups=in_channels, padding=padding) self.pointwise = nn.Conv2d(in_channels, out_channels, 1) def forward(self, x): x = self.depthwise(x) x = self.pointwise(x) return x ``` - **优化超参数设置** 调整学习率、批量大小和其他影响收敛性的因素来适应新的卷积方式带来的变化。这一步骤通常需要借助验证集来进行多次实验找到最优解[^2]. - **微调预训练权重** 如果有可用的预训练模型,则可以通过迁移学习的方式加载这些权重,并针对特定任务进一步微调。这样不仅可以加快训练过程还能改善最终效果。 #### 训练与评估流程 完成上述修改之后,按照常规步骤编译项目并启动训练会话。值得注意的是,在此过程中应当密切监控损失函数的变化趋势以及各项指标的表现情况,以便及时作出必要的调整。 ```bash # 激活虚拟环境并切换至工作目录 conda activate yolov8 cd pyCode/yolov8/ultralytics/ # 启动带有Extea DW改进后的YOLOv8训练 yolo task=detect mode=train model=yolov8s_pt_with_extea_dw.pth data=datasets/VisDrone.yaml batch=16 epochs=100 imgsz=640 workers=0 device=0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值